$is_request_port

The $is_request_port variable indicates whether the request was made on an alternate port rather than the default port for the protocol. — NGINX Core (HTTP)

$is_request_port NGINX Core (HTTP)

Description

The `$is_request_port` variable is used to determine if the incoming HTTP request was made on a non-standard port, allowing the server to differentiate between requests made on the standard ports (80 for HTTP and 443 for HTTPS) and those made on alternate ports. This variable is evaluated during the request processing phase, specifically when the request is being parsed and before it reaches any handlers. When this variable is set, it contains a value of '1' if the request was made on an alternate port and '0' if it was made on the standard HTTP or HTTPS ports. This is particularly useful in scenarios where applications need to apply specific configurations, logging, or access rules based on the port from which the request originated. The variable is automatically initialized by NGINX based on the incoming request's socket information and does not require manual setup. Developers often use this variable in combination with access control directives or in conditional blocks to provide tailored responses or logging based on whether the request was made to a standard or non-standard port, thereby enhancing control over server behavior based on client requests.

Config Example

server {
    listen 8080;
    location / {
        if ($is_request_port) {
            return 403;  # Deny access from non-standard ports
        }
        # Other configurations...
    }
}

Subsystem

http

Cacheable

Yes

Contexts

http, server, location, if

Ensure that the rewrite rules or access controls you set do not inadvertently block legitimate traffic when using this variable.

Be aware that the variable only evaluates to 1 or 0 based on the port; it does not provide the actual port number.