Nginx实现Rewrite重写

时间:2021-08-07
本文章向大家介绍Nginx实现Rewrite重写 ,主要包括Nginx实现Rewrite重写 使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Nginx实现Rewrite重写

什么是rewrite

Rewrite主要实现url地址重写,以及重定向,就是把传入web的请求重定向到其他url的过程。

Rewrite的使用场景

  • 1、地址跳转,用户访问www.drz.com这个URL是,将其定向至一个新的域名mobile.drz.com
  • 2、协议跳转,用户通过http协议请求网站时,将其重新跳转至https协议方式
  • 3、伪静态,将动态页面显示为静态页面方式的一种技术,便于搜索引擎的录入,同时建上动URL地址对外暴露过多的参数,提升更高的安全性。
  • 4、搜索引擎,SEO优化依赖于url路径,好记的url便于智齿搜索引擎录入

Rewrite的语法

语法:Syntax:  rewrite regex replacement [flag]
默认:Default: --
语境:Context: server,location,if

#用于切换维护页面场景
rewrite ^(.*)$ /page/maintain.html break;

Rewrite标记Flog

rewrite指令根据表达式来重定向URL,或者修改字符串,可以应用于server,location,if环境下,每行rewrite指令最后跟一个flag标记,支持的flag标记有如下表格所示:

flag 作用
last(不使用) 本条规则匹配完成后,停止匹配,不再匹配后面的规则
break(不使用) 本条规则匹配完成后,停止匹配,不再匹配后面的规则
redirect 返回302临时重定向,地址栏会显示跳转后的地址
permanent 返回301永久重定向,地址栏会显示跳转后的地址

redirect与permanent区别

redirect: 每次请求都会询问服务器,如果当服务器不可用时,则会跳转失败。

permanent: 第一次请求会询问,浏览器会记录跳转的地址,第二次则不再询问服务器,直接通过浏览器缓存的地址跳转。

简单的Rewrite跳转(跳转到百度)

[root@web01 ~]# vim /etc/nginx/conf.d/blog.dsr.com.conf
server{
        listen 80;
        server_name blog.dsr.com;
        root /code/wordpress;

        location / {
                index index.php inde.html;
        }

        location ~ \.php {
                fastcgi_pass unix:/code/php71w.sock;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include /etc/nginx/fastcgi_params;
        }

        location /ds {
                rewrite ^/ds http://www.baidu.com redirect;
        }
}

rewrite实战

开启rewrite日志
[root@web01 ~]# vim /etc/nginx/nginx.conf
error_log /var/log/nginx/error.log notice;

http {
     rewrite_log on;
}

案列一

用户访问/abc/1.html实际上真实访问的是/ccc/bbb/2.html

# 1.创建站点目录
[root@web01 ~]# mkdir /code/ccc/bbb -p

# 2.创建默认页面
[root@web01 ~]# echo '/ccc/bbb/2.html' > /code/ccc/bbb/2.html

# 3.rewrite编写
[root@web01 ~]# vim /etc/nginx/conf.d/test_rewrite.conf
server {
        listen 80;
        server_name test.dsr.com;
        root /code;

        location / {
                index index.html;
        }

        location /abc {
                rewrite ^/abc /ccc/bbb/2.html redirect;
        }
}

案例二

用户访问 /2018/ccc/2.html 实际上真实访问的是 /2014/ccc/bbb/2.html

[root@web01 ~]# mkdir /code/2014/ccc/bbb/ -p
[root@web01 ~]# echo '/2014/ccc/bbb/2.html' > /code/2014/ccc/bbb/2.html
[root@web01 ~]# vim /etc/nginx/conf.d/test_rewrite.conf
server {
        listen 80;
        server_name test.dsr.com;
        root /code;

        location / {
                index index.html;
        }

        location /abc {
                rewrite ^/abc /ccc/bbb/2.html redirect;
        }
        location /2018 {
				rewrite ^/2018/(.*) /2014/$1 redirect;
		}
}

案例三

用户访问 course-11-22-33.html 实际上真实访问的是 /course/11/22/33/course_33.html

[root@web01 ~]# mkdir /code/course/11/22/33/ -p

[root@web01 ~]# echo '[root@web01 ~]# mkdir /code/course/11/22/33/ -p' >
/code/course/11/22/33/course_33.html

[root@web01 nginx]# vim /etc/nginx/conf.d/test_rewrite.conf
server {
        listen 80;
        server_name test.dsr.com;
        root /code;

        location / {
                index index.html;
        }

        location /abc {
                rewrite ^/abc /ccc/bbb/2.html redirect;
        }
        location /2018 {
				rewrite ^/2018/(.*) /2014/$1 redirect;
		}
		location ~ ^/course {
				rewrite course-(.*)-(.*)-(.*).html /course/$1/$2/$3/course_$3.htmlredirect;
		}
}

wordpress 页面rewrite实战(实现查看文章显示静态页面)

[root@web01 ~]# vim /etc/nginx/conf.d/blog.dsr.com.conf 
server{
        listen 80;
        server_name blog.dsr.com;
        root /code/wordpress;

        location / {
                index index.php inde.html;
                if ( -f $request_filename/index.html ){
                        rewrite (.*) $1/index.html break;
                }
                if ( -f $request_filename/index.php ){
                        rewrite (.*) $1/index.php;
                }
                if ( !-f $request_filename ){
                        rewrite (.*) /index.php;
                }
                if ($http_user_agent ~* "Wget|ApacheBench|webBench|isouSpider|MJ12bot|YoudaoBot|Tomato|bingbot/2.0|com
patible"){
                        set $block_user_agent 1;
                }
                if ($block_user_agent = 1){
                        return 403;
                }
        }

        location ~ \.php {
                fastcgi_pass unix:/code/php71w.sock;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include /etc/nginx/fastcgi_params;
        }


Rewrite规则补充

Rewrite优先级

  • 1.先执行server块的rewrite指令
  • 2.其次执行location匹配规则
  • 3.最后执行location中的rewrite

rewrite可以使用nginx中的全局变量

$server_name    #当前用户请求的域名
$request_filename 请求的文件路径名(带网站的主目录/code/images/test.jpg)
$request_uri 当前请求的文件路径(不带网站的主目录/inages/test.jpg)

server {
		listen 80;
		server_name php.drz.com;
		return 302 https://$server_name$request_uri;
		rewrite ^(.*)$ https://$server_name$request_uri redirect;
}

原文地址:https://www.cnblogs.com/dsryyds/p/15111552.html