如何在多个位置重用NGINX代理设置

bmc*_*kim 8 security proxy webserver dry nginx

我需要根据查询字符串参数限制对某些文件的访问.我有一个NGINX代理服务器,位于其他几个nginx Web服务器之前,用于负载平衡.我已决定在代理服务器级别强制执行此查询字符串参数,以便合并配置更改.这为我的设置增加了一点复杂性,因为请求不会被困在if中,因为它需要向上游发送.

server {
        listen 443;
        # SSL Settings

        server_name staging.xxxx.com;

        location / {
                proxy_pass http://webdav-cluster;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Real-IP       $remote_addr;
                proxy_set_header Host            $http_host;
        }

        # Unless the correct application token is passed in as a query parameter
        # then deny access.
        location ~ \/protected\/.*txt$ {
                if ($arg_secret != abc) {
                        return 403;
                }

                proxy_pass http://webdav-cluster;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Real-IP       $remote_addr;
                proxy_set_header Host            $http_host;
        }
}
Run Code Online (Sandbox Code Playgroud)

有没有办法将这4个代理行存储在一个位置或变量中,然后在内部重定向到一行?我也可以在不同的虚拟主机中使用相同的设置.

VBa*_*art 4

在这种情况下,您应该使用include指令: http: //nginx.org/r/include

  • 从技术上讲是正确的,但是如果能够做到这一点而无需将其分成几个文件来处理小事情,那就太好了。 (5认同)