nginx之rewrite

1.rewrite的应用场景

1.URL访问跳转,支持开发设计页面跳转,兼容性支持,展示效果等
2.SEO优化
3.维护
4.安全

语法:rewrite regex replacement flag;,如:

rewrite ^/images/(.*.jpg)$ /imgs/$1 break;
rewrite ^/bbs/(.*)$ http://www.idfsoft.com/index.html redirect;
[root@khd~]#vim/usr/local/nginx/conf/nginx.conf
location / {
            root   html;
            index  index.html index.htm;
        }
        location /status {
            stub_status on;
            allow all;
        }
        location /lcr{
        root html;
        rewrite ^/lcr/(.*.PNG)$ /ly/$1 break;

验证
image

1.2 图片换位置指定所放位置

 location /lcr {
        root html;
        rewrite ^/lcr/(.*.PNG)$ /ly/$1 break;
        }

       location /ly {
       root  /var/www/html;
       }
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #

验证
image

1.3 break 指定网络位置

location /lcr {
        root html;
        rewrite ^/lcr/(.*.PNG)$ http://chuantu.xyz/t6/702/1560238118x992245975.jpg break;
        }

       location /ly {
       root  /var/www/html;
       }

验证

1.4 location直接指定放的位置

location /status {
            stub_status on;
            allow all;
        }
        location /lcr {
        root /var/www/html;
        rewrite ^/lcr/(.*.PNG)$ /ly/$1 break;
        }

验证
image

1.5 break 的使用

 location /status {
            stub_status on;
            allow all;
        }
        location /lcr {
        root /var/www/html;
        rewrite ^/lcr/(.*.PNG)$ /ly/$1 break;
        rewrite ^/ly/(.*.PNG)$ /cwh/$1 break;
        }
[root@khd ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@khd ~]# nginx -s reload

验证
image

1.6 break和last结合用法

location /lcr {
        root /var/www/html;
        rewrite ^/lcr/(.*.PNG)$ /ly/$1 last;
        rewrite ^/ly/(.*.PNG)$ /cwh/$1 break;
        }

        location /ly {
        root /var/www/html;
        }
root@khd ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@khd ~]# nginx -s reload

验证
image

1.7break和last的第二种用法

[root@khd ~]# cd /var/www/html/
[root@khd html]# ls
ly
[root@khd html]# mv ly cwh
[root@khd html]# ls
cwh
[root@khd html]# cd
[root@khd ~]# vim /usr/local/nginx/conf/nginx.conf
[root@khd ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@khd ~]# nginx -s reload
location /lcr {
        root /var/www/html;
        rewrite ^/lcr/(.*.PNG)$ /ly/$1 last;
        }

        location /ly {
        root /var/www/html;
        rewrite ^/ly/(.*.PNG)$ /cwh/$1 break;
        }

验证
image

常见的flag

--- ---
flag 作用
last 基本上都用这个flag,表示当前的匹配结束,继续下一个匹配,最多匹配10个到20个
一旦此rewrite规则重写完成后,就不再被后面其它的rewrite规则进行处理
而是由UserAgent重新对重写后的URL再一次发起请求,并从头开始执行类似的过程
break 中止Rewrite,不再继续匹配
一旦此rewrite规则重写完成后,由UserAgent对新的URL重新发起请求,且不再会被当前location内的任何rewrite规则所检查
redirect 以临时重定向的HTTP状态302返回新的URL
permanent 以永久重定向的HTTP状态301返回新的URL

rewrite模块的作用是用来执行URL重定向。这个机制有利于去掉恶意访问的url,也有利于搜索引擎优化(SEO)

nginx使用的语法源于Perl兼容正则表达式(PCRE)库,基本语法如下:

--- ---
标识符 意义
^ 必须以^后的实体开头
$ 必须以$前的实体结尾
. 匹配任意字符
| 匹配
[] 匹配指定字符集内的任意字符
[^] 匹配任何不包括在指定字符集内的任意字符串
() 分组,组成一组用于匹配的实体,通常会有 | 来协助

捕获子表达式,可以捕获放在()之间的任何文本,比如:

^(hello|sir)$       //字符串为“hi sir”捕获的结果:$1=hi$2=sir

//这些被捕获的数据,在后面就可以当变量一样使用了
原文地址:https://www.cnblogs.com/ly0629/p/11004737.html