Nginx Reverse Proxy Builder · PHP / Laravel Octane

Nginx Reverse Proxy Config for Laravel Octane & Reverb WebSockets

Generate an Nginx reverse proxy server block for PHP / Laravel Octane with URL prefix rewrite previews, Certbot SSL redirects, WebSocket upgrade headers, and Docker DNS checks.

Written & verified by Chathura Prabashana · Updated November 2026

1. Adjust stack settings or load a test scenario2. Inspect live diagnostics3. Copy generated config
Quick-Test Scenarios:

1. Domain, Routing & Upstream

2. TLS, WebSockets & Directives

# /etc/nginx/sites-available/api.laravelapp.com.conf
# Generated by OpsCheck DevTools — Validate with: sudo nginx -t && sudo systemctl reload nginx

# Required in http {} context (or top of sites-available file) for clean WebSocket upgrades
map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}

server {
    listen 80;
    listen [::]:80;
    server_name api.laravelapp.com;

    # Redirect all plaintext HTTP traffic permanently to HTTPS
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name api.laravelapp.com;

    # Let's Encrypt Certbot TLS Certificates
    ssl_certificate     /etc/letsencrypt/live/api.laravelapp.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/api.laravelapp.com/privkey.pem;
    ssl_protocols       TLSv1.2 TLSv1.3;
    ssl_ciphers         HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;

    client_max_body_size 50M;

    # Gzip compression for JSON, JS, CSS, and SVG responses
    gzip on;
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 5;
    gzip_min_length 1024;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml image/svg+xml;

    # Baseline HTTP Security Headers
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header Referrer-Policy "strict-origin-when-cross-origin" always;
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;

    location / {
        proxy_pass http://127.0.0.1:8000;
        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;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Port $server_port;

        # WebSocket & Upgrade support (requires map $http_upgrade $connection_upgrade)
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_cache_bypass $http_upgrade;

        proxy_connect_timeout 15s;
        proxy_send_timeout 3600s;
        proxy_read_timeout 3600s;
    }
}
$sudo nginx -t && sudo systemctl reload nginx
URL Routing TransformationPrefix Preserved
GET https://api.laravelapp.com/usershttp://127.0.0.1:8000/users

Root location '/' forwards '/users' unchanged to upstream http://127.0.0.1:8000/users.

Configuration Diagnostics

  • infoRoot Proxy Path (/) Preserves Full Request URI

    GET https://api.laravelapp.com/users -> Upstream receives http://127.0.0.1:8000/users. Root location '/' forwards '/users' unchanged to upstream http://127.0.0.1:8000/users.

  • passWebSocket Upgrade & Extended Timeout (3600s) Enabled

    Configured HTTP/1.1 Upgrade, Connection $connection_upgrade map, and extended proxy_read_timeout for persistent real-time sockets.

  • infoSingle-Layer CORS Policy Reminder

    This configuration intentionally omits Access-Control-Allow-Origin headers so your upstream backend framework (Laravel, FastAPI, Express, ASP.NET Core) owns CORS cleanly without duplicate header rejections.

Runs locally in your browser. Domain names and upstream topology are never sent to a server.

Common Nginx Error Log Pattern (PHP / Laravel Octane)

When upstream sockets, timeouts, body limits, or Docker DNS rules are misconfigured for PHP / Laravel Octane, Nginx typically records an entry matching the log line below inside /var/log/nginx/error.log:

/var/log/nginx/error.log

2026/11/22 16:04:11 [error] 3190#3190: *1205 upstream prematurely closed connection while reading upstream, client: 192.0.2.88, server: api.laravelapp.com, request: "GET /app/reverb-app-key?protocol=7&client=js HTTP/1.1", upstream: "http://127.0.0.1:8080/app/reverb-app-key", host: "api.laravelapp.com"

Reverse Proxy Architecture & Header Forwarding for PHP / Laravel Octane

Modern high-throughput Laravel 11 and 12 deployments increasingly replace traditional PHP-FPM with Laravel Octane (backed by FrankenPHP, Swoole, or RoadRunner on port 8000) paired with Laravel Reverb; Laravel's first-party WebSocket server listening on port 8080. Because both Octane and Reverb are long-running HTTP/WebSocket daemons, Nginx sits in front as an HTTP/1.1 reverse proxy rather than a FastCGI gateway.

Running Octane and Reverb behind a single HTTPS domain requires splitting traffic cleanly by location path while serving static public/ assets directly from disk. Regular application routes proxy to Octane on 127.0.0.1:8000, whereas Reverb's Pusher-compatible WebSocket handshake endpoint (/app) and HTTP broadcast trigger API (/apps) proxy to 127.0.0.1:8080 with Upgrade headers and an extended 3600-second read timeout.

A classic Laravel production bug occurs when TrustProxies is left unconfigured after putting Nginx TLS in front of Octane: signed URLs (URL::temporarySignedRoute), pagination links, and Sanctum secure cookies break because Laravel inspects the plain HTTP loopback connection instead of Nginx's X-Forwarded-Proto: https header. Configuring $middleware->trustProxies(at: '*') or trusting Nginx's exact IP in bootstrap/app.php resolves scheme detection across every route.

Separating static public assets via try_files $uri $uri/ @octane ensures Nginx serves CSS, JS, and uploaded storage images at C speed without occupying Octane PHP workers, while clean proxy_set_header rules keep Laravel Echo WebSocket connections rock-solid across mobile networks and corporate proxies.

In production Laravel Octane and Reverb environments, separate your Nginx access and error log streams or include $upstream_addr in your log format so you can immediately distinguish HTTP API latency on Octane port 8000 from WebSocket churn on Reverb port 8080. Pair Nginx reload safety (sudo nginx -t && sudo systemctl reload nginx) with php artisan octane:reload during deployments so active Reverb WebSocket subscribers stay connected while Octane PHP workers pick up new release code cleanly.

