Nginx often sits at the boundary between the public internet and an application that assumes a much friendlier environment. A reverse proxy configuration therefore defines more than an upstream address. It defines routing, identity, transport behavior, and several important limits. This overview explains the decisions worth making explicitly, so a working configuration remains understandable when the application grows or an incident sends you back to the request path.
Key takeaways
- Server selection, location matching, and proxy_pass URI handling together define the application’s public routing contract.
- Forwarded headers are only trustworthy within a deliberately configured proxy chain.
- Timeouts, buffering, and protocol upgrades must match the endpoint’s workload rather than one copied global setting.
Follow server and location selection
Begin with the listening address, port, server names, and default server. Confirm where an unexpected Host header is routed instead of assuming every request will use the intended domain. Inside that server, inspect location matching and the resulting upstream or filesystem path. A configuration can be syntactically valid while a more specific location or a regular expression sends a particular request somewhere the author did not expect.
Keep the routing tree small and test representative paths: the homepage, an API endpoint, a static file, a missing resource, and a path with a trailing slash. Be especially careful when combining root, alias, rewrites, and proxying. Explain the desired mapping in ordinary language first. That makes a review much easier than trying to infer the intended public URL contract from several interacting directives.
Be precise about upstream paths
The URI included in proxy_pass affects path replacement. A location prefix forwarded with an upstream URI can behave differently from a proxy_pass value containing only the upstream origin. This is why a seemingly harmless trailing slash can change which path the application receives. Consult the proxy module's rules for the configuration form you are using, and verify the actual upstream request rather than relying on visual similarity.
Test redirects and generated URLs as well as successful responses. An application may need to know its public scheme, host, or mounted path to avoid emitting internal addresses or incorrect redirects. Keep those values consistent with the deployment's trust model. A proxy should not quietly transform an application intended for one URL layout into another without checking cookies, assets, links, and authentication callbacks.
Decide which headers are trustworthy
Forwarded headers can describe the original client and public HTTPS connection, but they are only meaningful when the receiving application knows which proxies are trusted. Configure the boundary so an arbitrary external header cannot impersonate another client address or convince the application that an insecure request was secure. If a CDN sits in front of Nginx, include that hop in the model rather than treating the immediate network peer as the visitor.
Verify the client identity used by access logs, rate limits, application security checks, and any banning tool. Those components can disagree if each interprets forwarded values differently. Prefer one documented chain of interpretation and test both requests through the proxy and any reachable direct origin path. Log the evidence needed to investigate routing without recording authorization headers, session credentials, or unnecessary personal data.
Use timeouts to describe real work
Connection, send, and read timeouts protect different stages of the upstream exchange. Many proxy timeout settings describe inactivity between operations rather than a complete end-to-end request budget. Increasing every timeout can hide a blocked application while tying up resources for longer. Identify the failing stage first, then choose a limit consistent with the work the endpoint is supposed to perform and the client's own expectations.
Treat request size and buffering as intentional choices. Ordinary API responses often benefit from buffering, while event streams may need different behavior to deliver updates promptly. Large uploads need limits that protect the service without surprising legitimate users. Test the actual workload through the entire chain, including any CDN or load balancer. A local connection to the application does not reproduce the behavior of a public proxied request.
Handle upgraded and streamed connections
WebSocket connections require the proxy to support the protocol upgrade deliberately. Hop-by-hop headers are not simply forwarded like ordinary end-to-end metadata, so use Nginx's documented upgrade configuration. Verify idle behavior and reconnection as well as the initial handshake. A successful connection that reliably dies during ordinary inactivity is still an incomplete deployment, especially when another proxy imposes a shorter limit further upstream.
For server-sent events or media delivery, check buffering, compression, timeouts, and cache behavior against the format's requirements. Do not copy one streaming configuration across every endpoint. A chat event stream, a downloadable file, and an HLS playlist have different freshness and delivery needs. Keep exceptions scoped to the routes that require them, with a short explanation of the behavior being preserved.
Reload with evidence and a fallback
Run nginx -t before applying a configuration change; it checks syntax and attempts to open referenced files. Keep the previous configuration available and use the supported reload process rather than restarting blindly. Then test the public routes and inspect error logs. Syntax validation cannot prove that the correct certificate, upstream, client address, or cache policy was chosen for every path.
Record the expected routing and a small set of checks alongside the configuration. Include a missing URL and an upstream failure, not only the happy path. A reliable reverse proxy setup is one where another engineer can trace an incoming request, understand what is trusted, identify the active limits, and recover from a bad change. The best configuration is usually the smallest one that makes those responsibilities explicit.
Action notes
- Test the homepage, an API path, a trailing slash, a missing resource, and an unexpected Host header against the intended server.
- Trace client identity through the proxy, application logs, rate limits, and banning integrations.
- Run nginx -t before reloading, then test public behavior and inspect logs with the previous configuration available.