Nginx Reverse Proxy Setup: Route Multiple Apps with HTTPS, Headers, and Health Checks
NginxReverse ProxyHTTPSLinuxWeb HostingInfrastructure

Nginx Reverse Proxy Setup: Route Multiple Apps with HTTPS, Headers, and Health Checks

PPlkdt Labs
2026-11-07
8 min read

Route multiple apps through Nginx with HTTPS, WebSockets, headers, health checks, safe reloads, and a recurring troubleshooting checklist.

This Nginx reverse proxy setup guide shows how to route multiple web applications from one Linux server, terminate HTTPS, forward WebSocket traffic, add useful headers, monitor health, and reload configuration safely. It also provides a recurring checklist for reviewing routes, certificates, logs, and upstream services as your infrastructure changes.

Overview

Nginx can act as the public entry point for several applications running on the same server. Each application listens on a private port, while Nginx accepts requests on ports 80 and 443 and chooses an upstream based on the requested hostname. For example, app.example.com might route to a frontend on port 3000, while api.example.com routes to an API on port 8080.

This arrangement separates public traffic from application processes. It also gives you one place to manage TLS certificates, access logs, redirects, request headers, WebSocket forwarding, and basic routing rules. Nginx does not replace application-level authentication, authorization, input validation, or service monitoring, but it can provide a consistent edge layer for those applications.

Before changing configuration, confirm that your DNS records point the intended hostnames to the server's public address. If you are connecting a new domain or subdomain, a DNS propagation check can help distinguish a DNS issue from an Nginx issue. Keep each application bound to an appropriate interface and port, and avoid exposing internal service ports publicly when Nginx is the intended entry point.

The examples below assume a Debian- or Ubuntu-style Linux server, a user with administrative privileges, and two applications:

  • app.example.com routes to 127.0.0.1:3000.
  • api.example.com routes to 127.0.0.1:8080.

Package names and service commands can differ on other distributions, so treat the commands as a starting point and check the conventions of your operating system.

What to track

1. DNS and hostname ownership

Maintain a small inventory of every hostname, its purpose, its DNS record, and its Nginx configuration file. An address record usually maps a hostname to an IPv4 address, while an address record for IPv6 may be needed separately. A CNAME can point a hostname to another hostname, but it does not directly contain an IP address.

Record which hostnames are public, which are intended only for staging, and which should not be exposed. If DNS is managed through an API or infrastructure-as-code workflow, keep the DNS definition and Nginx server block changes in the same change process where practical. The guide to automating DNS records with Cloudflare API and Terraform covers one way to make DNS changes repeatable.

2. Upstream definitions

Define upstreams with meaningful names rather than repeating raw addresses throughout a configuration. A basic layout looks like this:

upstream frontend_app {
    server 127.0.0.1:3000;
}

upstream backend_api {
    server 127.0.0.1:8080;
}

For a single application process, this is mainly a readability improvement. If you later add multiple application instances, the upstream block gives you a place to list them and review their status-related settings. Nginx can distribute requests among listed servers, but application-level session handling and shared state still need to be designed separately.

3. Server blocks and routing rules

Use a separate server block for each public hostname or closely related routing group. A basic HTTP block can redirect traffic to HTTPS:

server {
    listen 80;
    listen [::]:80;
    server_name app.example.com;

    return 301 https://$host$request_uri;
}

The HTTPS block proxies requests to the selected upstream:

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name app.example.com;

    ssl_certificate /etc/letsencrypt/live/app.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/app.example.com/privkey.pem;

    location / {
        proxy_pass http://frontend_app;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Repeat the pattern for api.example.com, changing the server name, certificate paths, and upstream. The Host and forwarding headers help the application understand the original request context. Your framework may need an explicit trusted-proxy setting before it accepts forwarded protocol or client-address information.

4. TLS, headers, and WebSockets

Use certificates that match the hostnames users visit. Certificate issuance and renewal should be tested before a production deadline; if you use Certbot, follow the current instructions for your operating system and Nginx package rather than relying on an old command copied from another server.

Security headers should be selected based on the application. A cautious starting point might include:

add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;

Do not add a restrictive Content Security Policy without testing the application, because it can block required scripts, fonts, images, or API calls. Headers are not a substitute for secure application code.

For WebSockets, pass the upgrade headers explicitly. One common pattern is:

map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}

location /socket/ {
    proxy_pass http://frontend_app;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
}

Keep WebSocket paths specific when possible. Broadly applying upgrade behavior to every request can make troubleshooting harder and is unnecessary for ordinary HTTP traffic.

