NetSendo
Guides & Tips

Reverse proxy for a Docker app: HTTPS and WebSockets

NetSendo Team15 min read
Reverse proxy for a Docker app: HTTPS and WebSockets

To expose a self-hosted Docker app safely, keep its containers bound to 127.0.0.1 and put a reverse proxy (nginx, Caddy or Traefik) on ports 80 and 443 that terminates TLS and forwards requests to them. For WebSockets, such as a Laravel Reverb server, the proxy needs a separate location that passes the Upgrade and Connection headers and a read timeout longer than nginx's 60-second default, and the browser must be told to connect with wss:// on port 443.

TL;DR: Bind the app (for example port 5029) and the WebSocket server (for example port 8085) to 127.0.0.1, then let nginx or Caddy handle HTTPS on 443. In nginx, route /app/ to the WebSocket server with Upgrade/Connection headers and a long proxy_read_timeout; Caddy proxies WebSockets with no extra configuration. After switching to HTTPS, make the app trust X-Forwarded-Proto, set APP_URL to https, and point the browser's WebSocket host, port (443) and scheme (https) at the proxy. Finally, check that ports 5029 and 8085 are closed from the outside, because Docker-published ports bypass ufw.

What does the reverse proxy do in front of a Docker app?

The reverse proxy is the only process that listens on the public interface: it holds the TLS certificate, accepts HTTPS and WSS from browsers, and forwards plain HTTP to containers that listen on the loopback address only. The containers never handle certificates and are never reachable directly from the internet.

A typical self-hosted Laravel stack has two upstreams. The web container (nginx plus PHP-FPM inside Docker) serves pages and the API; the WebSocket server, Laravel Reverb in this example, holds long-lived connections for real-time features. They live on different ports, so the proxy routes by path: everything goes to the app, except /app/, which is the path Reverb serves WebSocket connections on (it also serves an HTTP API under /apps, which the app container reaches inside the Docker network) according to the Laravel Reverb documentation.

Browser ──HTTPS/WSS :443──▶ reverse proxy (nginx / Caddy on the host)
                                 │
                                 ├── /        ──HTTP──▶ 127.0.0.1:5029  (web container: nginx + PHP-FPM)
                                 └── /app/    ──HTTP──▶ 127.0.0.1:8085  (Reverb WebSocket server)
Port (TCP)Listens onWho connectsOpen in the firewall?
800.0.0.0 (proxy)Browsers, ACME HTTP-01 challenge✓ Yes
4430.0.0.0 (proxy)Browsers (HTTPS and WSS)✓ Yes
5029127.0.0.1 (app container)The proxy only✗ No
8085127.0.0.1 (WebSocket container)The proxy only✗ No

As of December 2026. Port numbers 5029 and 8085 are NetSendo's defaults; Reverb itself starts on 8080 unless told otherwise.

TLS termination

The proxy decrypts HTTPS and speaks plain HTTP to the upstream on the same machine. The app learns that the original request was HTTPS only from the X-Forwarded-Proto header, which is why trusting that header correctly matters later.

Bind containers to 127.0.0.1, not to every interface

Write the host address into the port mapping in docker-compose.yml: "127.0.0.1:5029:80" instead of "5029:80". Docker's own documentation warns that publishing container ports is insecure by default, and that a port published on 127.0.0.1 is reachable from the Docker host only. This matters more than it looks, because Docker inserts its own packet-filter rules and traffic to published ports is diverted before it reaches ufw's rules: a ufw deny 5029 does not protect a port published on 0.0.0.0.

services:
  webserver:
    ports:
      - "127.0.0.1:5029:80"
  reverb:
    ports:
      - "127.0.0.1:8085:8085"

How do I configure nginx for HTTPS and WebSockets?

Get a certificate with Certbot, then use one server block on 443 with two locations: / proxied to the app with the X-Forwarded-* headers, and /app/ proxied to the WebSocket server with Upgrade and Connection headers and a long read timeout. Port 80 only redirects to HTTPS.

  1. Point DNS at the server

    Create an A record (and AAAA if the server has IPv6) for netsendo.example.com. Certificate issuance fails until the name resolves to this machine.

  2. Install nginx and Certbot

    On Debian or Ubuntu: apt install nginx certbot python3-certbot-nginx. Open ports 80 and 443 in the firewall.

  3. Request the certificate

    Run certbot --nginx -d netsendo.example.com, or certbot certonly --nginx -d netsendo.example.com if you prefer to write the TLS lines yourself. According to the Certbot user guide, most installations renew automatically through a scheduled certbot renew; check it with certbot renew --dry-run.

  4. Write the site configuration

    Use the file below in /etc/nginx/sites-available/ (or conf.d/), run nginx -t, then systemctl reload nginx.

