$cookie_*
The $cookie_ variable prefix is used to access the value of HTTP cookies sent by the client. — NGINX Core (HTTP)
Description
In NGINX, variables prefixed with $cookie_ allow for easy access to cookie values set by clients in their HTTP requests. When a client sends a request to an NGINX server, it may include various cookies that store user-specific information, session details, or preferences. By referencing $cookie_
Config Example
server {
listen 80;
server_name example.com;
location / {
if ($cookie_user_id) {
add_header X-User-ID $cookie_user_id;
}
}
}Subsystem
httpCacheable
YesType
Prefix variableContexts
http, server, location, if, limit_exceptEnsure the cookie name does not contain special characters or spaces, as this can lead to unexpected behavior.
Be aware that missing cookies will result in an empty string, which could impact logic in conditional statements.
If caching is used, ensure that cookies are appropriately managed to avoid stale responses based on user sessions.