When working with NGINX reverse proxy configurations, you'll often encounter directives like:
proxy_pass_header Server;
This directive controls whether NGINX passes the Server
HTTP header from upstream servers to clients. By default, NGINX filters out this header to prevent exposing backend server information.
In security-sensitive environments, hiding server information is considered a best practice to:
- Prevent attackers from identifying specific server versions with known vulnerabilities
- Maintain a unified frontend appearance regardless of backend infrastructure
- Simplify debugging by standardizing headers across environments
You might want to enable passing the Server header when:
# Case 1: Debugging backend responses
location /debug/ {
proxy_pass http://backend;
proxy_pass_header Server;
proxy_pass_header X-Backend-Status;
}
# Case 2: API gateways needing original headers
location /api/ {
proxy_pass http://api_cluster;
proxy_pass_header Server;
proxy_pass_header X-Api-Version;
}
If you choose to pass the Server header, consider these alternatives:
# Option 1: Override with custom value
proxy_set_header Server "Custom Server Name";
# Option 2: Completely remove the header
proxy_hide_header Server;
Here's a complete NGINX configuration snippet demonstrating proper usage:
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://backend;
proxy_pass_header Server;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
# Cache settings
proxy_cache my_cache;
proxy_cache_valid 200 302 10m;
}
}
While the directive itself has negligible performance impact, consider that:
- Larger headers increase bandwidth usage
- Backend server information might help attackers optimize their exploits
- Debugging becomes easier with more header information available
For more granular control over headers:
# Pass only specific headers from upstream
map $upstream_http_server $backend_server {
default "";
"~*" $upstream_http_server;
}
server {
# ...
add_header X-Backend-Server $backend_server;
}
When working with NGINX as a reverse proxy, the proxy_pass_header Server;
directive plays a crucial role in HTTP header manipulation. By default, NGINX strips the Server
header from upstream server responses as a security measure. This directive explicitly allows the Server
header from the proxied server to pass through to the client.
Here's a typical configuration example where you might use this directive:
location / {
proxy_pass http://backend;
proxy_pass_header Server;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
The primary use cases for enabling the Server header include:
- Debugging purposes during development to identify which backend server handled the request
- Maintaining API compatibility when client applications expect specific Server headers
- Load balancer configurations where you need to preserve original server information
While the Server header can be useful, be aware that:
# For production environments, consider masking server information:
location / {
proxy_pass http://backend;
proxy_pass_header Server;
proxy_hide_header Server;
proxy_set_header Server "Custom-Server";
}
This configuration allows you to inspect the original Server header internally while presenting a generic value to clients.
If you need more control over headers, consider these NGINX directives:
proxy_set_header - Allows setting custom headers
proxy_hide_header - Hides specific headers from clients
more_clear_headers - Clears multiple headers at once