10.12-10.15 rewrite配置if,break和last的用法,rewrite规则,Nginx全局变量php
Nginx的Rwrite配置html
加粗 域名跳转(重定向)、URL重写(伪静态)、动静分离(跳转域名,并接入CDN实现加速)linux
依赖PCRE库nginx
模块:ngx_http_rewrite_modulegit
加粗 Rwrite相关指令正则表达式
if (条件) { command } https://coding.net/u/aminglinux/p/nginx/git/blob/master/rewrite/if.md vim
break和last https://coding.net/u/aminglinux/p/nginx/git/blob/master/rewrite/break.md 后端
开启rewrite日志记录:浏览器
1.在server加入 rewrite_log on;bash
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(支持变量)https://coding.net/u/aminglinux/p/nginx/git/blob/master/rewrite/return.md
反馈字符串能够这样写
return 200 "it's 200";
格式:return 状态码 "字符串";
rewrite规则 https://coding.net/u/aminglinux/p/nginx/git/blob/master/rewrite/rewrite_ruler.md
* regex是用于匹配URI的正则表达式(/(.*)这种),其不会匹配到$host(域名)
rewrite_log定义rewrite日志 rewrite_log on; 写到error_log notice级别
Rwrite相关全局变量
https://coding.net/u/aminglinux/p/nginx/git/blob/master/rewrite/variable.md
变量 | 说明 |
---|---|
$args | 请求中的参数,如www.123.com/1.php?a=1&b=2的$args就是a=1&b=2 |
$content_length | HTTP请求信息里的”Content-Length” |
$conten_type | HTTP请求信息里的”Content-Type” |
$document_root | nginx虚拟主机配置文件中的root参数对应的值 |
$document_uri | 当前请求中不包含指令的URI,如www.123.com/1.php?a=1&b=2的$document_uri就是1.php,不包含后面的参数 |
$host | 主机头,也就是域名 |
$http_user_agent | 客户端的详细信息,也就是浏览器的标识,用curl -A能够指定 |
$http_cookie | 客户端的cookie信息 |
$limit_rate | 若是nginx服务器使用limit_rate配置了显示网络速率,则会显示,若是没有设置, 则显示0 |
$remote_addr | 客户端的公网ip |
$remote_port | 客户端的port |
$remote_user | 若是nginx有配置认证,该变量表明客户端认证的用户名 |
$request_body_file | 作反向代理时发给后端服务器的本地资源的名称 |
$request_method | 请求资源的方式,GET/PUT/DELETE等 |
$request_filename | 当前请求的资源文件的路径名称,至关因而$document_root/$document_uri的组合 |
$request_uri | 请求的连接,包括$document_uri和$args |
$scheme | 请求的协议,如ftp,http,https |
$server_protocol | 客户端请求资源使用的协议的版本,如HTTP/1.0,HTTP/1.1,HTTP/2.0等 |
$server_addr | 服务器IP地址 |
$server_name | 服务器的主机名 |
$server_port | 服务器的端口号 |
$uri | 和$document_uri相同 |
$http_referer | 客户端请求时的referer,通俗讲就是该请求是经过哪一个连接跳过来的,用curl -e能够指定 |
return 200 能够搭配全局变量
格式:
return 200 "全局变量";
例如:
return 200 "$args";
配置参考以下:
server { listen 80; server_name 2.com; root /data/t-nginx/2.com; return 200 "$args"; }
[root@AliKvn vhost]# curl -x127.0.0.1:80 '2.com/2.php?1=a&2=b' 1=a&2=b[root@AliKvn vhost]#
其中1=a&2=b,就是"$args"的变量值了, 其余变量也能够用这钟方法取值
例如:
return 200 "$document_uri";
server { listen 80; server_name 2.com; root /data/t-nginx/2.com; return 200 "$document_uri"; } nginx -s reload [root@AliKvn vhost]# !curl curl -x127.0.0.1:80 '2.com/2.php?1=a&2=b' /2.php[root@AliKvn vhost]#
那么2.php就是$document_uri了
以上变量,能够组合在一块儿。