proxy_set_header

The `proxy_set_header` directive allows you to modify the headers sent to a proxied server. — NGINX HTTP Core

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

Описание

The `proxy_set_header` directive is used in NGINX to alter or define the headers that are sent from the NGINX server to the upstream server in a proxy context. This is particularly useful for passing additional information, such as original client information, or modifying existing headers before forwarding requests to a proxied backend. The directive takes two arguments: the header that you want to set or modify and the value to assign to that header. You can use variables provided by NGINX, allowing for dynamic header values that can adapt based on the request context. For instance, you might want to set the `X-Real-IP` header to the client's IP address. You can also override existing header values as necessary. It is important to note that if a header is set more than once for the same name, the last set value will take precedence. This directive can be used in various contexts including `http`, `server`, and `location` blocks, providing flexibility in how headers are handled for different parts of your server configuration. Properly configuring headers is crucial for correct functionality of backend applications, as they often rely on HTTP headers for routing, authentication, and other request management tasks.

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

location /api {
    proxy_pass http://backend;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
}

Headers set with `proxy_set_header` are cumulative; if a header is defined multiple times, the last defined value is used.

Make sure to define the directive in the correct context (http, server, or location) to avoid configuration errors.

Using simple values instead of variables (e.g., fixed strings) may not yield expected results in dynamic cases.