Nginx 1 cheatsheet

Nginx 1: Servers and static files

6 common Nginx 1 tasks.

Serve a static site

A server block that serves files from a folder.

server {
    listen 80;
    server_name example.com www.example.com;
    root /var/www/example;
    index index.html;

    location / {
        try_files $uri $uri/ =404;
    }
}

Serve a single-page app

Send unknown paths to index.html so client-side routing works.

location / {
    try_files $uri $uri/ /index.html;
}

Enable a site on Debian or Ubuntu

Link a file from sites-available to sites-enabled, then test and reload.

sudo ln -s /etc/nginx/sites-available/example /etc/nginx/sites-enabled/

Cache static assets

Tell browsers to cache images, scripts and styles for 30 days.

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

Enable gzip compression

Compress text responses to save bandwidth.

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

Allow larger uploads

Raise the request body limit, which defaults to 1 MB.

client_max_body_size 20m;

Was this page helpful?