nginx 正则 结尾 配置_Nginx 正则匹配

Nginx 正则表达式之匹配操作符

~ 区分大小写(大小写敏感)匹配成功

~* 不区分大小写匹配成功

!~ 区分大小写匹配失败

!~* 不区分大小写匹配失败

^ 以什么开头的匹配

$ 以什么结尾的匹配

* 代表任意字符

过期缓存

expires 30d;

表示过期时间30天

针对浏览器

location / {

if ($http_user_agent ~* chrome) {

return 503;

}

}

禁止访问Chrome浏览器。

针对文件类型

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$

{

expires 30d;

}

针对文件夹

location /test/ {

return 403;

}

判断文件,文件夹

-f和!-f用来判断是否存在文件

-d和!-d用来判断是否存在目录

-e和!-e用来判断是否存在文件或目录

-x和!-x用来判断文件是否可执行

设置某些类型文件的浏览器缓存时间

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$

{

expires 30d;

}

location ~ .*\.(js|css)$

{

expires 1h;

}

匹配到所有uri

location / {

}

last一般写在server和if中,而break一般使用在location中。

redirect : 返回302临时重定向,地址栏会显示跳转后的地址。

全局变量

$args 请求中的参数,如www.abc.com/test/hello?a=1&b=2的$args就是a=1&b=2

$document_root Nginx虚拟主机配置文件中的root参数对应的值

$document_uri 当前请求中不包含指令的URI,如www.abc.com/test/hello?a=1&b=2的document_uri就是/test/hello,不包含后面的参数

$host 主机头,也就是域名

$http_user_agent 客户端的详细信息,也就是浏览器的标识,用curl -A可以指定

$http_cookie 客户端的cookie信息

$limit_rate 如果Nginx服务器使用limit_rate配置了显示网络速率,则会显示,如果没有设置,则显示0

$remote_addr 客户端的公网IP

$remote_port 客户端的端口

$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,通俗讲就是该请求是通过哪个链接跳过来的

常用:$http_referer

$request_uri

$http_user_agent

案例

args:name=zhangsan&age=15

document_root:/home/wwwroot/default/wounion/dragonfly/public

document_uri:/test/hello

host:jiqing.wounion.com

http_user_agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36

http_cookie:PHPSESSID=3ire1fr9bl3qdpd787pv0qb7a5

limit_rate:0

remote_addr:127.0.0.1

remote_port:43180

request_method:GET

request_filename:/home/wwwroot/default/wounion/dragonfly/public/test/hello

request_uri:/test/hello?name=zhangsan&age=15

scheme:http

server_protocol:HTTP/1.1

server_addr:127.0.0.1

server_name:jiqing.wounion.com

server_port:80

uri:/test/hello

http_referer:

常用正则

. : 匹配除换行符以外的任意字符

? : 重复0次或1次

+ : 重复1次或更多次

* : 重复0次或更多次

\d :匹配数字

^ : 匹配字符串的开始

$ : 匹配字符串的介绍

{n} : 重复n次

{n,} : 重复n次或更多次

[c] : 匹配单个字符c

[a-z] : 匹配a-z小写字母的任意一个

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值