保障训练-20200922

1.16 nginx全局变量

1.17/1.18 rewrite实战

1.19 nginx的location配置

一、nginx全局变量

KaTeX parse error: Expected 'EOF', got '&' at position 54: …3.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参数对应的值
KaTeX parse error: Expected 'EOF', got '&' at position 62: …3.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等
r e q u e s t f i l e n a m e 当 前 请 求 的 资 源 文 件 的 路 径 名 称 , 相 当 于 是 request_filename 当前请求的资源文件的路径名称,相当于是 requestfilenamedocument_root/$document_uri的组合
r e q u e s t u r i 请 求 的 链 接 , 包 括 request_uri 请求的链接,包括 requesturidocument_uri和$args
$scheme 请求的协议,如ftp,http,https
$server_protocol 客户端请求资源使用的协议的版本,如HTTP/1.0,HTTP/1.1,HTTP/2.0等
$server_addr 服务器IP地址
$server_name 服务器的主机名
$server_port 服务器的端口号
u r i 和 uri 和 uridocument_uri相同
$http_referer 客户端请求时的referer,通俗讲就是该请求是通过哪个链接跳过来的,用curl -e可以指定

二、rewrite实战

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 ( u r i   ∗ ′ j p g ∣ j p e g ∣ g i f ∣ c s s ∣ p n g ∣ j s uri ~* 'jpg|jpeg|gif|css|png|js uri jpgjpeggifcsspngjs')
{
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)$
{
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=KaTeX parse error: Got function '\.' with no arguments as superscript at position 25: … rewrite ^([^\̲.̲]*)/forum-(\w+)… $1/forum.php?mod=forumdisplay&fid=$2&page=KaTeX parse error: Got function '\.' with no arguments as superscript at position 25: … rewrite ^([^\̲.̲]*)/thread-([0-… $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=KaTeX parse error: Got function '\.' with no arguments as superscript at position 25: … rewrite ^([^\̲.̲]*)/group-([0-9… $1/forum.php?mod=group&fid=$2&page=KaTeX parse error: Got function '\.' with no arguments as superscript at position 25: … rewrite ^([^\̲.̲]*)/space-(user… $1/home.php?mod=space&$2=KaTeX parse error: Got function '\.' with no arguments as superscript at position 25: … rewrite ^([^\̲.̲]*)/(fid|tid)-(… $1/index.php?action=$2&value=$3 last;
}

rewrite多个条件的并且

示例8:
location /{
set r u l e 0 ; i f ( rule 0; if ( rule0;if(document_uri !~ ‘^/abc’)
{
set r u l e " rule " rule"{rule}1";
}
if ($http_user_agent ~* ‘ie6|firefox’)
{
set r u l e " rule " rule"{rule}2";
}
if ($rule = “012”)
{
rewrite /(.*) /abc/$1 redirect;
}
}

三、nginx的location配置

安装第三方模块echo-nginx-module

git clone https://github.com/openresty/echo-nginx-module.git

./configure --add-module=/path/to/echo-nginx-module

location语法

location [=|^||~*] /uri/ { …. }

coding.net/u/aminglinux/p/nginx/git/blob/master/location/ruler.md

location优先级及案例

= 高于 ^~ 高于 ~* 等于 ~ 高于 /

coding.net/u/aminglinux/p/nginx/git/blob/master/location/priority.md

nginx location语法规则:location [=||*|^~] /uri/ { … }

nginx的location匹配的变量是$uri

= 表示精确匹配
^~ 表示uri以指定字符或字符串开头
~ 表示区分大小写的正则匹配
~* 表示不区分大小写的正则匹配
/ 通用匹配,任何请求都会匹配到

规则示例

location = “/12.jpg” { … }
如:
www.aminglinux.com/12.jpg 匹配
www.aminglinux.com/abc/12.jpg 不匹配

location ^~ “/abc/” { … }
如:
www.aminglinux.com/abc/123.html 匹配
www.aminglinux.com/a/abc/123.jpg 不匹配

location ~ “png” { … }
如:
www.aminglinux.com/aaa/bbb/ccc/123.png 匹配
www.aminglinux.com/aaa/png/123.html 匹配

location ~* “png” { … }
如:
www.aminglinux.com/aaa/bbb/ccc/123.PNG 匹配
www.aminglinux.com/aaa/png/123.html 匹配

location /admin/ { … }
如:
www.aminglinux.com/admin/aaa/1.php 匹配
www.aminglinux.com/123/admin/1.php 不匹配

小常识

有些资料上介绍location支持不匹配 !~,
如: location !~ ‘png’{ … }
这是错误的,location不支持 !~

如果有这样的需求,可以通过if来实现,
如: if ($uri !~ ‘png’) { … }

注意:location优先级小于if

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值