Nginx Configuration for Laravel: Production-Ready Setup
Table of Contents
Nginx serves over 34% of all websites globally and is the recommended web server for Laravel. A properly configured setup handles thousands of concurrent requests efficiently.
Complete Laravel Nginx Config
server {
listen 80;
server_name example.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
server_name example.com;
root /var/www/myapp/public;
index index.php;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml;
gzip_min_length 1000;
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;
location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff2)$ {
expires 30d;
add_header Cache-Control "public, immutable";
}
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\. {
deny all;
}
}
PHP-FPM Pool Tuning
[www]
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
Formula: max_children = (Total RAM - OS RAM) / Average PHP Process Size. A typical process uses 30-50MB.
Rate Limiting
limit_req_zone $binary_remote_addr zone=api:10m rate=10r/s;
location /api/ {
limit_req zone=api burst=20 nodelay;
try_files $uri $uri/ /index.php?$query_string;
}
This setup gives you SSL, compression, caching, security headers, and rate limiting — covering 90% of production needs.
Related Posts
Docker for Developers: From Zero to Containerized Applications
Master Docker fundamentals — images, containers, volumes, and networks — to ship consistent environments every time.
Docker Compose: Orchestrating Multi-Container Applications
Define and run multi-container applications with Docker Compose — databases, caches, queues, and your app in one command.
Kubernetes Fundamentals: Container Orchestration at Scale
Understand Kubernetes core concepts — Pods, Deployments, Services, and Ingress — to run production workloads at any scale.
Comments (0)
No comments yet. Be the first to comment!