Defensive proxy configuration
Start with the trust boundary
A defensive reverse proxy limits which requests reach which resources. It is not a substitute for backend authorization. For a small public documentation site, permit only the intended host and public files; keep administrative endpoints and private data outside the document root. The default virtual server below rejects unknown hosts. The named site is a separate server on the same loopback socket. These are configuration fragments for the http context, not standalone files.
server { listen 127.0.0.1:19094 default_server; server_name _; return 404;}server { listen 127.0.0.1:19094; server_name docs.test; root /srv/demo/public; location / { try_files $uri =404; }}Constrain files and handlers
Publish only reviewed files using a worker that cannot modify the document root. A location marked internal is available to internal request processing, not ordinary direct external requests. It is not an access-control mechanism for filesystem permissions. Keep backups and private configuration physically outside the public tree, and avoid broad aliases. Symlink restrictions can add a layer where the OS supports them, but check module-specific limitations. The following fragment serves a friendly error page while retaining 404.
root /srv/demo/public;autoindex off;disable_symlinks on;error_page 404 /errors/not-found.html;location = /errors/not-found.html { internal; }Bound request work
Use endpoint-specific size limits based on legitimate payloads. client_max_body_size returns 413 for an oversized request; a zero size disables that check. Header timeout covers receipt of the full header; body timeout measures gaps between reads rather than total upload duration. For a GET/HEAD-only static location, limit_except applies access checks to other methods (GET implies HEAD). Do not put a rewrite-phase return in a location that depends on access-phase restrictions. Backends must still enforce their own method and input contracts.
client_header_timeout 10s;client_body_timeout 10s;client_max_body_size 8k;location /downloads/ { limit_except GET { deny all; } try_files $uri =404;}Review boundaries, not attack payloads
Path traversal is unintended file access; inconsistent URI interpretation between layers can cause routing or authorization mistakes. Request desynchronization concerns disagreement about message boundaries. SSRF is unintended server-side fetching. Prevent these classes with narrow static roots, fixed upstreams, aligned HTTP parsing and supported patched software, not ad hoc string filters. Use a fixed redirect destination and replace forwarded identity at the first trusted edge. Restrict direct origin access separately; keep application authorization for each resource. In the illustrative proxy fragment, the origin is a fixed loopback service that must actually exist before execution.
proxy_pass http://127.0.0.1:19095;proxy_set_header Host backend.test;proxy_set_header X-Forwarded-For $remote_addr;proxy_set_header X-Forwarded-Proto $scheme;Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Defensive proxy configuration
Design a defensive Nginx boundary with explicit hosts, narrowly scoped files, bounded requests and independent application authorization.
Why put private files outside the web root?
What does client_max_body_size 8k bound?
What does a client_body_timeout measure?
Which methods are implicitly allowed by limit_except GET?
Which design reduces unintended upstream fetching?