Nginx Cheat Sheet: Common Configuration Snippets

This article provides a concise collection of frequently used Nginx configuration examples—including listening ports, access logs, server names, static assets, redirects, reverse proxy, load balancing, and SSL settings—to help developers quickly set up a high‑performance web server.

Top Architect
Top Architect
Top Architect
Nginx Cheat Sheet: Common Configuration Snippets

Nginx is a high‑performance HTTP and reverse‑proxy web server that also offers IMAP/POP3/SMTP services, and it is widely appreciated for its rich feature set, stability, example configuration files, and low system‑resource consumption.

The article summarizes a set of commonly used Nginx configuration snippets that can be directly copied into nginx.conf to handle typical scenarios.

Listening Ports

server {<br/># Standard HTTP Protocol<br/>listen 80;<br/># Standard HTTPS Protocol<br/>listen 443 ssl;<br/># For http2<br/>listen 443 ssl http2;<br/># Listen on 80 using IPv6<br/>listen [::]:80;<br/># Listen only on using IPv6<br/>listen [::]:80 ipv6only=on;<br/>}

Access Logs

server {<br/># Relative or full path to log file<br/>access_log /path/to/file.log;<br/># Turn 'on' or 'off'<br/>access_log on;<br/>}

Server Name (Domain)

server {<br/># Listen to yourdomain.com<br/>server_name yourdomain.com;<br/># Listen to multiple domains<br/>server_name yourdomain.com www.yourdomain.com;<br/># Listen to all domains<br/>server_name *.yourdomain.com;<br/># Listen to all top‑level domains<br/>server_name yourdomain.*;<br/># Listen to unspecified Hostnames (IP address itself)<br/>server_name "";<br/>}

Static Assets

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

Redirects

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

Reverse Proxy

server {<br/>listen 80;<br/>server_name yourdomain.com;<br/>location / {<br/>proxy_pass http://0.0.0.0:3000;<br/># where 0.0.0.0:3000 is your application server (e.g., node.js) bound on port 3000<br/>}<br/>}

Load Balancing

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

SSL Configuration

server {<br/>listen 443 ssl;<br/>server_name yourdomain.com;<br/>ssl on;<br/>ssl_certificate /path/to/cert.pem;<br/>ssl_certificate_key /path/to/privatekey.pem;<br/>ssl_stapling on;<br/>ssl_stapling_verify on;<br/>ssl_trusted_certificate /path/to/fullchain.pem;<br/>ssl_protocols TLSv1 TLSv1.1 TLSv1.2;<br/>ssl_session_timeout 1h;<br/>ssl_session_cache shared:SSL:50m;<br/>add_header Strict-Transport-Security max-age=15768000;<br/>}<br/># Permanent Redirect for HTTP to HTTPS<br/>server {<br/>listen 80;<br/>server_name yourdomain.com;<br/>return 301 https://$host$request_uri;<br/>}

These snippets cover the most common use‑cases such as reverse proxying, HTTPS, HTTP/2, IPv6, caching, WordPress, CDN, Node.js, and Python (Django) deployments, and they can be generated automatically with tools like nginxconfig.io or the open‑source project on GitHub.

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.

Backend DevelopmentNginxWeb serverSSL
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

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.