Nginx and PHP-FPM Configuration
Set up Nginx as a reverse proxy to PHP-FPM for serving PHP applications.
Nginx and PHP-FPM Configuration is a free PHP Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Nginx + PHP-FPM?
Nginx is a high-performance web server and reverse proxy. PHP-FPM (FastCGI Process Manager) manages a pool of PHP worker processes. Nginx passes PHP requests to FPM via FastCGI.
Basic Nginx Server Block
A minimal Nginx configuration for a PHP application.
# /etc/nginx/sites-available/myapp
server {
listen 80;
server_name example.com;
root /srv/myapp/public;
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
}
location ~ /\.ht {
deny all;
}
}try_files Directive
try_files $uri $uri/ /index.php?$query_string attempts to serve a static file first. If not found, it falls back to index.php — enabling front-controller routing (Laravel, Symfony).
PHP-FPM Pool Configuration
Configure the worker pool in /etc/php/8.3/fpm/pool.d/www.conf.
[www]
pm = dynamic
pm.max_children = 20
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8
pm.max_requests = 500 ; recycle workers to prevent leaksUnix Socket vs TCP
Using a Unix socket (fastcgi_pass unix:/run/php/php8.3-fpm.sock) is faster than TCP (127.0.0.1:9000) for same-machine communication.
HTTPS with Let's Encrypt
Use Certbot to obtain free TLS certificates.
$ certbot --nginx -d example.com
# Certbot modifies your Nginx config and sets up auto-renewalGzip Compression
Enable gzip to reduce response sizes.
# nginx.conf or server block:
gzip on;
gzip_types text/plain text/css application/javascript application/json;
gzip_min_length 1024;Browser Caching for Static Assets
Add cache-control headers for static files.
location ~* \.(js|css|png|jpg|gif|ico|woff2)$ {
expires 1y;
add_header Cache-Control "public, immutable";
}Rate Limiting in Nginx
Limit requests per IP using the limit_req module — first line of defence before PHP even starts.
http {
limit_req_zone $binary_remote_addr zone=one:10m rate=10r/s;
}
server {
location /api/ {
limit_req zone=one burst=20 nodelay;
}
}Security Headers
Add security headers to all responses.
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
add_header Referrer-Policy "strict-origin-when-cross-origin";Reload vs Restart
After changing Nginx config, reload gracefully (no downtime): nginx -t && nginx -s reload. After changing PHP-FPM config: systemctl reload php8.3-fpm.
Summary
Configure Nginx with a server block pointing to public/index.php via FastCGI. Tune PHP-FPM pool settings. Use Unix sockets. Enable HTTPS, gzip, and static file caching.
Quick Check
What does try_files $uri $uri/ /index.php?$query_string do?
Frequently asked questions
Is the “Nginx and PHP-FPM Configuration” lesson free?
Yes — the full text of “Nginx and PHP-FPM Configuration” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.
What will I learn in “Nginx and PHP-FPM Configuration”?
Set up Nginx as a reverse proxy to PHP-FPM for serving PHP applications. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start PHP Academy?
No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Nginx and PHP-FPM Configuration” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this PHP Academy lesson?
Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Nginx and PHP-FPM Configuration
- Environment Variables and .env Files
- Deploying with GitHub Actions
- Dockerizing a PHP Application