$server_addr

The $server_addr variable returns the IP address of the server as specified in the NGINX configuration. — NGINX Core (HTTP)

$server_addr NGINX Core (HTTP)

Description

The `$server_addr` variable is part of the NGINX Core module and provides the actual IP address of the server that is responding to a request. This variable is set during the processing of a request, specifically within the context of TCP/IP communication, allowing it to reflect the address that the client connected to. If the server has multiple IP addresses or is behind a load balancer, `$server_addr` will represent the primary address that NGINX uses to respond to incoming client requests. This variable can be particularly useful in logging or in the construction of response headers. It is worth noting that `$server_addr` reflects the address from which a request was processed, and it is determined before the request is passed to any `location` blocks or handlers. In scenarios where NGINX operates without a specific bound server IP (such as in a containerized environment where the IP can change), this variable may require appropriate configuration to ensure it always returns a valid address. Typically, it will return either an IPv4 or an IPv6 address, depending on the server's configuration and the nature of client requests.

Config Example

server {
    listen 80;
    server_name example.com;
    location / {
        add_header X-Server-IP $server_addr;
        # other configurations
    }
}

Subsystem

http

Cacheable

Yes

Contexts

http, server, location, if

If NGINX is behind a reverse proxy, the `$server_addr` may not reflect the original client's IP unless properly configured with `proxy_set_header X-Forwarded-For`.

In IPv6 configurations, ensure that the server is properly configured to handle IPv6 addresses to avoid unexpected results.