![dc60c2e375fc267a9a44e3f142b14501.png](https://i-blog.csdnimg.cn/blog_migrate/c46b35dd6f89c43a31e926fae8444ea5.jpeg)
--nginx全局变量、rewrite实战、nginx的location配置
一、nginx全局变量
nginx 主配置文件中的log_format,常用全局变量:
https://github.com/aminglinux/nginx/blob/master/rewrite/variable.md
![e47728851e57a5f7124eaf1620c455b6.png](https://i-blog.csdnimg.cn/blog_migrate/0b17377842aaf3895c4f00e830682575.jpeg)
变量
说明
$args
请求中的参数,如www.123.com/1.php?a=1&b=2的$args就是a=1&b=2
$body_bytes_sent
服务器发送给客户端的响应body字节数
$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,不包含后面的参数
$http_referer
记录此次请求是从哪个连接访问过来的,可以根据该参数进行防盗链设置
$host
主机头,也就是域名
$http_user_agent
客户端的详细信息,也就是浏览器的标识,用curl -A可以指定
$http_cookie
客户端的cookie信息
$http_x_forwarded_for
当前端有代理服务器时,设置web节点记录客户端地址的配置,此参数生效的前提是代理服务器也要进行相关的x_forwarded_for设置
$limit_rate
如果nginx服务器使用limit_rate配置了显示网络速率,则会显示,如果没有设置, 则显示0
$remote_addr
客户端的公网ip
$remote_port
客户端的port
$remote_user
如果nginx有配置认证,该变量代表客户端认证的用户名
$request
请求的URI和HTTP协议,如“GET /article-10000.html HTTP/1.1”
$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
服务器的端口号
$status
http状态码,记录请求返回的状态码,例如:200、301、404等
$uri
和$document_uri相同
$http_referer
客户端请求时的referer,通俗讲就是该请求是通过哪个链接跳过来的,用curl -e可以指定
$time_local
记录访问时间与时区,如18/Jul/2014:17:00:01 +0800
二、rewrite实战
域名跳转(域名重定向)
示例1(不带条件的):
server{ listen 80; server_name www.a.com; root /data/wwwroot/www.a.com; index index.html; rewrite /(.*) http://www.b.com/$1 permanent;}
访问a.com跳转到b.com
![aa20356cdff69d9131d53455002224e6.png](https://i-blog.csdnimg.cn/blog_migrate/6e250aef733d3e38965016f2de087907.jpeg)
示例2(带条件的):
server{ listen 80; server_name www.a.com a.com; root /data/wwwroot/www.a.com; index index.html; if ($host != 'www.a.com') { rewrite /(.*) http://www.a.com/$1 permanent; }}
通过判断条件,如果$host不等于www.a.com的,跳转到www.a.com
![d2a8288803f903f069a42ea811713fbf.png](https://i-blog.csdnimg.cn/blog_migrate/b4305f644a8f19c2137d356b8fa05d8f.jpeg)
示例3(http跳转到https):
server{ listen 80; server_name www.a.com; root /data/wwwroot/www.a.com; index index.html; rewrite /(.*) https://www.a.com/$1 permanent;}
![10d1198ac8d98f71367b2cb75b5d1cdb.png](https://i-blog.csdnimg.cn/blog_migrate/29cae91c3b2bd072b94e28f849a6468a.jpeg)
示例4(域名访问二级目录)
server{ listen 80; server_name blog.a.com; index index.html; rewrite /(.*) http://www.a.com/blog/$1 permanent;}
![7bf5b21d0101f647a5676324df1edad1.png](https://i-blog.csdnimg.cn/blog_migrate/4f848fe8776923ea5c3b9192faa43540.jpeg)
示例5(静态请求分离)
server{ listen 80; server_name www.a.com; location ~* ^.+.(jpg|jpeg|gif|css|png|js)$ { rewrite /(.*) http://img.a.com/$1 permanent; } }或者:server{ listen 80; server_name www.a.com; index index.html; if ( $uri ~* 'jpg|jpeg|gif|css|png|js$') { rewrite /(.*) http://img.a.com/$1 permanent; }}
![8a85b469f4a33b1f24fcaad1797631bb.png](https://i-blog.csdnimg.cn/blog_migrate/72ac5db7e108725f00ace59f64bb5733.jpeg)
防盗链
示例6
server{ listen 80; server_name www.a.com; location ~* ^.+.(jpg|jpeg|gif|css|png|js|rar|zip|flv)$ { valid_referers none blocked server_names *.a.com a.com *.tobe.com tobe.com; if ($invalid_referer) { rewrite /(.*) http://img.a.com/images/forbidden.png; } } ....... }
说明:这里是通配,跟正则里面的不是一个意思,valid_referers定义白名单,none指的是referer不存在、为空的情况(curl -e 测试), blocked指的是referer头部的值被防火墙或者代理服务器删除或者伪装的情况,
该情况下,referer头部的值不以http://或者https://开头(curl -e 后面跟的referer不以http://或者https://开头)。
或者:
location ~* ^.+.(jpg|jpeg|gif|css|png|js|rar|zip|flv)$ { valid_referers none blocked server_names *.a.com *.tobe.com a.com tobe.com; if ($invalid_referer) { return 403; } }
伪静态
示例7(discuz伪静态):
location / { rewrite ^([^.]*)/topic-(.+).html$ $1/portal.php?mod=topic&topic=$2 last; rewrite ^([^.]*)/forum-(w+)-([0-9]+).html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last; rewrite ^([^.]*)/thread-([0-9]+)-([0-9]+)-([0-9]+).html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3 last; rewrite ^([^.]*)/group-([0-9]+)-([0-9]+).html$ $1/forum.php?mod=group&fid=$2&page=$3 last; rewrite ^([^.]*)/space-(username|uid)-(.+).html$ $1/home.php?mod=space&$2=$3 last; rewrite ^([^.]*)/(fid|tid)-([0-9]+).html$ $1/index.php?action=$2&value=$3 last;}
rewrite多个条件的并且
示例8:
location /{ set $rule 0; #值为0 if ($document_uri !~ '^/abc') #!~不匹配以abc开头的 { set $rule "${rule}1"; #01 } if ($http_user_agent ~* 'ie6|firefox') { set $rule "${rule}2"; #012 } if ($rule = "012") { rewrite /(.*) /abc/$1 redirect; }}
当满足两个条件不以abc开头,并且http_user_agent为ie6或firefox时,跳转到http://www.a.com/aaa/,不满足其中的一个条件时,返回了404
![6d03437439aa0779e8b29b32da99c152.png](https://i-blog.csdnimg.cn/blog_migrate/b7950fad7092478b7fdf39cfb229caa9.jpeg)
三、nginx的location配置
安装第三方模块echo-nginx-module
cd /usr/local/src/git clone https://github.com/openresty/echo-nginx-module.git
nginx编译安装后的操作:
cd /usr/local/src/nginx-1.16.1
重新编译
./configure --prefix=/usr/local/nginx --add-module=/usr/local/src/echo-nginx-modulemake && make install
/usr/local/nginx/sbin/nginx -V
测试中使用,可以在虚拟主机配置文件中直接使用,如:echo 123;
![18ad1f9a2ee1a0e23cff166420287ce4.png](https://i-blog.csdnimg.cn/blog_migrate/380a3e427de39941906fc819d1453be2.jpeg)
location语法:
nginx location语法规则:location [=|~|~*|^~] /uri/ { … }nginx的location匹配的变量是$uri
符号说明=表示精确匹配^~表示uri以指定字符或字符串开头~表示区分大小写的正则匹配~*表示不区分大小写的正则匹配/通用匹配,任何请求都会匹配到
规则优先级:
= 高于 ^~ 高于 ~* 等于 ~ 高于 /
规则示例:
location = "/12.jpg" { ... }如:www.a.com/12.jpg 匹配www.a.com/abc/12.jpg 不匹配location ^~ "/abc/" { ... }如:www.a.com/abc/123.html 匹配www.a.com/a/abc/123.jpg 不匹配location ~ "png" { ... }如:www.a.com/aaa/bbb/ccc/123.png 匹配www.a.com/aaa/png/123.html 匹配location ~* "png" { ... }如:www.a.com/aaa/bbb/ccc/123.PNG 匹配www.a.com/aaa/png/123.html 匹配location /admin/ { ... }如:www.a.com/admin/aaa/1.php 匹配www.a.com/123/admin/1.php 不匹配
小常识:
有些资料上介绍location支持不匹配 !~,如: location !~ 'png'{ ... }这是错误的,location不支持 !~如果有这样的需求,可以通过if来实现,如: if ($uri !~ 'png') { ... }注意:location优先级小于if