Blog | G5 Cyber Security

NGINX Reverse Proxy: Backend Server Exposure

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

  1. Understand the Risk
  • Custom Error Pages
  • The most important step is creating custom error pages that don’t reveal internal information.

    server {
      ...
      error_page 502 504 /custom_error.html;
      location = /custom_error.html {
        root /var/www/errors;
        internal;
      }
    }
  • Create a simple custom_error.html file in the specified directory (e.g., /var/www/errors/custom_error.html) with a user-friendly message.
  • Important: The internal; directive prevents direct access to your error page from outside, enhancing security.
  • Hide Backend Server Information in Headers
  • Control the headers NGINX sends to clients.

    http {
      ...
      server_tokens off;
    }
  • Remove or modify the Server header using the proxy_hide_header Server; directive within your location block.
  • location / {
      proxy_pass http://backend_server:8080;
      proxy_hide_header Server;
    }
  • Add a custom 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;
    }
  • Check Your Configuration
  • Consider Additional Security Measures
  • Exit mobile version