$proxy_protocol_server_port

The $proxy_protocol_server_port variable returns the server port as specified in the PROXY protocol. — NGINX Core (HTTP)

$proxy_protocol_server_port NGINX Core (HTTP)

Description

The $proxy_protocol_server_port variable is used in NGINX configurations to retrieve the port number that the server has received from the PROXY protocol header. This variable is particularly relevant when the PROXY protocol is enabled, allowing NGINX to understand when it is behind a proxy that forwards client connection information along with it. When a request is processed, if the PROXY protocol is activated via the 'proxy_protocol on' directive within the server block, the server port number will be dynamically set based on the incoming connection details relayed by the proxy. For example, if a request comes in on port 80, that value is what you would get if you were to reference $proxy_protocol_server_port. This variable can be very useful to configure SSL termination or to correctly redirect traffic based on the initial port that was used for the connection before any transformations. It can have typical values such as '80' for HTTP or '443' for HTTPS connections, depending on the configuration and the proxy server’s forwarding behavior.

Config Example

server {
    listen 80 proxy_protocol;
    location / {
        return 200 "Server port: $proxy_protocol_server_port";
    }
}

Subsystem

http

Cacheable

Yes

Contexts

http, server, location

Ensure that the PROXY protocol is actually enabled on the upstream server; otherwise, this variable will remain empty.

Be aware that using this variable without the necessary proxy settings will cause unexpected behavior, as it relies on the presence of the PROXY protocol header.

If using SSL, ensure the upstream server is transmitting both the PROXY protocol and the SSL settings correctly.