我正在使用nginx-1.6.2,2 ,我想返回匹配/browse.php?u=http URL的error 410 ,所以像这样的请求将得到410:
162.218.208.16 - - [21/Nov/2014:12:35:40 -0500] "GET /browse.php?u=http%3A%2F%2Fwww.bing.com%2Fsearch%3Fq%3DBroke%2C%2BUSA%2Bfiletype%3Apdf%26first%3D0%26count%3D20%26format%3Drss&b=156&f=norefer HTTP/1.1" 403 570 "http://ww.thespacesurf.com/browse.php?u=http%3A%2F%2Fwww.bing.com%2Fsearch%3Fq%3DBroke%2C%2BUSA%2Bfiletype%3Apdf%26first%3D0%26count%3D20%26format%3Drss&b=156&f=norefer" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)"
我真的不需要regex ,所以像这样的东西,但它不工作:
location = /browse.php?u=http { return 410; }
几天后,我在/var/log/nginx-access.loginput了grep -c
$ bzip2 -cd /var/log/nginx-access.log.0.bz2 | grep -c ' 410 ' 5665 $
这让我感到温暖和模糊)再次感谢!
这是因为location是/browse.php ,而不是你指定的那个。
有几种方法可以做到这一点。 例如,像这样的事情应该这样做:
location = /browse.php { if ($query_string ~ ^u=http) { return 410; } }
PS:或者使用~*代替~如果u=http不区分大小写。
你不能在位置声明的声明中匹配查询string,所以你需要这样的东西:
location = /browse.php { if ($arg_u ~ "^http") { return 410; } }