nginx全局变量及rewrite实战
admin
2023-02-23 08:40:04
0次

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

nginx 常用全局变量
变量 说明
$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可以指定 #常用

Rewrite实战

本部分内容为nginx生产环境中使用的场景示例。

域名跳转(域名重定向)

示例1(不带条件的):
server{
listen 80;
server_name www.aminglinux.com;
rewrite /(.*) http://www.aming.com/$1 permanent;
.......

}

示例2(带条件的):
server{
listen 80;
server_name www.aminglinux.com aminglinux.com;
if ($host != 'www.aminglinux.com')
{
rewrite /(.*) http://www.aminglinux.com/$1 permanent;
}
.......

}
示例3(http跳转到https):
server{
listen 80;
server_name www.aminglinux.com;
rewrite /(.*) https://www.aminglinux.com/$1 permanent;
.......

}
示例4(域名访问二级目录)
server{
listen 80;
server_name bbs.aminglinux.com;
rewrite /(.*) http://www.aminglinux.com/bbs/$1 last;
.......

}
示例5(静态请求分离)
server{
listen 80;
server_name www.aminglinux.com;
location ~ ^.+.(jpg|jpeg|gif|css|png|js)$
{
rewrite /(.
) http://img.aminglinux.com/$1 permanent;
}

.......

}
或者:
server{
listen 80;
server_name www.aminglinux.com;
if ( $uri ~ 'jpg|jpeg|gif|css|png|js$')
{
rewrite /(.
) http://img.aminglinux.com/$1 permanent;
}

.......

}

防盗链

示例6
server{
listen 80;
server_name www.aminglinux.com;
location ~ ^.+.(jpg|jpeg|gif|css|png|js|rar|zip|flv)$
{
#白名单 空 blocked 域名
valid_referers none blocked server_names
.aminglinux.com aminglinux.com .aming.com aming.com;
if ($invalid_referer) #黑名单
{
rewrite /(.
) http://img.aminglinux.com/images/forbidden.png;
}
}

.......

}
说明:这里是通配,跟正则里面的不是一个意思,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
.aminglinux.com *.aming.com aminglinux.com aming.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; #定义一个变量
if ($document_uri !~ '^/abc')
{
set $rule "${rule}1"; # 01
}
if ($http_user_agent ~ 'ie6|firefox')
{
set $rule "${rule}2"; #02
}
if ($rule = "012")
{
rewrite /(.
) /abc/$1 redirect;
}
}

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...