Nginx Cheat Sheet
Nginx is a web server which can also be used as a reverse proxy, load balancer, mail
proxy and HTTP cache.
by lam
󰅂General Settings
Port (listen)
server {
# standard HTTP proto
col
listen 80;
# standard HTTPS prot
ocol
listen 443 ssl;
# listen on 80 using
IPv6
listen [::]:80;
# listen only on IPv6
listen [::]:80 ipv6on
ly=on;
}
Domain name (server_name)
server {
# Listen to yourdomai
n.com
server_name yourdomai
n.com;
# Listen to multiple
domains
server_name yourdomai
n.com www.yourdomain.co
m;
# Listen to all sub-d
omains
server_name *.yourdom
ain.com;
# Listen to all top-l
evel domains
server_name yourdomai
n.*;
# Listen to unspecifi
ed hostnames (listens t
o IP address itself)
󰅂Serving Files
Static assets
server {
listen 80;
server_name yourdomai
n.com;
location / {
root /path/to/w
ebsite;
}
}
Static assets with HTML5
History Mode
server {
listen 80;
server_name yourdomai
n.com;
root /path/to/websit
e;
location / {
try_files $uri
$uri/ /index.html;
}
}
󰅂Redirects
301 Permanent
Useful for handling
www.yourdomain.com vs.
yourdomain.com or
redirecting http to https. In
this case we will redirect
www.yourdomain.com to
yourdomain.com.
server {
listen 80;
server_name www.yourd
omain.com;
return 301 http://you
rdomain.com$request_ur
i;
}
302 Temporary
server {
listen 80;
server_name yourdomai
n.com;
return 302 http://oth
erdomain.com;
}
Redirect on speci c URL
Can be permanent (301) or
temporary (302).
server {
listen 80;
server_name yourdomai
n.com;
location /redirect-ur
l {
return 301 htt
p://otherdomain.com;
}
}
󰅂Reverse Proxy
Basic
server {
listen 80;
server_name yourdomai
n.com;
location / {
proxy_pass http://
0.0.0.0:3000;
# where 0.0.0.0:300
0 is your Node.js Serve
r bound on 0.0.0.0 list
ing on port 3000
}
}
󰅂TLS/SSL (HTTPS)
󰏪
󰅢
server_name "";
}
Access Logging (access_log)
server {
# Relative or full pa
th to log file
access_log /path/to/f
ile.log;
# Turn 'on' or 'off'
access_log on;
}
Miscellaneous (gzip,
client_max_body_size)
server {
# Turn gzip compressi
on 'on' or 'off'
gzip on;
# Limit client body s
ize to 10mb
client_max_body_size
10M;
}
Basic+
upstream node_js {
server 0.0.0.0:3000;
# where 0.0.0.0:3000 i
s your Node.js Server b
ound on 0.0.0.0 listin
g on port 3000
}
server {
listen 80;
server_name yourdomai
n.com;
location / {
proxy_pass http://no
de_js;
}
}
Upgraded Connection
(Recommended for Node.js
Applications)
Useful for Node.js
applications with support for
WebSockets like socket.io.
upstream node_js {
server 0.0.0.0:3000;
}
server {
listen 80;
server_name yourdomai
n.com;
location / {
proxy_pass http://n
ode_js;
proxy_redirect off;
proxy_http_version
1.1;
proxy_set_header Up
grade $http_upgrade;
proxy_set_header Co
nnection "upgrade";
proxy_set_header Ho
st $host;
# not required but
useful for application
s with heavy WebSocket
usage
# as it increases t
he default timeout conf
If you are looking for free
SSL certi cates, Let's
Encrypt is a free,
automated, and open
Certi cate Authority. Also,
here is a wonderful step-by-
step guide from Digital
Ocean on how to setup
TLS/SSL on Ubuntu 16.04.
server {
listen 443 ssl;
server_name yourdoma
in.com;
ssl on;
ssl_certificate /pat
h/to/cert.pem;
ssl_certificate_key
/path/to/privkey.pem;
ssl_stapling on;
ssl_stapling_verify
on;
ssl_trusted_certific
ate /path/to/fullchai
n.pem;
ssl_protocols TLSv1
TLSv1.1 TLSv1.2;
ssl_session_timeout
1d;
ssl_session_cache sh
ared:SSL:50m;
add_header Strict-Tr
ansport-Security max-a
ge=15768000;
}
# Permanent redirect f
or HTTP to HTTPS
server {
listen 80;
server_name yourdoma
in.com;
return 301 https://
$host$request_uri;
}
󰅂
Large Scale
Applications
Load Balancing
upstream node_js {
server 0.0.0.0:3000;
server 0.0.0.0:4000;
server 123.131.121.12
2;
}
server {
listen 80;
server_name yourdomai
n.com;
location / {
proxy_pass http://n
ode_js;
}
}
󰏪
󰅢
CheatSheetMaker.com SimpleCheatSheet.com
iguration of 60
proxy_read_timeout
80;
}
}
󰏪
󰅢

