阻止特定 URL 的 Nginx 规则

use*_*598 3 nginx nginx-location

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

有问题的网址是:

www.somesite.com/catalogsearch/result/?q=downloader
Run Code Online (Sandbox Code Playgroud)

我尝试了以下方法,但这不起作用

location ^~ catalogsearch/result/?q=downloader {
    deny all;
}
Run Code Online (Sandbox Code Playgroud)

我“认为”是因为?包含问号是将 url 视为查询字符串吗??

问候

Val*_*Psr 6

如果您只想通过q=downloaderURL www.somesite.com/catalogsearch/result/ 上的参数阻止访问:

error_page 418 = @blockAccess;

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

location @blockAccess {
        deny all;
}
Run Code Online (Sandbox Code Playgroud)

添加之前 location /

如果你想阻止q=downloader所有 URL 的参数,只需将下面的代码放在前面location

error_page 418 = @blockAccess;

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

location @blockAccess {
    deny all;
}
Run Code Online (Sandbox Code Playgroud)

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

error_page 418 = @blockAccess;

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

location @blockAccess {
        deny all;
}
Run Code Online (Sandbox Code Playgroud)