TL;DR
Yes, an NGINX reverse proxy can reveal the backend server if not configured correctly when serving dynamic content. This is usually due to error pages exposing internal details or improper header handling. We’ll cover how to prevent this.
Solution Guide
- Understand the Risk
- When NGINX encounters an issue communicating with your backend server (e.g., 502 Bad Gateway, 504 Timeout), it might display a default error page that includes information about the backend server’s address and port.
- Incorrectly configured headers can also leak backend details to clients. For example, the
Serverheader might show the backend application server instead of NGINX.
The most important step is creating custom error pages that don’t reveal internal information.
- Edit your NGINX configuration file (usually in
/etc/nginx/sites-available/or similar). - Use the
error_pagedirective to specify a custom HTML page for common error codes.
server {
...
error_page 502 504 /custom_error.html;
location = /custom_error.html {
root /var/www/errors;
internal;
}
}
custom_error.html file in the specified directory (e.g., /var/www/errors/custom_error.html) with a user-friendly message.internal; directive prevents direct access to your error page from outside, enhancing security.Control the headers NGINX sends to clients.
- Use the
server_tokens off;directive in yourhttpblock to hide the NGINX version. This is a basic step but helpful.
http {
...
server_tokens off;
}
Server header using the proxy_hide_header Server; directive within your location block.location / {
proxy_pass http://backend_server:8080;
proxy_hide_header Server;
}
Server header to identify NGINX as the proxy, masking the backend.location / {
proxy_pass http://backend_server:8080;
add_header X-Proxyed-By "NGINX Proxy";
proxy_hide_header Server;
}
- After making changes, test your configuration.
- Use a tool like
curl -Ito inspect the headers returned by NGINX. Verify that backend server information is not present. - Simulate errors (e.g., stop the backend server temporarily) and check if the custom error page is displayed without revealing internal details.
- Implement a Web Application Firewall (WAF) to protect against common web attacks, including those that attempt to reveal server information.
- Regularly update NGINX and your backend software to patch security vulnerabilities.
- Use strong authentication and authorization mechanisms for access to your backend services.