Nginx cheat sheet

  • 1.
    Nginx Cheat Sheet Nginxis a web server which can also be used as a reverse proxy, load balancer, mail proxy and HTTP cache. by lam 󰅂General Settings Port (listen) server { # standard HTTP proto col listen 80; # standard HTTPS prot ocol listen 443 ssl; # listen on 80 using IPv6 listen [::]:80; # listen only on IPv6 listen [::]:80 ipv6on ly=on; } Domain name (server_name) server { # Listen to yourdomai n.com server_name yourdomai n.com; # Listen to multiple domains server_name yourdomai n.com www.yourdomain.co m; # Listen to all sub-d omains server_name *.yourdom ain.com; # Listen to all top-l evel domains server_name yourdomai n.*; # Listen to unspecifi ed hostnames (listens t o IP address itself) 󰅂Serving Files Static assets server { listen 80; server_name yourdomai n.com; location / { root /path/to/w ebsite; } } Static assets with HTML5 History Mode server { listen 80; server_name yourdomai n.com; root /path/to/websit e; location / { try_files $uri $uri/ /index.html; } } 󰅂Redirects 301 Permanent Useful for handling www.yourdomain.com vs. yourdomain.com or redirecting http to https. In this case we will redirect www.yourdomain.com to yourdomain.com. server { listen 80; server_name www.yourd omain.com; return 301 http://you rdomain.com$request_ur i; } 302 Temporary server { listen 80; server_name yourdomai n.com; return 302 http://oth erdomain.com; } Redirect on speci c URL Can be permanent (301) or temporary (302). server { listen 80; server_name yourdomai n.com; location /redirect-ur l { return 301 htt p://otherdomain.com; } } 󰅂Reverse Proxy Basic server { listen 80; server_name yourdomai n.com; location / { proxy_pass http:// 0.0.0.0:3000; # where 0.0.0.0:300 0 is your Node.js Serve r bound on 0.0.0.0 list ing on port 3000 } } 󰅂TLS/SSL (HTTPS) 󰏪 󰅢
  • 2.
    server_name ""; } Access Logging(access_log) server { # Relative or full pa th to log file access_log /path/to/f ile.log; # Turn 'on' or 'off' access_log on; } Miscellaneous (gzip, client_max_body_size) server { # Turn gzip compressi on 'on' or 'off' gzip on; # Limit client body s ize to 10mb client_max_body_size 10M; } Basic+ upstream node_js { server 0.0.0.0:3000; # where 0.0.0.0:3000 i s your Node.js Server b ound on 0.0.0.0 listin g on port 3000 } server { listen 80; server_name yourdomai n.com; location / { proxy_pass http://no de_js; } } Upgraded Connection (Recommended for Node.js Applications) Useful for Node.js applications with support for WebSockets like socket.io. upstream node_js { server 0.0.0.0:3000; } server { listen 80; server_name yourdomai n.com; location / { proxy_pass http://n ode_js; proxy_redirect off; proxy_http_version 1.1; proxy_set_header Up grade $http_upgrade; proxy_set_header Co nnection "upgrade"; proxy_set_header Ho st $host; # not required but useful for application s with heavy WebSocket usage # as it increases t he default timeout conf If you are looking for free SSL certi cates, Let's Encrypt is a free, automated, and open Certi cate Authority. Also, here is a wonderful step-by- step guide from Digital Ocean on how to setup TLS/SSL on Ubuntu 16.04. server { listen 443 ssl; server_name yourdoma in.com; ssl on; ssl_certificate /pat h/to/cert.pem; ssl_certificate_key /path/to/privkey.pem; ssl_stapling on; ssl_stapling_verify on; ssl_trusted_certific ate /path/to/fullchai n.pem; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_session_timeout 1d; ssl_session_cache sh ared:SSL:50m; add_header Strict-Tr ansport-Security max-a ge=15768000; } # Permanent redirect f or HTTP to HTTPS server { listen 80; server_name yourdoma in.com; return 301 https:// $host$request_uri; } 󰅂 Large Scale Applications Load Balancing upstream node_js { server 0.0.0.0:3000; server 0.0.0.0:4000; server 123.131.121.12 2; } server { listen 80; server_name yourdomai n.com; location / { proxy_pass http://n ode_js; } } 󰏪 󰅢
  • 3.
    CheatSheetMaker.com SimpleCheatSheet.com iguration of60 proxy_read_timeout 80; } } 󰏪 󰅢