$uid_got

The $uid_got variable returns the user ID of the request's owner, which is useful for access control and logging. — NGINX Core (HTTP)

$uid_got NGINX Core (HTTP)

Description

The $uid_got variable in NGINX represents the unique identifier of the user associated with the request. This variable is typically set by the NGINX core when a request is processed. It is particularly useful in configurations that require access control based on user permissions or when tracking user activities through logs. When a request is received, NGINX checks the associated user ID, which can be derived from various sources, including system users, for authentication and authorization checks. In environments that utilize user-based access controls, such as when integrating with applications that require distinct user permissions, the $uid_got variable becomes vital. The variable is populated when the connection is established, and it reflects the UID assigned to that connection. Given its nature, the value may vary depending on how the request is processed and the context it operates in. Typically, you will see values ranging from 0 to 65535, which correspond to common user IDs (or reserved user IDs) on a UNIX-like system. For enhanced security and precise logging, the $uid_got variable can be logged in a custom NGINX log format or used in conditional configurations to restrict access based on user identification, helping establish a robust access control strategy.

Config Example

location /protected {
    if ($uid_got = 1001) {
        return 403;  # Deny access to users with UID 1001
    }
    proxy_pass http://backend;
}

Subsystem

http

Cacheable

Yes

Contexts

http, server, location, if

Ensure that the user context is correctly established; otherwise, $uid_got may not return expected values.

Using $uid_got in the wrong context (e.g., as part of server directives) can lead to unexpected behavior.

Be cautious of using $uid_got in high-performance contexts, as it can introduce unnecessary overhead.