Cheat sheets

Cheat sheet: Nginx

Managing Nginx: test the configuration, reload the service, and the most useful blocks for your configuration.

Management

Check the configuration (always first!) sudo nginx -t
Show the complete compiled configuration sudo nginx -T
Reload without interruption sudo systemctl reload nginx
Restart sudo systemctl restart nginx
Show the status systemctl status nginx
The version and the options it was built with nginx -V

Logs

Follow the access log sudo tail -f /var/log/nginx/access.log
Follow the error log sudo tail -f /var/log/nginx/error.log
The last 50 errors sudo tail -n 50 /var/log/nginx/error.log

Activating a site (Debian and Ubuntu)

Enable a site sudo ln -s /etc/nginx/sites-available/mysite /etc/nginx/sites-enabled/
Disable a site sudo rm /etc/nginx/sites-enabled/mysite
Location of the main configuration /etc/nginx/nginx.conf

Certificates (certbot)

Request and configure a certificate sudo certbot --nginx -d example.com -d www.example.com
Test renewal sudo certbot renew --dry-run
Check the result curl -I https://example.com

Testing

Test a site without DNS (different hostname) curl -I -H "Host: example.com" http://127.0.0.1
The headers of a page curl -sI https://example.com

Good to know

  • A static site: server { listen 80; server_name example.com; root /var/www/example; index index.html; }
  • Forwarding to an app (reverse proxy): inside a location / { } you add proxy_pass http://127.0.0.1:3000; and the lines proxy_set_header Host $host; and proxy_set_header X-Forwarded-For $remote_addr;.
  • From http to https: in a server on port 80: return 301 https://$host$request_uri;
  • Test always with nginx -t before you reload: an error in the configuration can bring down the entire site.
  • More about https and proxies: Reverse proxy with https, and check the result with our SSL check and HTTP headers tool.

Sources and attribution

This overview has been compiled in our own words. Commands and options change per version: when in doubt, check the official documentation.