$request_port

The $request_port variable contains the port number used by the client to send the request to the server. — NGINX Core (HTTP)

$request_port NGINX Core (HTTP)

Description

The $request_port variable in NGINX represents the port on which the request was received from the client. This variable is set during the processing of the request and is derived from the client's connection details as specified in the request header. When a client makes an HTTP or HTTPS request, the request is routed to the specified server block based on matching criteria, which includes the port number. For standard HTTP requests, this is typically port 80, while for HTTPS requests, it's usually port 443. If the request is made using a non-standard port, $request_port will reflect that specific port number. For example, if the client connects to port 8080, then the value of $request_port will be "8080". It is important to note that when using protocols like HTTP/2, the port can be dynamically negotiated, but NGINX will still capture and reflect the port number used for that connection in the $request_port variable. Being aware of this value can be helpful for logging, conditional configuration, or when rewriting URLs based on the received port.

Config Example

server {
    listen 80;
    location / {
        return 200 "Request received on port: $request_port";
    }
}

Subsystem

http

Cacheable

Yes

Contexts

http, server, location, if

The variable will be empty if the server is running behind a proxy that does not forward the original client port.

Using this variable in certain contexts may lead to unexpected results if the request is forwarded or modified by an upstream server.