【问题标题】:How to re-use NGINX proxy settings in several locations如何在多个位置重用 NGINX 代理设置
【发布时间】:2013-02-19 19:40:00
【问题描述】:

我需要根据查询字符串参数限制对某些文件的访问。我有一个 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;
        }
}

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

【问题讨论】:

  • OP,你最后做了什么?我也很好奇这个。

标签: security proxy nginx webserver dry


【解决方案1】:

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

【讨论】:

  • 技术上是正确的,但如果能够做到这一点而不将其拆分成几个小文件,那就太好了。
  • 然后使用您喜欢的模板引擎生成配置。也可以是bash
猜你喜欢
  • 2018-11-24
  • 2020-12-18
  • 1970-01-01
  • 1970-01-01
  • 2015-07-20
  • 2021-10-01
  • 2015-03-10
  • 2021-07-12
  • 2013-04-23
相关资源
最近更新 更多