Skip to content

Instantly share code, notes, and snippets.

@pasupulaphani
Created December 1, 2013 23:38
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save pasupulaphani/7742371 to your computer and use it in GitHub Desktop.
Save pasupulaphani/7742371 to your computer and use it in GitHub Desktop.
Configure Nginx: Load balancing and reverse proxy
### host file under sites-available ###
# The upstream module is the link between Node.js and Nginx.
# Upstream is used for proxying requests to other servers.
# All requests for / get distributed between any of the servers listed.
upstream app_nodeapp1 {
# Set up multiple Node.js webservers for Load balancing.
# max_fails refers to number of failed attempts
# before server is considered inactive.
# weight priorities traffic to server. Ex. weight=2 will recieve
# twice as much traffic as server with weight=1
server 127.0.0.1:3000 max_fails=0 fail_timeout=10s weight=1;
# server <your server ip>:3001 max_fails=0 fail_timeout=10s weight=1;
# server <your server ip>:3002 max_fails=0 fail_timeout=10s weight=1;
# server <your server ip>:3003 max_fails=0 fail_timeout=10s weight=1;
# Send visitors back to the same server each time.
ip_hash;
# Enable number of keep-alive connections.
keepalive 512;
}
server {
listen 80;
listen [::]:80 default_server ipv6only=on;
# Index files.
index index.html;
# Domain names.
# Make sure to set the A Record on your domain's DNS settings to your server's IP address.
# You can test if was set properly by using the `dig` command: dig yourdomain.com
server_name nodeapp1.com www.nodeapp1.com;
# Timeout for closing keep-alive connections.
keepalive_timeout 10;
# Enable gzip compression.
gzip on;
gzip_http_version 1.1;
gzip_vary on;
gzip_comp_level 6;
gzip_proxied any;
gzip_buffers 16 8k;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
# Max upload size.
# client_max_body_size 16M;
# Change access and error log files.
# access_log /var/log/nginx/yourdomain.com/access.log;
# error_log /var/log/nginx/yourdomain.com/error.log;
# Custom error page.
# error_page 404 maintenance.html;
# error_page 500 502 503 504 maintenance.html;
# location /maintenance.html {
# root /var/www;
# }
location / {
# Set this to your upstream module.
proxy_pass http://app_nodeapp1;
# Proxy headers.
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_set_header X-NginX-Proxy true;
# To handle websocket requests
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_cache_bypass $http_upgrade;
proxy_http_version 1.1;
proxy_redirect off;
# Go to next upstream after if server down.
proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
proxy_connect_timeout 5s;
# Gateway timeout.
proxy_read_timeout 20s;
proxy_send_timeout 20s;
# Buffer settings.
proxy_buffers 8 32k;
proxy_buffer_size 64k;
}
# Enable caching of static files.
# location ~* \.(css|js|gif|jpe?g|png)$ {
# expires 168h;
# add_header Pragma public;
# add_header Cache-Control "public, must-revalidate, proxy-revalidate";
# }
# Don't cache html files.
# location ~* \.html$ {
# expires -1;
# }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment