Skip to content

Instantly share code, notes, and snippets.

@badsyntax
Last active December 5, 2023 23:10
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save badsyntax/8455064 to your computer and use it in GitHub Desktop.
Save badsyntax/8455064 to your computer and use it in GitHub Desktop.
My basic nginx config files for proxying to a node.js application
server {
listen YOUR_IP_ADDRESS:80 default_server;
root /var/www/nginx;
index index.html index.htm;
# This is just an invalid value which will never trigger on a real hostname.
server_name _;
server_name_in_redirect off;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to index.html
try_files $uri $uri/ /index.html;
}
}
# 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_domain.tld {
server 127.0.0.1:3001 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 YOUR_IP_ADDRESS:80;
# Index files.
index index.html;
server_name domain.tld;
# 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 /home/richard/www/logs/domain.tld-nginx.log;
location / {
# Set this to your upstream module.
proxy_pass http://app_domain.tld;
# 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;
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;
}
# Serve static files without going through upstreams
location ~ ^/(images/|img/|javascript/|js/|css/|stylesheets/|flash/|media/|static/|robots.txt|humans.txt|favicon.ico) {
root /home/richard/www/sites/domain.tld/public;
access_log off;
expires 1h;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment