10.12-10.16 rewrite配置if,break和last的用法,规则,nginx全局变量

10.12-10.15 rewrite配置if,break和last的用法,rewrite规则,Nginx全局变量


Nginx的Rwrite配置


加粗 域名跳转(重定向)、URL重写(伪静态)、动静分离(跳转域名,并接入CDN实现加速)

  • 依赖PCRE库

  • 模块:ngx_http_rewrite_module

加粗 Rwrite相关指令

开启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(支持变量)https://coding.net/u/aminglinux/p/nginx/git/blob/master/rewrite/return.md 

反馈字符串可以这样写


return  200 "it's 200";

格式:return 状态码 "字符串";


Rwrite相关全局变量

https://coding.net/u/aminglinux/p/nginx/git/blob/master/rewrite/variable.md 

nginx 常用全局变量

变量说明
$args请求中的参数,如www.123.com/1.php?a=1&b=2的$args就是a=1&b=2
$content_lengthHTTP请求信息里的”Content-Length”
$conten_typeHTTP请求信息里的”Content-Type”
$document_rootnginx虚拟主机配置文件中的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了


以上变量,可以组合在一起。




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章