Skip to content

Instantly share code, notes, and snippets.

@anhdiepmmk
Last active January 10, 2020 07:35
Show Gist options
  • Save anhdiepmmk/3e353501233b7a24199abbc8b9614343 to your computer and use it in GitHub Desktop.
Save anhdiepmmk/3e353501233b7a24199abbc8b9614343 to your computer and use it in GitHub Desktop.
The simplest configuration for load balancing with nginx
http {
upstream myapp1 {
server srv1.example.com;
server srv2.example.com;
server srv3.example.com;
}
server {
listen 80;
location / {
proxy_pass http://myapp1;
}
}
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name omg.test;
root /var/www/html/public_html/public;
index index.php;
client_max_body_size 10M;
client_body_buffer_size 10M;
location ~* \.php$ {
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
fastcgi_pass omg_upstream;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
}
##
# `gzip` Settings
#
#
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 9;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_min_length 0;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/vnd.ms-fontobject application/x-font-ttf font/opentype image/svg+xml image/x-icon;
}
upstream omg_upstream {
server services_omg_phpfpm_1:9000;
# server services_omg_phpfpm_2:9000;
# server services_omg_phpfpm_3:9000;
# server services_omg_phpfpm_4:9000;
# server services_omg_phpfpm_5:9000;
}
http {
server {
listen 3000;
server_name io.yourhost.com;
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_pass http://nodes;
# enable WebSockets
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
upstream nodes {
# enable sticky session based on IP
ip_hash;
server app01:3000;
server app02:3000;
server app03:3000;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment