nginx封禁路径访问

nginx封禁某个路径及其子路径并且还有其他后端代理地址可能包含这个路径也需要封禁

全局封禁包含 "/restricted" 的路径

rewrite ^.*restricted.*$ /forbidden_path break;  # 匹配包含 /restricted 的路径并重写为 /forbidden_path

# 设置 /forbidden_path 路径为禁止访问
location /forbidden_path {
    return 403;  # 返回 403 Forbidden
}

注:上面可能会有一些风险?比如性能问题?

另外的办法:使用一个map,再针对单个location进行if判断

http {
    map $request_uri $forbidden_path {
        default         0;
        ~*restricted    1;
    }

    server {
        location / {
            if ($forbidden_path) {
                return 403;
            }
            proxy_pass http://backend;
        }
    }
}
posted @ 2025-03-25 17:22  狗狗没有坏心眼  阅读(79)  评论(0)    收藏  举报