A quick reference to common server configurations from serving static files to using in congruency with Node.js applications.
Each configuration below is written with minimum requirements for their described function. Please know that real world applications will most likely use a combination of these settings. This cheatsheet is meant to provide a general overview of how to setup specific features of nginx.
These configurations are meant to be used as Name-Based Virtual Hosts, saved within /etc/nginx/sites-enabled.
server {
# standard HTTP protocollisten80;
# standard HTTPS protocollisten443 ssl;
# listen on 80 using IPv6listen [::]:80;
# listen only on IPv6listen [::]:80 ipv6only=on;
}
Domain name (server_name)
server {
# Listen to yourdomain.comserver_name yourdomain.com;
# Listen to multiple domainsserver_name yourdomain.com www.yourdomain.com;
# Listen to all sub-domainsserver_name *.yourdomain.com;
# Listen to all top-level domainsserver_name yourdomain.*;
# Listen to unspecified hostnames (listens to IP address itself)server_name"";
}
Access Logging (access_log)
server {
# Relative or full path to log fileaccess_log /path/to/file.log;
# Turn 'on' or 'off'access_log on;
}
Miscellaneous (gzip, client_max_body_size)
server {
# Turn gzip compression 'on' or 'off'gzip on;
# Limit client body size to 10mbclient_max_body_size10M;
}
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 {
listen80;
server_name www.yourdomain.com;
return301 http://yourdomain.com$request_uri;
}
302 Temporary
server {
listen80;
server_name yourdomain.com;
return302 http://otherdomain.com;
}
server {
listen80;
server_name yourdomain.com;
location/ {
proxy_pass http://0.0.0.0:3000;
# where 0.0.0.0:3000 is your Node.js Server bound on 0.0.0.0 listing on port 3000
}
}
Basic+
upstreamnode_js {
server 0.0.0.0:3000;
# where 0.0.0.0:3000 is your Node.js Server bound on 0.0.0.0 listing on port 3000
}
server {
listen80;
server_name yourdomain.com;
location/ {
proxy_pass http://node_js;
}
}
Upgraded Connection (Recommended for Node.js Applications)
Useful for Node.js applications with support for WebSockets like socket.io.
upstreamnode_js {
server 0.0.0.0:3000;
}
server {
listen80;
server_name yourdomain.com;
location/ {
proxy_pass http://node_js;
proxy_redirect off;
proxy_http_version1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
# not required but useful for applications with heavy WebSocket usage# as it increases the default timeout configuration of 60proxy_read_timeout80;
}
}
TLS/SSL (HTTPS)
Basic
The below configuration is only an example of what a TLS/SSL setup should look like. Please do not take these settings as the perfect secure solution for your applications. Please do research the proper settings that best fit with your Certificate Authority.
If you are looking for free SSL certificates, Let's Encrypt is a free, automated, and open Certificate Authority. Also, here is a wonderful step-by-step guide from Digital Ocean on how to setup TLS/SSL on Ubuntu 16.04.
SimulatedGREG/nginx-cheatsheet
nginx-cheatsheet
Each configuration below is written with minimum requirements for their described function. Please know that real world applications will most likely use a combination of these settings. This cheatsheet is meant to provide a general overview of how to setup specific features of nginx.
These configurations are meant to be used as Name-Based Virtual Hosts, saved within
/etc/nginx/sites-enabled
.Table of Configurations
listen
)server_name
)access_log
)gzip
,client_max_body_size
)301
Permanent302
TemporaryGeneral Settings
Port (
listen
)Domain name (
server_name
)Access Logging (
access_log
)Miscellaneous (
gzip
,client_max_body_size
)Serving Files
Static assets
The traditional web server.
Static assets with HTML5 History Mode
Useful for Single-Page Applications like Vue, React, Angular, etc.
Redirects
301
PermanentUseful for handling
www.yourdomain.com
vs.yourdomain.com
or redirectinghttp
tohttps
. In this case we will redirectwww.yourdomain.com
toyourdomain.com
.302
TemporaryRedirect on specific URL
Can be permanent (
301
) or temporary (302
).Reverse Proxy
Useful for Node.js applications like express.
Basic
Basic+
Upgraded Connection (Recommended for Node.js Applications)
Useful for Node.js applications with support for WebSockets like socket.io.
TLS/SSL (HTTPS)
Basic
The below configuration is only an example of what a TLS/SSL setup should look like. Please do not take these settings as the perfect secure solution for your applications. Please do research the proper settings that best fit with your Certificate Authority.
If you are looking for free SSL certificates, Let's Encrypt is a free, automated, and open Certificate Authority. Also, here is a wonderful step-by-step guide from Digital Ocean on how to setup TLS/SSL on Ubuntu 16.04.
Large Scale Applications
Load Balancing
Useful for large applications running multiple instances.