`
cppmule
  • 浏览: 435506 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类

Nginx的location匹配规则

 
阅读更多


 

 

Nginx的重定向用到了Nginx的HttpRewriteModule,下面简单解释以下如何使用的方法:

rewrite命令

nginx的rewrite相当于apache的rewriterule(大多数情况下可以把原有apache的rewrite规则加上引号就可以直接使用),它可以用在server,location 和IF条件判断块中,命令格式如下:
rewrite 正则表达式 替换目标 flag标记
flag标记可以用以下几种格式:
last – 基本上都用这个Flag。
break – 中止Rewirte,不在继续匹配
redirect – 返回临时重定向的HTTP状态302
permanent – 返回永久重定向的HTTP状态301
例如下面这段设定nginx将某个目录下面的文件重定向到另一个目录,$2对应第二个括号(.*)中对应的字符串:
location /download/ {
rewrite ^(/download/.*)/m/(.*)\..*$ $1/nginx-rewrite/$2.gz break;
}

nginx重定向的IF条件判断

在server和location两种情况下可以使用nginx的IF条件判断,条件可以为以下几种:
正则表达式

如:
匹配判断

~  为区分大小写匹配; !~为区分大小写不匹配
 ~* 为不区分大小写匹配;!~为不区分大小写不匹配
例如下面设定nginx在用户使用ie的使用重定向到/nginx-ie目录下:
if ($http_user_agent ~ MSIE) {
rewrite ^(.*)$ /nginx-ie/$1 break;
}

文件和目录判断
  -f和!-f判断是否存在文件
 -d和!-d判断是否存在目录
 -e和!-e判断是否存在文件或目录
 -x和!-x判断文件是否可执行
例如下面设定nginx在文件和目录不存在的时候重定向:
if (!-e $request_filename) {
proxy_pass http://127.0.0.1;
}

return

返回http代码,例如设置nginx防盗链
location ~* \.(gif|jpg|png|swf|flv)$ {
valid_referers none blocked www.jefflei.com www.leizhenfang.com;
if ($invalid_referer) {
return 404;
}
}

 

 

 





nginx内使用正则表达式(花括号)需要注意的问题
2010年05月24日 星期一 13:42
朋友为对付一些乱七八糟网站的盗链,于是就在nginx配置文件内写了一个正则来禁止那些域名,他写的是这个样子:

if ($http_referer ~* .*([0-9]{2,3}[a-z]{3}\.com|ffkkk\.net|444nnn\.net)) {
return   403;
}

但是用nginx -t来测试nginx配置文件语法的时候报错,提示在[0-9]这里有错。我看了看,正则写的没有问题呀,怀疑是[]的问题,我自己以前没有在ngnix内用过[],就简化了下:

if ($http_referer ~* .*[0-9]\.com) {
return   403;
}

用上面这个配置语法测试通过了。说明[]是可以直接用的,这样试过之后我恍然大悟,确定是{}的问题,因为nginx内{}是用来表示配置段的,直接用肯定有问题。但是在nginx的配置文件内,难道正则就不能用{}了吗?

肯定是可以的,要不然也太弱智了,经过google,终于找到答案:原来如果正则内有用{}的话,只要在正则的两边加上单引号或双引号就行了。

所以写成这样子就通过了:

if ($http_referer ~* "^.*[0-9]{2,3}[a-z]{3}\.com") {
return   403;
}


参考:http://www.nginxcn.com/doc/standard/httprewrite.html

注: 对花括号( { 和 } )来说, 他们既能用在重定向的正则表达式里,也是用在配置文件里分割代码块, 为了避免冲突, 正则表达式里带花括号的话,应该用双引号(或者单引号)包围。比如,要将类似以下的url


/photos/123456

重定向到:

/path/to/photos/12/1234/123456.png

可以用以下方法 (注意双引号):

rewrite  "/photos/([0-9] {2})([0-9] {2})([0-9] {2})" /path/to/photos/$1/$1$2/$1$2$3.png;









nginx 正则

November 12th, 2010 by admin Leave a reply »

location

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

default: no
默认:否

 

context: server
上下文:server

This directive allows different configurations depending on the URI. It can be configured using both conventional strings and regular expressions. To use regular expressions, you must use the prefix ~* for case insensitive match and ~ for case sensitive match.
这个指令随URL不同而接受不同的结构。你可以配置使用常规字符串和正则表达式。如果使用正则表达式,你必须使用 ~* 前缀选择不区分大小写的匹配或者 ~ 选择区分大小写的匹配。

To determine which location directive matches a particular query, the conventional strings are checked first. Conventional strings match the beginning portion of the query and are case-sensitive – the most specific match will be used (see below on how nginx determines this). Afterwards, regular expressions are checked in the order defined in the configuration file. The first regular expression to match the query will stop the search. If no regular expression matches are found, the result from the convention string search is used.
确定 哪个location 指令匹配一个特定指令,常规字符串第一个测试。常规字符串匹配请求的开始部分并且区分大小写,最明确的匹配将会被使用(查看下文明白 nginx 怎么确定它)。然后正则表达式按照配置文件里的顺序测试。找到第一个比配的正则表达式将停止搜索。如果没有找到匹配的正则表达式,使用常规字符串的结果。

There are two ways to modify this behavior. The first is to use the prefix “=”, which matches an exact query only. If the query matches, then searching stops and the request is handled immediately. For example, if the request “/” occurs frequently, then using “location = /” will expedite the processing of this request.
有两个方法修改这个行为。第一个方法是使用 “=”前缀,将只执行严格匹配。如果这个查询匹配,那么将停止搜索并立即处理这个请求。例子:如果经常发生”/”请求,那么使用 “location = /” 将加速处理这个请求。

The second is to use the prefix ^~. This prefix is used with a conventional string and tells nginx to not check regular expressions if the path provided is a match. For instance, “location ^~ /images/” would halt searching if the query begins with /images/ – all regular expression directives would not be checked.
第二个是使用 ^~ 前缀。如果把这个前缀用于一个常规字符串那么告诉nginx 如果路径匹配那么不测试正则表达式。

Furthermore it is important to know that NGINX does the comparison not URL encoded, so if you have a URL like “/images/%20/test” then use “/images/ /test” to determine the location.
而且它重要在于 NGINX 做比较没有 URL 编码,所以如果你有一个 URL 链接’/images/%20/test’ , 那么使用 “images/ /test” 限定location。

To summarize, the order in which directives are checked is as follows:
总结,指令按下列顺序被接受:

1. Directives with the = prefix that match the query exactly. If found, searching stops.
1. = 前缀的指令严格匹配这个查询。如果找到,停止搜索。
2. All remaining directives with conventional strings, longest match first. If this match used the ^~ prefix, searching stops.
2. 剩下的常规字符串,长的在前。如果这个匹配使用 ^~ 前缀,搜索停止。
3. Regular expressions, in order of definition in the configuration file.
3. 正则表达式,按配置文件里的顺序。
4. If #3 yielded a match, that result is used. Else the match from #2 is used.
4. 如果第三步产生匹配,则使用这个结果。否则使用第二步的匹配结果。

Example:
例子:

location = / {
# matches the query / only.
# 只匹配 / 查询。
[ configuration A ]
}
location / {
# matches any query, since all queries begin with /, but regular
# expressions and any longer conventional blocks will be
# matched first.
# 匹配任何查询,因为所有请求都已 / 开头。但是正则表达式规则和长的块规则将被优先和查询匹配。
[ configuration B ]
}
location ^~ /images/ {
# matches any query beginning with /images/ and halts searching,
# so regular expressions will not be checked.
# 匹配任何已 /images/ 开头的任何查询并且停止搜索。任何正则表达式将不会被测试。
[ configuration C ]
}
location ~* “.(gif|jpg|jpeg)$ {
# matches any request ending in gif, jpg, or jpeg. However, all
# requests to the /images/ directory will be handled by
# Configuration C.
# 匹配任何已 gif、jpg 或 jpeg 结尾的请求。然而所有 /images/ 目录的请求将使用 Configuration C。
[ configuration D ]
}

Example requests:
例子请求:

*

/ -> configuration A
*

/documents/document.html -> configuration B
*

/images/1.gif -> configuration C
*

/documents/1.jpg -> configuration D

Note that you could define these 4 configurations in any order and the results would remain the same.
注意:按任意顺序定义这4个配置结果将仍然一样。

一、介绍Nginx是俄罗斯人编写的十分轻量级的HTTP服务器,Nginx,它的发音为“engine X”, 是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/POP3/SMTP 代理服务器.
二、Location语法语法:location [=|~|~*|^~] /uri/ { … }
注:
1、~   为区分大小写匹配
2、~* 为不区分大小写匹配
3、!~和!~*分别为区分大小写不匹配及不区分大小写
不匹配
示例一:
location  / {
 }
匹配任何查询,因为所有请求都以 / 开头。但是正则表达式规则将被优先和查询匹配。
示例二:
location =/ {}
仅仅匹配/

示例三:
location ~* \.(gif|jpg|jpeg)$ {
rewrite \.(gif|jpg)$ /logo.png;

注:不区分大小写匹配任何以gif,jpg,jpeg结尾的文件

三、ReWrite语法
last – 基本上都用这个Flag。
break – 中止Rewirte,不在继续匹配
redirect – 返回临时重定向的HTTP状态302
permanent – 返回永久重定向的HTTP状态301

1、下面是可以用来判断的表达式:
-f和!-f用来判断是否存在文件
-d和!-d用来判断是否存在目录
-e和!-e用来判断是否存在文件或目录
-x和!-x用来判断文件是否可执行
2、下面是可以用作判断的全局变量
例:http://localhost:88/test1/test2/test.php
$host:localhost
$server_port:88
$request_uri:
http://localhost:88/test1/test2/test.php
$document_uri:/test1/test2/test.php
$document_root:D:\nginx/html
$request_filename:D:\nginx/html/test1/test2/test.php

四、Redirect语法
server {
listen 80;
server_name start.igrow.cn;
index index.html index.php;
root html;
if ($http_host !~ “^star\.igrow\.cn

正则表达式匹配,其中:

  1. * ~ 为区分大小写匹配
  2. * ~* 为不区分大小写匹配
  3. * !~和!~*分别为区分大小写不匹配及不区分大小写不匹配

文件及目录匹配,其中:

  1. * -f和!-f用来判断是否存在文件
  2. * -d和!-d用来判断是否存在目录
  3. * -e和!-e用来判断是否存在文件或目录
  4. * -x和!-x用来判断文件是否可执行

flag标记有:

  1. * last 相当于Apache里的[L]标记,表示完成rewrite
  2. * break 终止匹配, 不再匹配后面的规则
  3. * redirect 返回302临时重定向 地址栏会显示跳转后的地址
  4. * permanent 返回301永久重定向 地址栏会显示跳转后的地址

一些可用的全局变量有,可以用做条件判断(待补全)

  1. $args
  2. $content_length
  3. $content_type
  4. $document_root
  5. $document_uri
  6. $host
  7. $http_user_agent
  8. $http_cookie
  9. $limit_rate
  10. $request_body_file
  11. $request_method
  12. $remote_addr
  13. $remote_port
  14. $remote_user
  15. $request_filename
  16. $request_uri
  17. $query_string
  18. $scheme
  19. $server_protocol
  20. $server_addr
  21. $server_name
  22. $server_port
  23. $uri

结合QeePHP的例子

  1. if (!-d $request_filename) {
  2. rewrite ^/([a-z-A-Z]+)/([a-z-A-Z]+)/?(.*)$ /index.php?namespace=user&controller=$1&action=$2&$3 last;
  3. rewrite ^/([a-z-A-Z]+)/?$ /index.php?namespace=user&controller=$1 last;
  4. break;

多目录转成参数
abc.domian.com/sort/2 => abc.domian.com/index.php?act=sort&name=abc&id=2

  1. if ($host ~* (.*)\.domain\.com) {
  2. set $sub_name $1;
  3. rewrite ^/sort\/(\d+)\/?$ /index.php?act=sort&cid=$sub_name&id=$1 last;
  4. }

目录对换
/123456/xxxx -> /xxxx?id=123456

  1. rewrite ^/(\d+)/(.+)/ /$2?id=$1 last;

例如下面设定nginx在用户使用ie的使用重定向到/nginx-ie目录下:

  1. if ($http_user_agent ~ MSIE) {
  2. rewrite ^(.*)$ /nginx-ie/$1 break;
  3. }

目录自动加“/”

  1. if (-d $request_filename){
  2. rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
  3. }

禁止htaccess

  1. location ~/\.ht {
  2. deny all;
  3. }

禁止多个目录

  1. location ~ ^/(cron|templates)/ {
  2. deny all;
  3. break;
  4. }

禁止以/data开头的文件
可以禁止/data/下多级目录下.log.txt等请求;

  1. location ~ ^/data {
  2. deny all;
  3. }

禁止单个目录
不能禁止.log.txt能请求

  1. location /searchword/cron/ {
  2. deny all;
  3. }

禁止单个文件

  1. location ~ /data/sql/data.sql {
  2. deny all;
  3. }

给favicon.ico和robots.txt设置过期时间;
这里为favicon.ico为99天,robots.txt为7天并不记录404错误日志

  1. location ~(favicon.ico) {
  2. log_not_found off;
  3. expires 99d;
  4. break;
  5. }

  6. location ~(robots.txt) {
  7. log_not_found off;
  8. expires 7d;
  9. break;
  10. }

设定某个文件的过期时间;这里为600秒,并不记录访问日志

  1. location ^~ /html/scripts/loadhead_1.js {
  2. access_log off;
  3. root /opt/lampp/htdocs/web;
  4. expires 600;
  5. break;
  6. }

文件反盗链并设置过期时间
这里的return 412 为自定义的http状态码,默认为403,方便找出正确的盗链的请求
“rewrite ^/ http://leech.c1gstudio.com/leech.gif;”显示一张防盗链图片
“access_log off;”不记录访问日志,减轻压力
“expires 3d”所有文件3天的浏览器缓存

  1. location ~* ^.+\.(jpg|jpeg|gif|png|swf|rar|zip|css|js)$ {
  2. valid_referers none blocked *.c1gstudio.com *.c1gstudio.net localhost 208.97.167.194;
  3. if ($invalid_referer) {
  4. rewrite ^/ http://leech.c1gstudio.com/leech.gif;
  5. return 412;
  6. break;
  7. }
  8. access_log off;
  9. root /opt/lampp/htdocs/web;
  10. expires 3d;
  11. break;
  12. }

只充许固定ip访问网站,并加上密码

  1. root /opt/htdocs/www;
  2. allow 208.97.167.194;
  3. allow 222.33.1.2;
  4. allow 231.152.49.4;
  5. deny all;
  6. auth_basic “C1G_ADMIN”;
  7. auth_basic_user_file htpasswd;

将多级目录下的文件转成一个文件,增强seo效果
/job-123-456-789.html 指向/job/123/456/789.html

  1. rewrite ^/job-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /job/$1/$2/jobshow_$3.html last;

将根目录下某个文件夹指向2级目录
如/shanghaijob/ 指向 /area/shanghai/
如果你将last改成permanent,那么浏览器地址栏显是/location/shanghai/

  1. rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;

上面例子有个问题是访问/shanghai 时将不会匹配

  1. rewrite ^/([0-9a-z]+)job$ /area/$1/ last;
  2. rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;

这样/shanghai 也可以访问了,但页面中的相对链接无法使用,
如./list_1.html真实地址是/area/shanghia/list_1.html会变成/list_1.html,导至无法访问。

那我加上自动跳转也是不行咯
(-d $request_filename)它有个条件是必需为真实目录,而我的rewrite不是的,所以没有效果

  1. if (-d $request_filename){
  2. rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
  3. }

知道原因后就好办了,让我手动跳转吧

  1. rewrite ^/([0-9a-z]+)job$ /$1job/ permanent;
  2. rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;

文件和目录不存在的时候重定向:

  1. if (!-e $request_filename) {
  2. proxy_pass http://127.0.0.1;
  3. }

域名跳转

  1. server
  2. {
  3. listen 80;
  4. server_name jump.c1gstudio.com;
  5. index index.html index.htm index.php;
  6. root /opt/lampp/htdocs/www;
  7. rewrite ^/ http://www.c1gstudio.com/;
  8. access_log off;
  9. }

多域名转向

  1. server_name www.c1gstudio.com www.c1gstudio.net;
  2. index index.html index.htm index.php;
  3. root /opt/lampp/htdocs;
  4. if ($host ~ “c1gstudio\.net”) {
  5. rewrite ^(.*) http://www.c1gstudio.com$1 permanent;
  6. }

三级域名跳转

  1. if ($http_host ~* “^(.*)\.i\.c1gstudio\.com$”) {
  2. rewrite ^(.*) http://top.yingjiesheng.com$1;
  3. break;
  4. }

域名镜向

  1. server
  2. {
  3. listen 80;
  4. server_name mirror.c1gstudio.com;
  5. index index.html index.htm index.php;
  6. root /opt/lampp/htdocs/www;
  7. rewrite ^/(.*) http://www.c1gstudio.com/$1 last;
  8. access_log off;
  9. }

某个子目录作镜向

  1. location ^~ /zhaopinhui {
  2. rewrite ^.+ http://zph.c1gstudio.com/ last;
  3. break;
  4. }

discuz ucenter home (uchome) rewrite

  1. rewrite ^/(space|network)-(.+)\.html$ /$1.php?rewrite=$2 last;
  2. rewrite ^/(space|network)\.html$ /$1.php last;
  3. rewrite ^/([0-9]+)$ /space.php?uid=$1 last;

discuz 7 rewrite

  1. rewrite ^(.*)/archiver/((fid|tid)-[\w\-]+\.html)$ $1/archiver/index.php?$2 last;
  2. rewrite ^(.*)/forum-([0-9]+)-([0-9]+)\.html$ $1/forumdisplay.php?fid=$2&page=$3 last;
  3. rewrite ^(.*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/viewthread.php?tid=$2&extra=page\%3D$4&page=$3 last;
  4. rewrite ^(.*)/profile-(username|uid)-(.+)\.html$ $1/viewpro.php?$2=$3 last;
  5. rewrite ^(.*)/space-(username|uid)-(.+)\.html$ $1/space.php?$2=$3 last;
  6. rewrite ^(.*)/tag-(.+)\.html$ $1/tag.php?name=$2 last;

给discuz某版块单独配置域名

  1. server_name bbs.c1gstudio.com news.c1gstudio.com;

  2. location = / {
  3. if ($http_host ~ news\.c1gstudio.com$) {
  4. rewrite ^.+ http://news.c1gstudio.com/forum-831-1.html last;
  5. break;
  6. }
  7. }

discuz ucenter 头像 rewrite 优化

  1. location ^~ /ucenter {
  2. location ~ .*\.php?$
  3. {
  4. #fastcgi_pass unix:/tmp/php-cgi.sock;
  5. fastcgi_pass 127.0.0.1:9000;
  6. fastcgi_index index.php;
  7. include fcgi.conf;
  8. }

  9. location /ucenter/data/avatar {
  10. log_not_found off;
  11. access_log off;
  12. location ~ /(.*)_big\.jpg$ {
  13. error_page 404 /ucenter/images/noavatar_big.gif;
  14. }
  15. location ~ /(.*)_middle\.jpg$ {
  16. error_page 404 /ucenter/images/noavatar_middle.gif;
  17. }
  18. location ~ /(.*)_small\.jpg$ {
  19. error_page 404 /ucenter/images/noavatar_small.gif;
  20. }
  21. expires 300;
  22. break;
  23. }
  24. }

jspace rewrite

  1. location ~ .*\.php?$
  2. {
  3. #fastcgi_pass unix:/tmp/php-cgi.sock;
  4. fastcgi_pass 127.0.0.1:9000;
  5. fastcgi_index index.php;
  6. include fcgi.conf;
  7. }

  8. location ~* ^/index.php/
  9. {
  10. rewrite ^/index.php/(.*) /index.php?$1 break;
  11. fastcgi_pass 127.0.0.1:9000;
  12. fastcgi_index index.php;
  13. include fcgi.conf;
  14. }

amp;quot  {
rewrite ^(.*)
 http://star.igrow.cn$1 redirect;
}
}

五、防盗链location ~* \.(gif|jpg|swf)$ {
valid_referers none blocked start.igrow.cn sta.igrow.cn;
if ($invalid_referer) {
rewrite ^/
 http://$host/logo.png;
}
}

六、根据文件类型设置过期时间
location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
if (-f $request_filename) {
expires    1h;
break;
}
}

七、禁止访问某个目录
location ~* \.(txt|doc)${
root /data/www/wwwroot/linuxtone/test;
deny all;
}

XX

分享到:
评论

相关推荐

    nginx location匹配实例详解

    Nginx配置指令location匹配符优先级和安全问题详解Nginx location 匹配规则Nginx服务器的location指令匹配规则详解利用nginx如何匹配多个条件Nginx location匹配规则的方法示例简介Nginx中的location匹配规则nginx ...

    Nginx location匹配规则的方法示例

    主要介绍了Nginx location匹配规则的方法示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    详解Nginx location 匹配规则

    语法规则 location [=|~|~*|^~] /uri/ { … } 模式 含义 ...location = /uri = 表示精确匹配,只有完全匹配上才能生效 ...location ^~ /uri ...通用匹配,任何未匹配到其它location的请求都会匹配到,相当于swit

    nginx的server和location匹配规则

    nginx的server和location匹配规则

    Nginx关于location的匹配规则详解.docx

    NULL 博文链接:https://eyesmore.iteye.com/blog/1141660

    简介Nginx中的location匹配规则

    location匹配命令 ~ #波浪线表示执行一个正则匹配,区分大小写 ~* #表示执行一个正则匹配,不区分大小写 ^~ #^~表示普通字符匹配,如果该选项匹配,只匹配该选项,不匹配别的选项,一般用来匹配目录 = #进行普通...

    Nginx服务器的location指令匹配规则详解

    Nginx 中的 Location 指令 是NginxHttpCoreModule中重要指令。Location 指令,是用来为匹配的 URI 进行配置,URI 即语法中的”/uri/”,可以是字符串或正则表达式。但如果要使用正则表达式,则必须指定前缀。 nginx ...

    一文弄懂Nginx的location匹配的实现

    之前对 location 的匹配规则是一知半解的。为了搞明白 location 是如何匹配的,特意花了点时间查了些资料,总结此文。希望能给大家带来帮助。 语法规则 location [ = | ~ | ~* | ^~ ] uri { ... } location @name ...

    nginx 配置location匹配规则实例讲解

    nginx的配置指令的作用域可以分为 main,server,location这3个种,实际上这3者不是依次包含的关系,而是相互独立的关系,比如一个只具有main级别作用域的指令,是不能写在某个server或者location内的,模块的某个...

    Nginx配置之location的匹配优先级浅析

    主要给大家介绍了关于Nginx配置之location的匹配优先级的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用Nginx具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧

    Nginx Location指令URI匹配规则详解小结

    1、介绍 location指令是http模块当中最核心的一项配置,根据预先定义的URL匹配规则来接收用户发送的请求,根据匹配结果,将请求转发到后台服务器、非法的请求直接拒绝并返回403、404、500...4、location URI匹配规则

    nginx location 配置 正则表达式实例详解

    1.location 介绍 •location 是在 server 块中配置,用来通过匹配接收的uri来实现...2.localtion 匹配规则 •location [ = | ~ | ~* | ^~ ] uri { … } •location @name { … } 注1:规则不能混合使用  注2:以下例

    Nginx服务器中的location配置详解

    location匹配测试只使用请求URI的部分,而不使用参数部分。(原因:参数的写法太多,无法精确匹配) location匹配顺序 多个location配置的前提下,location的匹配顺序(未验证,嘿嘿,google上搜的) 1.首先匹\u914d...

    Nginx正则表达式相关的参数和规则介绍

    最近帮客户配置服务器,经常修改Nginx的配置文件,频繁的用到正式匹配规则,这里整理了一些常用的正则参数及规则,以备查询。 Nginx配置中Location的语法规则 location [ = | ~ | ~* | ^~ | !~ | !~* ] /uri/{ … } ...

    如何利用nginx通过正则拦截指定url请求详解

    匹配规则 location / { if ($request_uri ~* ^/\?http(.*)$) { return 404; } } 经过这样的匹配,我们就可以拦截所有请求根目录的网址并且参数为?httpxxx类似的请求都会显示404. 防盗链 返回http代码,例如设置...

Global site tag (gtag.js) - Google Analytics