5. Logs and health signals

Track the Nginx access log, error log, application logs, service status, disk space, memory pressure, and certificate expiry. A reverse proxy can be healthy while an upstream application is failing, so monitor both layers. Add a simple application health endpoint when appropriate, but make sure it reports useful dependency status without exposing secrets or sensitive diagnostic details.

Cadence and checkpoints

A lightweight review each month or quarter is usually more useful than waiting for an outage. The exact cadence should reflect how often the server and applications change.

Before every configuration change

  1. Confirm the intended hostname, upstream port, and DNS target.
  2. Back up or commit the current Nginx configuration.
  3. Check that certificates and private key paths are correct.
  4. Run sudo nginx -t before reloading.
  5. Reload rather than restart when the syntax check passes: sudo systemctl reload nginx.
  6. Test the affected hostname over HTTP and HTTPS, including any API or WebSocket path.

Monthly or quarterly review

  • Remove server blocks and DNS records for retired applications.
  • Check that every public hostname still reaches the intended server.
  • Review 4xx and 5xx responses, especially repeated 502 and 504 errors.
  • Confirm certificate renewal is scheduled and has been tested.
  • Review exposed listening ports and firewall rules.
  • Verify log rotation and available disk space.
  • Recheck application and operating-system update plans.

If the application is deployed with containers, document how container ports map to the host and whether containers are restarted automatically. The Docker deployment guide for a Linux server provides additional context for turning a container into a managed service. For a repeatable release process, see the guide to Docker, GitHub Actions, and Nginx deployments.

How to interpret changes

A 502 Bad Gateway usually means Nginx could not obtain a valid response from the upstream. Start with the error log, then check whether the application is running and listening on the configured address and port:

sudo systemctl status your-app-service
sudo ss -ltnp
curl -i http://127.0.0.1:3000

If the local request fails, investigate the application, process manager, container, or firewall first. If the local request succeeds but the public request fails, compare the server block, hostname, TLS configuration, and forwarding behavior. A mismatch between proxy_pass and the application's expected URL path can also produce confusing results.

A 504 Gateway Timeout suggests that Nginx waited too long for the upstream. Look for slow database queries, blocked dependencies, overloaded workers, or an unsuitable timeout. Increasing a timeout can hide an application performance problem, so measure the upstream behavior before changing values.

If HTTPS fails while HTTP works, verify that the certificate covers the requested name, the certificate files are readable by Nginx, port 443 is reachable, and the correct server block is being selected. If a WebSocket connection closes immediately, inspect the upgrade headers, proxy HTTP version, path routing, and application logs.

When a change appears to be a DNS problem, query the authoritative configuration and compare it with several resolvers. DNS caches can retain previous answers until their configured lifetime expires. A DNS check can show where answers differ, but it cannot confirm that Nginx is serving the correct application after the request reaches the server.

When to revisit

Revisit this setup whenever you add a service, rename a hostname, move an application to a new port, change certificate tooling, alter firewall rules, or introduce a CDN or load balancer. Also review it after operating-system, Nginx, runtime, or container-platform changes that could affect service startup, file permissions, TLS behavior, or network binding.

Use a quarterly checkpoint even when nothing obvious has changed. Confirm that backups can restore the configuration, certificates renew successfully, logs remain available, and the documented upstream ports still match reality. Infrastructure drifts when deployment scripts, manual fixes, and application changes are made independently.

For the next change, create a short record containing the hostname, upstream, DNS change, expected result, rollback step, and verification commands. Then apply this sequence:

  1. Update DNS or the application deployment as needed.
  2. Update the relevant Nginx server block.
  3. Run nginx -t.
  4. Reload Nginx without interrupting existing connections where supported by the service manager.
  5. Test the public URL, API response, TLS certificate, and any WebSocket endpoint.
  6. Watch access and error logs for several requests.
  7. Record the result and the date of the next review.

If you need to add a staging hostname, keep its routing and access controls separate from production. The guide to creating a staging subdomain covers ways to reduce accidental discovery while you test. A documented, repeatable Nginx reverse proxy setup makes adding the next application less risky and makes a future 502 easier to diagnose.

Related Topics

#Nginx#Reverse Proxy#HTTPS#Linux#Web Hosting#Infrastructure
P

Plkdt Labs

Editorial Team

Senior editor and content strategist. Writing about technology, design, and the future of digital media. Follow along for deep dives into the industry's moving parts.