10.12-10.15 rewrite配置if,break和last的用法,rewrite规则,Nginx全局变量
Nginx的Rwrite配置
加粗 域名跳转(重定向)、URL重写(伪静态)、动静分离(跳转域名,并接入CDN实现加速)
依赖PCRE库
模块:ngx_http_rewrite_module
加粗 Rwrite相关指令
if (条件) { command } coding.net/u/aminglinux/p/nginx/git/blob/master/rewrite/if.md
break和last coding.net/u/aminglinux/p/nginx/git/blob/master/rewrite/break.md
开启rewrite日志记录:
1.在server加入 rewrite_log on;
2.在nginx.conf 配置error_log logs/nginx_error.log notice; notice会记录rewrite错误信息
配置:
vim nginx/conf/nginx.conf error_log logs/nginx_error.log notice; vim nginx/conf/vhost/1.com_default.conf server{ listen 80; server_name *.1.com 1.com; index index.html 80.html; root /data/t-nginx/1.com; rewrite_log on; rewrite /1.html /2.html ; rewrite /2.html /3.html ; } # curl -x127.0.0.1:80 1.com/1.html 3333
3.当我们请求1.html时,最终访问到的是3.html,两条rewrite规则先后执行。
tail nginx/logs/nginx_error.log 2018/10/22 11:21:00 [notice] 20967#0: *1906 "/1.html" matches "/1.html", client: 127.0.0.1, server: *.1.com, request: "GET HTTP://1.com/1.html HTTP/1.1", host: "1.com" 2018/10/22 11:21:00 [notice] 20967#0: *1906 rewritten data: "/2.html", args: "", client: 127.0.0.1, server: *.1.com, request: "GET HTTP://1.com/1.html HTTP/1.1", host: "1.com" 2018/10/22 11:21:00 [notice] 20967#0: *1906 "/2.html" matches "/2.html", client: 127.0.0.1, server: *.1.com, request: "GET HTTP://1.com/1.html HTTP/1.1", host: "1.com" 2018/10/22 11:21:00 [notice] 20967#0: *1906 rewritten data: "/3.html", args: "", client: 127.0.0.1, server: *.1.com, request: "GET HTTP://1.com/1.html HTTP/1.1", host: "1.com"
日志解释:
以""为分界,左边规则,右边匹配
return 后面跟状态码、URL、text(支持变量)coding.net/u/aminglinux/p/nginx/git/blob/master/rewrite/return.md
反馈字符串可以这样写
return 200 "it's 200";
格式:return 状态码 "字符串";
rewrite规则 coding.net/u/aminglinux/p/nginx/git/blob/master/rewrite/rewrite_ruler.md
rewrite_log定义rewrite日志 rewrite_log on; 写到error_log notice级别
Rwrite相关全局变量
coding.net/u/aminglinux/p/nginx/git/blob/master/rewrite/variable.md
Rwrite实战
coding.net/u/aminglinux/p/nginx/git/blob/master/rewrite/example.md