$proxy_protocol_port

The $proxy_protocol_port variable returns the port number from which a proxied request is received when using the PROXY protocol. — NGINX Core (HTTP)

$proxy_protocol_port NGINX Core (HTTP)

Description

The $proxy_protocol_port variable is utilized within NGINX to retrieve the port number specified in the PROXY protocol header for connections that have been proxied. It is primarily relevant in configurations where NGINX acts as a reverse proxy or load balancer, allowing the server to accurately identify the original port from which the client connection was made. This information can be crucial for logging, security measures, or backend server decisions. This variable is set only when the PROXY protocol is enabled in the upstream server block. If the PROXY protocol is not utilized, the variable will not hold a valid value, effectively resolving to 0. Typical values for this variable would be the original port numbers such as 80 or 443 for HTTP and HTTPS traffic. Using $proxy_protocol_port allows for greater context about the incoming request, which is especially valuable in multi-tier architectures where backend processing may depend on the original client port.

Config Example

server {
    listen 80 proxy_protocol;
    location / {
        add_header X-Proxy-Port $proxy_protocol_port;
    }
}

Subsystem

http

Cacheable

Yes

Contexts

http, server, location

Ensure that the PROXY protocol is enabled at the listen directive; otherwise, the variable will not be set and will return 0.

For the variable to provide meaningful data, it must be used in a context where the PROXY protocol is supported and correctly configured.