目录html
1.URL访问跳转,支持开发设计页面跳转,兼容性支持,展现效果等
2.SEO优化
3.维护
4.安全nginx
语法: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;
验证
vim
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 #
验证
安全
location /lcr { root html; rewrite ^/lcr/(.*\.PNG)$ http://chuantu.xyz/t6/702/1560238118x992245975.jpg break; } location /ly { root /var/www/html; }
验证
网络
location /status { stub_status on; allow all; } location /lcr { root /var/www/html; rewrite ^/lcr/(.*\.PNG)$ /ly/$1 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
验证
搜索引擎
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
验证
url
[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; }
验证
设计
常见的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 //这些被捕获的数据,在后面就能够当变量同样使用了