# /etc/nginx/conf.d/netsendo.conf
map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}

server {
    listen 80;
    listen [::]:80;
    server_name netsendo.example.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    http2 on;
    server_name netsendo.example.com;

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

    client_max_body_size 100M;

    # Pages and API
    location / {
        proxy_pass http://127.0.0.1:5029;
        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;
        proxy_set_header X-Forwarded-Host $host;
        proxy_read_timeout 300s;
    }

    # WebSockets (Laravel Reverb)
    location /app/ {
        proxy_pass http://127.0.0.1:8085;
        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;
        proxy_read_timeout 3600s;
        proxy_send_timeout 3600s;
    }
}

We tested this file with nginx -t on nginx 1.31.8 (December 2026). The lines that matter:

  • map $http_upgrade $connection_upgrade is the pattern from the nginx WebSocket proxying page: it sends Connection: upgrade when the client asked for an upgrade and close otherwise. A hard-coded Connection "upgrade", as in many tutorials and the Reverb docs, also works for a location that only ever receives WebSocket handshakes.
  • proxy_http_version 1.1 is required on nginx older than 1.29.7, which used HTTP/1.0 to upstreams by default; the proxy module reference says 1.1 is the default since 1.29.7. Keeping the line is harmless and makes the file portable to older distributions.
  • proxy_read_timeout 3600s: by default nginx closes a proxied WebSocket if the upstream sends nothing for 60 seconds. Reverb's protocol has client and server pings that normally keep traffic flowing, but a longer timeout avoids reconnect storms when a ping is delayed.
  • http2 on; is the current syntax. With listen 443 ssl http2; nginx 1.25.1 and newer still start but log the "listen ... http2" directive is deprecated. The WebSocket location keeps talking HTTP/1.1 to Reverb either way.
  • No trailing URI in proxy_pass: http://127.0.0.1:8085 (without a slash) passes /app/<key> unchanged. Writing http://127.0.0.1:8085/ would strip /app/ and Reverb would answer 404.
⚠️ Watch out: the map block belongs in the http context. Files in conf.d/ and sites-enabled/ are included inside http, so it can sit at the top of the site file, but only once per server: a second identical map in another site file fails nginx -t with a duplicate variable error.

If the app sends large cookies or Link preload headers, the outer proxy can also return 502 with "upstream sent too big header"; the fix is in our guide to nginx proxy and FastCGI buffers.

Is Caddy simpler, and when does Traefik fit better?

Yes: Caddy obtains and renews the certificate itself, redirects HTTP to HTTPS and proxies WebSockets without any extra directives, so the whole site fits in about ten lines. Traefik fits better when many containers come and go and you want routing defined by Docker labels instead of a config file.

