Operations 5 min read

Essential Nginx Configuration Cheat Sheet: Ports, Logs, Domains, and More

This cheat sheet compiles the most common Nginx configuration snippets—including listening ports, access logs, server names, static file serving, redirects, reverse proxy, load balancing, and SSL settings—providing a quick reference for developers and operators to set up high‑performance web servers.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Essential Nginx Configuration Cheat Sheet: Ports, Logs, Domains, and More

Overview

Nginx is a high‑performance HTTP and reverse‑proxy web server that also supports IMAP/POP3/SMTP. It is popular for its rich feature set, stability, example configurations, and low resource consumption. The following sections present frequently used configuration snippets.

Listening Ports

Configure standard HTTP, HTTPS, HTTP/2, and IPv6 listeners:

server {
# Standard HTTP
listen 80;
# Standard HTTPS
listen 443 ssl;
# HTTP/2
listen 443 ssl http2;
# IPv6 on port 80
listen [::]:80;
# IPv6 only
listen [::]:80 ipv6only=on;
}

Access Log

Enable or disable logging and set the log file path:

server {
# Path to log file
access_log /path/to/file.log;
# Turn logging on or off
access_log on;
}

Server Names (Domain Configuration)

Define which hostnames the server should respond to:

server {
# Single domain
server_name yourdomain.com;
# Multiple domains
server_name yourdomain.com www.yourdomain.com;
# Wildcard subdomains
server_name *.yourdomain.com;
# All top‑level domains
server_name yourdomain.*;
# Empty host (listen on IP address)
server_name "";
}

Static Resources

Serve static files from a directory:

server {
listen 80;
server_name yourdomain.com;
location / {
root /path/to/website;
}
}

Redirects

Permanent redirects from one domain to another or from a specific URL to another location:

server {
listen 80;
server_name www.yourdomain.com;
return 301 http://yourdomain.com$request_uri;
}
server {
listen 80;
server_name www.yourdomain.com;
location /redirect-url {
return 301 http://otherdomain.com;
}
}

Reverse Proxy

Proxy requests to an upstream application server (e.g., a Node.js app on port 3000):

server {
listen 80;
server_name yourdomain.com;
location / {
proxy_pass http://0.0.0.0:3000;
# 0.0.0.0:3000 is the application server
}
}

Load Balancing

Define an upstream group with multiple backend servers and proxy to it:

upstream node_js {
server 0.0.0.0:3000;
server 0.0.0.0:4000;
server 123.131.121.122;
}
server {
listen 80;
server_name yourdomain.com;
location / {
proxy_pass http://node_js;
}
}

SSL Configuration

Enable SSL, specify certificates, and enforce security headers:

server {
listen 443 ssl;
server_name yourdomain.com;
ssl on;
ssl_certificate /path/to/cert.pem;
ssl_certificate_key /path/to/privatekey.pem;
ssl_stapling on;
ssl_stapling_verify on;
ssl_trusted_certificate /path/to/fullchain.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_session_timeout 1h;
ssl_session_cache shared:SSL:50m;
add_header Strict-Transport-Security max-age=15768000;
}

Additionally, redirect all HTTP traffic to HTTPS:

server {
listen 80;
server_name yourdomain.com;
return 301 https://$host$request_uri;
}

Reference: https://vishnu.hashnode.dev/nginx-cheatsheet

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

load balancingNginxreverse proxyWeb serverSSL
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.