[NGINX] loadbalance php‐fpm - fourslickz/notes GitHub Wiki
# Upstream block: Load balancing for multiple PHP-FPM instances
upstream php_backend {
least_conn; # Use the least connections load-balancing method
server 192.168.1.20:9000; # PHP-FPM VM1
server 192.168.1.30:9000; # PHP-FPM VM2
server 192.168.1.40:9000; # PHP-FPM VM3
}
# Server block for the website
server {
listen 80;
server_name example.com www.example.com;
# Document root for your site
root /var/www/example;
index index.php index.html;
# Access and error logs
access_log /var/log/nginx/example_access.log;
error_log /var/log/nginx/example_error.log;
# Location block for serving static files
location / {
try_files $uri $uri/ =404;
}
# Location block for handling PHP files
location ~ \.php$ {
include fastcgi_params; # Include default FastCGI params
fastcgi_pass php_backend; # Use the upstream block for PHP
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_index index.php;
}
# Security and performance optimizations
location ~ /\.ht {
deny all;
}
# Enable Gzip compression (optional)
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
gzip_proxied any;
gzip_comp_level 5;
}