Skip to the content.

Nginx Reverse Proxy Setup

This guide explains how to run the Laravel backend over HTTPS behind Nginx while the app server itself (php artisan serve) listens on a localhost port. The sample config terminates TLS at Nginx and proxies traffic to the backend on 127.0.0.1:8000.

Prerequisites

File Locations

Replace paths with your own in production. Never commit private keys publicly.

Example Nginx Server Block

server {
  listen 8222 ssl;
  listen [::]:8222 ssl;
  server_name $IP_ADDRESS $ANOTHER_IP_ADDRESS ;

  ssl_certificate     /PATH/TO/$PROJECT_NAME/backend/certs/dev.crt;
  ssl_certificate_key /PATH/TO/$PROJECT_NAME/backend/certs/dev.key;

  # Stronger TLS (optional, adjust to your needs)
  ssl_protocols TLSv1.2 TLSv1.3;
  ssl_ciphers HIGH:!aNULL:!MD5;
  ssl_prefer_server_ciphers on;

  # Redirect well-known HTTP to HTTPS (optional separate :80 server)
  # server {
  #   listen 80;
  #   server_name $IP_ADDRESS $ANOTHER_IP_ADDRESS ;
  #   return 301 https://$host:8222$request_uri;
  # }

  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 https;
    proxy_set_header   X-Forwarded-Host $host;
    proxy_set_header   X-Forwarded-Port $server_port;

    # Websockets/support for SSE if used
    proxy_set_header   Upgrade $http_upgrade;
    proxy_set_header   Connection $connection_upgrade;

    # Timeouts (tune as necessary)
    proxy_connect_timeout   60s;
    proxy_send_timeout      60s;
    proxy_read_timeout      60s;
  }

  # Static storage served via backend (proxied)
  location /storage/ {
    proxy_pass http://127.0.0.1:8000;
  }
}

Enable the Site

sudo ln -s /etc/nginx/sites-available/$PROJECT_NAME.conf /etc/nginx/sites-enabled/$PROJECT_NAME.conf
sudo nginx -t
sudo systemctl reload nginx

Generate a Self-Signed Certificate (Development)

mkdir -p /var/www/html/cansu/$PROJECT_NAME/backend/certs
openssl req -x509 -nodes -newkey rsa:2048 \
  -keyout /var/www/html/cansu/$PROJECT_NAME/backend/certs/dev.key \
  -out   /var/www/html/cansu/$PROJECT_NAME/backend/certs/dev.crt \
  -days 365 \
  -subj "/CN=$IP_ADDRESS"

Laravel Considerations

Frontend (Quasar) Notes

Troubleshooting

Security Notes