nginx reference

paths (debian/ubuntu)

service control

ActionCommand
Start/stop/restartsudo systemctl start|stop|restart nginx
Reload configsudo systemctl reload nginx
Enable at bootsudo systemctl enable nginx
Statussystemctl status nginx

config checks

TaskCommand
Syntax testsudo nginx -t
Show compiled modulesnginx -V

minimal server block (http)

server {
    listen 80;
    server_name example.com www.example.com;
    root /var/www/example;
    access_log /var/log/nginx/example-access.log;
    error_log  /var/log/nginx/example-error.log;
    location / {
        try_files $uri $uri/ =404;
    }
}

https with certbot

sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d example.com -d www.example.com
sudo systemctl reload nginx

common reverse proxy

server {
    listen 443 ssl;
    server_name api.example.com;
    ssl_certificate /etc/letsencrypt/live/api.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/api.example.com/privkey.pem;

    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

gzip and caching

gzip on;
gzip_types text/plain text/css application/javascript application/json;

location ~* \\.(jpg|jpeg|png|gif|ico|css|js)$ {
    expires 7d;
    add_header Cache-Control \"public\";
}

common errors & fixes

Return to Home