Skip to content

Instantly share code, notes, and snippets.

@frozenminds
Last active September 20, 2016 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frozenminds/3488da5416cc83bcaa6a44bfc079c0b9 to your computer and use it in GitHub Desktop.
Save frozenminds/3488da5416cc83bcaa6a44bfc079c0b9 to your computer and use it in GitHub Desktop.
Nginx Load Balancing
user www-data;
worker_processes auto;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type text/html;
log_format loadbalancer 'Request "$request" - Upstream [Addr: $upstream_addr $upstream_http_name, Status: $upstream_status]';
index index.html index.htm index.php;
charset utf-8;
gzip on;
# upstream backend
upstream backend {
zone backend 64k;
server backend1.dev:8081;
server backend2.dev:8082;
server backend3.dev:8083;
server backup1.dev:8084 backup;
}
# load balancer
server {
listen 80;
server_name lb.dev;
access_log /var/log/nginx/lb.access.log loadbalancer;
proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
proxy_next_upstream_timeout 2s;
proxy_read_timeout 2s;
proxy_connect_timeout 2s;
location /upstream_conf {
upstream_conf;
}
location = /status.html {
root /usr/share/nginx/html;
}
location = /status {
status;
}
location / {
# Rewrite the 'Host' header to the value in the client request or primary server name
proxy_set_header Host $host;
proxy_pass http://backend;
health_check interval=3s fails=1 passes=3 uri=/ping match=statusok;
}
# static
location ~* \.(css|js|ico|png|gif|jpg|jpeg|svg|eot|ttf|woff|woff2)$ {
access_log off;
}
}
match statusok {
status 200;
}
# backend servers
# simulate custom configs, take down/up, timeout, etc.
server {
listen 8081;
server_name backend1.dev;
location ~ ^/(status|ping)$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location / {
root /var/www/html;
index index.php;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
}
server {
listen 8082;
server_name backend2.dev;
location ~ ^/(status|ping)$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location / {
root /var/www/html;
index index.php;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
}
server {
listen 8083;
server_name backend3.dev;
location ~ ^/(status|ping)$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location / {
root /var/www/html;
index index.php;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
}
server {
listen 8084;
server_name backup1.dev;
location ~ ^/(status|ping)$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location / {
root /var/www/html;
index index.php;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment