proxy_cookie_path

The 'proxy_cookie_path' directive modifies the path attribute of Set-Cookie headers in proxied responses. — NGINX HTTP Core

proxy_cookie_path
httpserverlocation
Синтаксисproxy_cookie_path original_path new_path;
По умолчаниюnone
Контекстhttp, server, location
МодульNGINX HTTP Core
Аргументы1-2

Описание

The 'proxy_cookie_path' directive is used in NGINX configurations where responses are being proxied to a backend server. Its primary function is to adjust the 'Path' attribute of the 'Set-Cookie' headers returned from the proxied server. This is useful for ensuring that cookies are sent only to the desired URLs under specific paths, thus enhancing security and functionality in scenarios where the proxied server's cookie path does not align with the NGINX server's URL structure. This directive can take one or two arguments. The first argument is the original cookie path which would typically be found in the Set-Cookie headers of the response. The second argument is the new path that should replace the original one. If only one argument is provided, it indicates that all cookies with that original path should be replaced with the new default path. When both arguments are specified, only cookies that match the exact original path will be affected. The directive operates at multiple contexts including 'http', 'server', and 'location', making it versatile for various configuration scopes. The behavior of the 'proxy_cookie_path' directive also accommodates multiple directives for different paths, allowing multiple declarations with specific behavior, which provides granular control over how cookies from the backend server are handled in relation to their URL paths in the frontend NGINX server.

Пример конфига

location /api {
    proxy_pass http://backend;
    proxy_cookie_path /myapp /;
}

Ensure the original path matches the Path attribute of the headers you wish to modify.

Using incorrect paths can lead to cookies not being sent to the browser, impacting user sessions.

Overusing this directive can lead to confusion if different paths are modified inappropriately.