Step-by-Step Nginx & PHP / Laravel Octane Deployment Guide

Apply the Nginx server block above alongside these upstream framework trust-proxy settings so HTTPS detection, real client IPs, and reload safety work reliably in production.

  1. 1Route Static Assets, Octane (8000), and Reverb WebSockets (8080) in Nginx

    /etc/nginx/sites-available/api.laravelapp.com.conf

    Combine the generated HTTPS server block with a @octane fallback for Laravel public files and dedicated /app and /apps location blocks pointing to Laravel Reverb on port 8080 with WebSocket Upgrade headers.

    /etc/nginx/sites-available/api.laravelapp.com.confnginx
    root /var/www/laravel/public;
    
    location / {
        try_files $uri $uri/ @octane;
    }
    
    location @octane {
        proxy_pass http://127.0.0.1:8000;
        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;
    }
    
    # Laravel Reverb WebSocket Server (port 8080)
    location /app {
        proxy_pass http://127.0.0.1:8080;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_read_timeout 3600s;
        proxy_send_timeout 3600s;
    }
  2. 2Configure TrustProxies in Laravel 11/12 Bootstrap

    bootstrap/app.php

    Tell Laravel to trust Nginx's X-Forwarded-* headers so request()->secure(), signed verification emails, and Sanctum cookies recognize HTTPS accurately behind Nginx TLS termination.

    bootstrap/app.phpphp
    <?php
    
    use Illuminate\Foundation\Application;
    use Illuminate\Foundation\Configuration\Middleware;
    use Illuminate\Http\Request;
    
    return Application::configure(basePath: dirname(__DIR__))
        ->withMiddleware(function (Middleware $middleware) {
            $middleware->trustProxies(
                at: ['127.0.0.1'],
                headers: Request::HEADER_X_FORWARDED_FOR |
                         Request::HEADER_X_FORWARDED_HOST |
                         Request::HEADER_X_FORWARDED_PORT |
                         Request::HEADER_X_FORWARDED_PROTO
            );
        })
        ->create();
  3. 3Align Laravel Reverb & Echo Environment Variables for HTTPS Port 443

    .env

    Run Reverb internally on HTTP port 8080 while instructing frontend Laravel Echo clients to connect via WSS on port 443 through Nginx; never expose port 8080 directly to the internet.

    .envdotenv
    REVERB_SERVER_HOST=127.0.0.1
    REVERB_SERVER_PORT=8080
    REVERB_HOST=api.laravelapp.com
    REVERB_PORT=443
    REVERB_SCHEME=https
    OCTANE_HTTPS=true
  4. 4Supervise Octane and Reverb Workers With Supervisor and Reload Nginx

    terminal

    Keep both Octane and Reverb monitored under Supervisor (or systemd) with clean reload hooks, then validate Nginx syntax before reloading.

    terminalbash
    php artisan octane:start --server=frankenphp --host=127.0.0.1 --port=8000
    php artisan reverb:start --host=127.0.0.1 --port=8080
    sudo nginx -t && sudo systemctl reload nginx

Common PHP / Laravel Octane Nginx Reverse Proxy Pitfalls

Review these three production failure modes before reloading Nginx on staging or live servers.

Pitfall 1

Reverb /app Path Colliding With a Laravel Application Route Named /app

Because Reverb uses /app/{appKey} for WebSocket handshakes, if your Laravel web app already serves a SPA or dashboard under /app, Nginx sends dashboard page loads to Reverb (returning 404). Host Reverb on a dedicated subdomain like ws.laravelapp.com when /app is already taken.

Pitfall 2

Forgetting /apps Location Block for Server-Side Broadcast Triggers

If another microservice triggers Reverb broadcasts over your public domain, Reverb listens on /apps/{appId}/events. If you only proxy /app without /apps, WebSocket clients connect fine while external HTTP broadcast triggers fail with 404.

Pitfall 3

Leaving proxy_read_timeout at 60s for Laravel Echo Connections

Idle Laravel Echo sockets get severed by Nginx every 60 seconds unless proxy_read_timeout is raised to 3600s or Reverb's ping_interval stays well below Nginx's timeout threshold.

Related Tools

Pair this Nginx reverse proxy block with healthcheck-gated Docker Compose orchestration and verified browser CORS headers:

Frequently Asked Questions

Do I still need PHP-FPM when running Laravel Octane behind Nginx?
No. Octane boots your Laravel application once in memory via FrankenPHP, Swoole, or RoadRunner and speaks HTTP directly to Nginx via proxy_pass, completely replacing fastcgi_pass and PHP-FPM for that site.
Why do Laravel signed routes return 403 Invalid Signature behind Nginx?
Laravel generates signed URLs using the incoming scheme and host. If TrustProxies is not configured in bootstrap/app.php or Nginx does not forward X-Forwarded-Proto $scheme and Host $host, Laravel validates an https:// signature against an http:// request URL and rejects it with 403.
Should Reverb run on a separate subdomain like ws.example.com?
Using a dedicated subdomain like ws.example.com avoids any chance of /app route collisions with your main Laravel application and allows independent connection limits and file-descriptor tuning in Nginx.
How many concurrent WebSocket connections can Nginx proxy to Laravel Reverb?
Each proxied WebSocket uses two file descriptors (client + upstream). Increase worker_connections (e.g., 16384) and worker_rlimit_nofile in /etc/nginx/nginx.conf alongside ulimit -n on your Reverb Supervisor process to support thousands of concurrent sockets.
Browse all 10 Nginx guides →