write rules introduced
Rewite rule action
- Rewrite rules can be implemented to rewrite the url, and redirect
Action scenes:
- Jump URL access, support the development of design, such as page jumps, the compatibility support, showing effects
- SEO optimization
- Maintenance: background maintenance, traffic forwarding, etc.
- Safety
Note: nginx official document: http://nginx.org/en/docs/http/ngx_http_rewrite_module.html
Configuration syntax:
- Syntax: rewrite regex replacement [flag];
- Default:——
- Context:server、location、if
Simple examples
rewrite ^(.*)$ /pages/maintain.html break;
explain:
- We will redirect all requests to /pages/maintain.html page
Common regular expression
Under linux, can be used to test pcretest
Small example
rewrite index\.php$ /pages/maintain.html break; if ($http_user_agent ~ MSIE) { rewrite ^(.*)$ /msie/$1 break; }
explain
- \ - escape character
- () - means for matching content between parentheses, by $ 1, $ 2 call
(*) Content will extract all the parameters, passed / msie / under
rewrite configuration syntax flag parameters
last stop rewrite detection [If there is no match to, will continue to match the downward]
[Stop rewrite detecting break if no matched, no longer matches the downwardly directly returns the result 404]
302 temporary redirect redirect return address after the jump address bar displays
permanent return 301 permanent redirect, the address after the jump address bar displays
last break and the difference between Case
test_rewrite1.conf
server { listen 80 default_server; server_name www.zhangbiao.com; access_log /var/log/nginx/log/host.access.log main; root /opt/app/code; location ~ ^/break { rewrite ^/break /test/ break; } location ~ ^/last { rewrite ^/last /test/ last; } location /test/ { default_type application/json; return 200 '{"status":"success"}'; } }
The whole process can be seen in the end-use request curl -vL
curl -vL www.zhangbiao.com/last/
access
http://www.zhangbiao.com/test/
access
http://www.zhangbiao.com/last/
access
http://www.zhangbiao.com/break/
可以发现访问 last 的时候新建立了一个请求 /test/ ,而访问/break/ 请求的时候 会去 /opt/app/code 下找相应的资源,没找到所以报错
Rewrite规则_redirect和permanent区别
test_rewrite1.conf
server { listen 80 default_server; server_name www.zhangbiao.com; access_log /var/log/nginx/log/host.access.log main; root /opt/app/code; location ~ ^/imooc { rewrite ^/imooc http://www.imooc.com/ permanent; #rewrite ^/imooc http://www.imooc.com/ redirect; } }
redirect 表示临时的重定向 ,只要后端服务是开者的。每次访问 /imoc 都会重定向到 http://www.imooc.com
permanent 表示永久重定向,第一次访问成功后,把后端服务关闭后,访问/imoc 仍然会重定向到 http://www.imooc.com
rewrite 规则使用场景案例
test_rewrite.conf
server { listen 80; server_name www.zhangbiao.com; root /opt/app/code; location / { rewrite ^/course-(\d+)-(\d+)-(\d+)\.html$ /course/$1/$2/course_$3.html break; if ($http_user_agent ~* Chrome) { rewrite ^/nginx http://coding.imooc.com/class/121.html redirect; } if (!-f $request_filename) { rewrite ^/(.*)$ http://www.baidu.com/$1 redirect; } index index.html index.htm; } error_page 500 502 503 504 404 /50x.html; }
访问在 /opt/app/code/course/11/22 下存在的资源文件
http://www.zhangbiao.com/course-11-22-33.html
访问在 /opt/app/code/course/11/22 下不存在的资源文件
http://www.zhangbiao.com/course-11-22-5
rewrite优先级规则
- 执行server 块的rewrite 指令。将所有的网站都重定向同一个网站。
- 执行location匹配。
- 执行选定的location中的rewrite。