Skip to content

Instantly share code, notes, and snippets.

@miguelmota
Last active January 30, 2023 02:28
Show Gist options
  • Save miguelmota/6912559 to your computer and use it in GitHub Desktop.
Save miguelmota/6912559 to your computer and use it in GitHub Desktop.
Nginx + Node.js server configuration. Blog post: http://www.miguelmota.com/blog/nodejs-and-ngnix-on-ubuntu/
# 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 helloworld {
# 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 <your server ip>: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 yourdomain.com www.yourdomain.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://helloworld;
# Headers to pass to proxy server.
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;
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|ico|gif|jpe?g|png|svg)$ {
# expires 168h;
# add_header Pragma public;
# add_header Cache-Control "public, must-revalidate, proxy-revalidate";
# }
# Don't cache html files.
# location ~* \.html$ {
# expires -1;
# }
# Serve static files without going through upstreams
location ~ ^/(images/|img/|javascript/|js/|css/|stylesheets/|flash/|media/|static/|robots.txt|humans.txt|favicon.ico) {
root /var/www/helloworld;
access_log off;
expires 1h;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment