Nginx规则阻止特定URL

时间:2018-02-04 22:11:42

标签: nginx nginx-location

在编写规则以阻止以下请求方面需要一些帮助

有问题的网址是:

www.somesite.com/catalogsearch/result/?q=downloader

我尝试过以下操作,但这不起作用

location ^~ catalogsearch/result/?q=downloader {
    deny all;
}

我"思考"因为?包含的问号是将url视为查询字符串??

此致

1 个答案:

答案 0 :(得分:3)

如果您希望仅通过URL www.somesite.com/catalogsearch/result/阻止通过参数q=downloader进行访问:

error_page 418 = @blockAccess;

location /catalogsearch/result {
        if ($args ~* "q=downloader") {
                return 418;
        }
}

location @blockAccess {
        deny all;
}
  

location /之前添加

如果您想阻止所有网址的q=downloader参数,只需将代码放在location之前:

error_page 418 = @blockAccess;

if ($args ~* "q=downloader") {
    return 418;
}

location @blockAccess {
    deny all;
}

如果您想阻止www.somesite.com/catalogsearch/result/:

error_page 418 = @blockAccess;

# Add before "location /"
location /catalogsearch/result {
        return 418;
}

location @blockAccess {
        deny all;
}