Skip to content
lanzai.co
Go back

Nginx Commands and Config Cheat Sheet

High-frequency Nginx operations and common config snippets.

Basic commands

nginx -t                    # test config
nginx -s reload             # graceful reload
nginx -s stop               # stop
sudo systemctl reload nginx # reload via systemd
sudo systemctl status nginx # status

gzip compression

gzip on;
gzip_types text/plain text/css application/javascript application/json image/svg+xml;
gzip_min_length 1024;

Static asset caching

location ~* \.(js|css|png|jpg|svg|woff2)$ {
    expires 30d;
    add_header Cache-Control "public, immutable";
}

Rate limiting

limit_req_zone $binary_remote_addr zone=api:10m rate=10r/s;

location /api/ {
    limit_req zone=api burst=20 nodelay;
    proxy_pass http://127.0.0.1:8080;
}

Access control

# allow only private network
location /admin/ {
    allow 192.168.1.0/24;
    deny all;
}

Redirects

# HTTP -> HTTPS
server {
    listen 80;
    server_name blog.example.com;
    return 301 https://$host$request_uri;
}

Logs

sudo tail -f /var/log/nginx/access.log
sudo tail -f /var/log/nginx/error.log

Custom log format:

log_format main '$remote_addr - $request_time s "$request" $status $body_bytes_sent';
access_log /var/log/nginx/access.log main;

Troubleshooting

curl -I http://localhost          # check response headers
sudo nginx -t && sudo systemctl reload nginx   # standard flow after edits
ss -tlnp | grep :80               # confirm port is listening

Share this post:

Previous Post
SSH Key Setup and Passwordless Login
Next Post
Git Commands Cheat Sheet