# /etc/caddy/Caddyfile
netsendo.example.com {
	encode zstd gzip
	request_body {
		max_size 100MB
	}
	reverse_proxy /app/* 127.0.0.1:8085 {
		stream_close_delay 5m
	}
	reverse_proxy 127.0.0.1:5029
}

This Caddyfile passes caddy validate on the official caddy:2 image (December 2026). The reverse_proxy documentation says the proxy performs the WebSocket upgrade and then switches to a bidirectional tunnel, and that it sets X-Forwarded-For, X-Forwarded-Proto and X-Forwarded-Host itself, ignoring values sent by clients unless you configure trusted_proxies. By default Caddy closes WebSocket connections when its configuration is reloaded; stream_close_delay keeps them open for a while, which avoids every browser reconnecting at once after a caddy reload.

Automatic HTTPS has requirements of its own, listed in Caddy's automatic HTTPS page: the A/AAAA records point to the server, ports 80 and 443 are reachable from outside, and Caddy's data directory is writable and persistent (in Docker, mount a volume on /data, or certificates are requested again after every container re-creation). Let's Encrypt is the default issuer with ZeroSSL as fallback.

Neednginx + CertbotCaddyTraefik
Certificates~ Certbot, separate timer✓ Built in✓ Built in (ACME resolver)
WebSocket config~ Upgrade headers, timeouts✓ None needed✓ None needed
Routing defined byConfig filesCaddyfileDocker labels or files
Already on the server✓ Often✗ Rarely✗ Rarely
Best forExisting nginx hosts, fine controlOne or a few appsMany containers that change often

As of December 2026.

Traefik reads containers from the Docker socket, which means the proxy container needs access to that socket (read-only at least). For a single app on a VPS this is extra attack surface for little gain; nginx or Caddy on the host is simpler to reason about.

Why do I get redirect loops or http:// links after enabling HTTPS?

Because the app sees plain HTTP from the proxy and does not know the visitor used HTTPS. Fix it in three places: the proxy sends X-Forwarded-Proto, the app trusts that header from the proxy, and the app's base URL (APP_URL in Laravel) starts with https://.

Trusted proxies in Laravel

Laravel reads X-Forwarded-* only from proxies it trusts. In Laravel 11 and newer this lives in bootstrap/app.php; the Laravel requests documentation shows both forms:

->withMiddleware(function (Middleware $middleware): void {
    // Trust only the proxy addresses you know
    $middleware->trustProxies(at: ['127.0.0.1', '172.16.0.0/12']);

    // Or trust any proxy (cloud load balancers with unknown addresses)
    // $middleware->trustProxies(at: '*');
})

Trusting * is only safe when nobody can reach the app without passing through your proxy, because then the proxy is the one writing the headers. If port 5029 is open to the internet, anyone can send X-Forwarded-Proto: https or a fake X-Forwarded-For directly. This is one more reason to bind containers to 127.0.0.1.

Behind Docker there are usually two proxies: the host proxy and the nginx inside the web container. The inner nginx passes the X-Forwarded-* headers on to PHP-FPM unchanged, but PHP sees the connection coming from the Docker bridge address, so that range (typically within 172.16.0.0/12) must be trusted too.

Redirect loops

ERR_TOO_MANY_REDIRECTS almost always means two layers disagree about the scheme. The usual combinations:

  • The app forces HTTPS (middleware or URL::forceScheme('https')) but does not trust X-Forwarded-Proto, so every request looks like HTTP and is redirected again.
  • A CDN such as Cloudflare in "Flexible" SSL mode talks HTTP to your origin, while your origin redirects HTTP to HTTPS. Use "Full (strict)" with a real certificate on the origin.
  • Two return 301 https://… rules, one in the host proxy and one inside the container, combined with a proxy that connects to the container over HTTP on port 80.

Mixed content

If the page loads over HTTPS but the console shows "Mixed Content: … requested an insecure resource", some URLs are still generated with http://. Check APP_URL, cached configuration (php artisan config:clear then config:cache), and absolute URLs stored in the database or in email templates. Browsers block a ws:// connection from an HTTPS page in the same way, which leads to the next section.

Which WebSocket settings must change in the browser?

The browser must connect to the public proxy address: host = your domain, port = 443, scheme = https (which makes the client use wss://). The server-side variables that tell Laravel where to deliver broadcasts usually stay on the internal address.

Reverb has two pairs of variables that are easy to confuse, and the Reverb documentation warns about it: REVERB_SERVER_HOST/REVERB_SERVER_PORT set where Reverb itself listens, while REVERB_HOST/REVERB_PORT tell Laravel where to send broadcast messages. The browser uses a third set, by convention the VITE_REVERB_* variables passed to Laravel Echo.

VariableUsed byLocal (no proxy)Behind an HTTPS proxy
REVERB_SERVER_PORTReverb process80858085 (unchanged)
REVERB_HOST / REVERB_PORT / REVERB_SCHEMELaravel (broadcasting)reverb / 8085 / httpreverb / 8085 / http (internal, unchanged)
VITE_REVERB_HOSTBrowser (Echo)localhostnetsendo.example.com
VITE_REVERB_PORTBrowser (Echo)8085443
VITE_REVERB_SCHEMEBrowser (Echo)httphttps

As of December 2026; values for a Docker setup where the Reverb service is named reverb.

With the scheme set to https, the Echo configuration Laravel generates (forceTLS: (import.meta.env.VITE_REVERB_SCHEME ?? 'https') === 'https', with wssPort taken from VITE_REVERB_PORT) opens wss://netsendo.example.com:443/app/<key>, which the proxy routes to Reverb.

The rebuild step

In a standard Laravel app the VITE_* values are read by Vite at build time and written into the JavaScript bundle (Vite: env variables and modes). Changing .env is not enough: run npm run build again (or rebuild the Docker image that runs it) and reload the page. If you still see the old port in the devtools Network tab, you are looking at the old bundle or a cached one.

Some apps avoid this by passing the WebSocket settings from the server on each page load instead of baking them into the bundle. NetSendo does this, as described below.

How do I test the proxy and close the direct ports?

Test from outside the server in four steps: HTTPS answers with the right headers, the WebSocket handshake returns 101 Switching Protocols, the browser shows an open wss:// connection, and the app and WebSocket ports are unreachable from the internet.

  1. HTTPS and redirect

    curl -I http://netsendo.example.com should return 301 to https; curl -I https://netsendo.example.com should return 200 or a 302 to the login page with an https:// Location.

  2. WebSocket handshake

    Send an upgrade request by hand. A 101 response proves the proxy and the headers are right. Reverb then sends pusher:connection_established; if it sends pusher:error with code 4001 ("Application does not exist") instead, the app key is wrong. A 404 points to the path, a 502 to a stopped Reverb or a wrong port.

  3. Browser devtools

    Open the page, then Network → filter "WS". You should see wss://netsendo.example.com/app/… with status 101 and messages flowing in the Messages tab, including pusher:connection_established.

  4. TLS quality

    Run the domain through SSL Labs (ssllabs.com/ssltest) to check the certificate chain and protocol versions.

  5. Closed ports

    From another machine: nc -zv your-server-ip 5029 and nc -zv your-server-ip 8085 must fail. On the server, ss -tlnp should list 5029 and 8085 on 127.0.0.1 only.

# 1. HTTPS
curl -I https://netsendo.example.com

# 2. WebSocket handshake through the proxy (replace YOUR_REVERB_APP_KEY)
curl -i --http1.1 -N --max-time 5 \
  -H "Connection: Upgrade" -H "Upgrade: websocket" \
  -H "Sec-WebSocket-Version: 13" \
  -H "Sec-WebSocket-Key: $(openssl rand -base64 16)" \
  "https://netsendo.example.com/app/YOUR_REVERB_APP_KEY?protocol=7&client=js&version=8.4.0"
# Expected: HTTP/1.1 101 Switching Protocols, then a pusher:connection_established message

# 3. What listens where (run on the server)
ss -tlnp | grep -E ':(80|443|5029|8085)\b'

# 4. From another machine: these must fail
nc -zv -w 3 203.0.113.10 5029
nc -zv -w 3 203.0.113.10 8085
ℹ️ Note: ufw status does not tell you whether a Docker-published port is reachable, because Docker's rules are evaluated before ufw's. Always test from outside, or check the address in docker ps (it must show 127.0.0.1:5029->80/tcp, not 0.0.0.0:5029->80/tcp).

What do common WebSocket proxy errors mean?

Most failures come from one of five mismatches: wrong path, missing upgrade headers, wrong port, wrong scheme in the browser, or a timeout. The table maps symptoms to causes.

SymptomLikely causeFix
Console: WebSocket connection to 'wss://…/app/…' failed, nginx access log shows 404 for /app/Request reached the app instead of Reverb, or proxy_pass has a trailing slashAdd the /app/ location; use proxy_pass http://127.0.0.1:8085; without a URI
Handshake returns a status other than 101Upgrade/Connection headers not forwardedAdd both headers and proxy_http_version 1.1
502 Bad Gateway on /app/Reverb not running, or the proxy points at 8080 while Reverb listens on 8085docker compose ps, docker compose logs reverb, align ports
101, then pusher:error code 4001Browser uses a different app key than the Reverb serverMake VITE_REVERB_APP_KEY equal REVERB_APP_KEY
Browser tries ws://…:8085 from an HTTPS pageBrowser settings still localHost = domain, port 443, scheme https; rebuild if values are bundled
Connection drops and reconnects every minuteProxy read timeout (nginx default 60 s) or a load balancer idle timeoutRaise proxy_read_timeout; check the LB/CDN idle timeout
All WebSockets reconnect after a config reloadCaddy closes streams on reload by defaultstream_close_delay

As of December 2026.

When the cause is not obvious, read both sides at once: tail -f /var/log/nginx/error.log on the host and docker compose logs -f reverb in the project folder, then reload the page.

Doing it with NetSendo

NetSendo's docker-compose.yml already publishes the web container on 127.0.0.1:5029 and the database on 127.0.0.1, and its installation guide and DOCKER_INSTALL.md contain an nginx example with a /app/ location for Reverb on 8085 (checked against version 2.13.0, 30 November 2026). Two details differ from a generic Laravel app:

  • Reverb is published on all interfaces by default (0.0.0.0:8085), so it works without a proxy. Once your proxy forwards /app/, set REVERB_BIND_ADDRESS=127.0.0.1 in .env and run docker compose up -d to close the direct port. If 8085 is taken on the host, change REVERB_HOST_PORT, never REVERB_PORT, which the app containers use inside the Docker network.
  • No frontend rebuild is needed. Since 2.7.1 the browser receives the VITE_REVERB_* values from the server on each page load. Set VITE_REVERB_HOST to your domain, VITE_REVERB_PORT=443 and VITE_REVERB_SCHEME=https, then run docker compose up -d. Do not run npm run build in the production image: it fails there, because the image removes node_modules after building the assets.

Set APP_URL=https://your-domain; NetSendo derives the session domain and the secure-cookie flag from it, and it trusts forwarded headers from any proxy, which is safe as long as port 5029 stays on 127.0.0.1. Reverb powers real-time features such as Live Visitors, so a broken /app/ location shows up as live views that never update and a failed wss:// request in the browser console. The configuration reference lists the other variables, and if you are still choosing a tool, our comparison of self-hosted email marketing software covers what each one needs to run. Before exposing any instance, also set up the backups described in our backup and restore guide.

🎯 Expert tips

1
Change one layer at a time

Get HTTPS working for pages first, then add the WebSocket location, then close the direct ports. When everything changes at once, a 404 from Reverb and a redirect loop look the same in the browser.

2
Test the handshake with curl before the browser

A 101 from the curl command proves the proxy is right. If curl works and the browser does not, the problem is in the browser-side host, port or scheme, not in nginx.

3
Keep a CDN out of the WebSocket path at first

CDNs add their own idle timeouts and SSL modes. Get the origin working with DNS-only records, then enable the proxy layer and test again.

4
Raise file limits before you have many connections

Every open WebSocket is a file descriptor. The Reverb docs recommend checking ulimit -n and note that the default event loop is limited to about 1,024 open files, so installs with more than 1,000 concurrent connections need the ext-uv event loop.

Frequently asked questions

How do I proxy WebSockets with nginx?

Add a location for the WebSocket path that sets proxy_http_version 1.1, passes Upgrade $http_upgrade and a Connection header of upgrade, and raises proxy_read_timeout above the 60-second default. Without the Upgrade and Connection headers the handshake never returns 101 Switching Protocols.

Why does my WebSocket connection fail behind a reverse proxy?

Usually one of five things: the request reaches the app instead of the WebSocket server, the upgrade headers are not forwarded, the proxy points at the wrong port, the browser still uses ws:// and the old port, or an idle timeout closes the connection. A curl handshake test that expects HTTP 101 tells you which side is wrong.

Does Caddy need special configuration for WebSockets?

No. Caddy's reverse_proxy performs the WebSocket upgrade automatically and also sets X-Forwarded-For, X-Forwarded-Proto and X-Forwarded-Host. Consider stream_close_delay so connections are not all dropped when the configuration reloads.

Which port should Laravel Echo use behind an HTTPS proxy?

Port 443 with the https scheme, so the browser opens wss:// to your domain and the proxy forwards it to Reverb. The server-side REVERB_HOST and REVERB_PORT stay on the internal address, for example reverb and 8085 in Docker.

Does ufw protect ports published by Docker?

No. Docker adds its own packet-filter rules and traffic to published ports is diverted before ufw's rules apply. Publish ports on 127.0.0.1 instead, for example 127.0.0.1:5029:80, and test from another machine.

Do I need to rebuild the frontend after changing VITE_REVERB variables?

In a standard Laravel app yes, because Vite writes VITE_ values into the JavaScript bundle at build time, so run npm run build again. Apps that pass these values from the server at page load, such as NetSendo since 2.7.1, only need a restart.

Sources

Run NetSendo behind your own proxy

NetSendo ships with its ports on 127.0.0.1 and a documented nginx setup for HTTPS and Reverb, so you only add the certificate and your domain.

#nginx#caddy#docker#websockets#laravel#https
Share