是否可以配置 Nginx 以接受对同一 URL 具有和不具有代理协议的请求?

是否可以配置 Nginx 以接受对同一 URL 具有和不具有代理协议的请求?

我在代理服务器后面有一组应用程序,它们适当地转发请求并使用代理协议来保存请求的原始数据。这些应用程序还会相互发出请求,因此我希望它们接受使用和不使用代理协议的请求。是否可以配置 Nginx 以某种方式执行此操作,而无需使用其他server_name或端口?

答案1

如果不使用不同的server块,唯一的方法是使用不同的listen指令。这意味着运行 nginx 的服务器必须具有不同的 IP 地址,以便从外部代理和内部服务器群连接到服务器。

例如,您可能有一个用于内部应用的内部网络 10.87.239.0/24,而运行 nginx 的服务器位于 10.87.239.3。然后,您有一个外部网络 10.87.238.0/24,您的外部代理服务器使用它来访问 nginx,并且该服务器的地址为 10.87.238.3。在这种情况下,您可以将 nginx 配置为:

server {
    # PROXY protocol connections
    listen 10.87.238.3:443 ssl http2 proxy_protocol;
    set_real_ip_from 10.87.238.2;  # The address(es) of the proxies
    real_ip_header proxy_protocol;

    # Direct connections
    listen 10.87.239.3:443 ssl http2;
    listen [::]:443 ssl http2;

    # everything else for this block
}

顺便说一句,即使没有全球 IPv6 连接,您也应该已经在组织内部署了 IPv6。如果您没有单独的内部 IPv4 网络,则可以将其用于内部通信。

答案2

代理协议规范代理协议明确指出了运行监听器的风险,该监听器试图猜测是否在同一地址和端口上使用代理协议。

The receiver MUST be configured to only receive the protocol described in this
specification and MUST not try to guess whether the protocol header is present
or not. This means that the protocol explicitly prevents port sharing between
public and private access. Otherwise it would open a major security breach by
allowing untrusted parties to spoof their connection addresses. The receiver
SHOULD ensure proper access filtering so that only trusted proxies are allowed
to use this protocol.

因此,尝试接受带有和不带有代理协议的请求都会有风险。

相关内容