nginx

, and

Syntax

root path; / alias path; / try_files;

Parameters

ParametersDescriptionExamplesLevel
root root /var/www/html; Common
alias ( location ) alias /data/files/; Common
try_files try_files $uri $uri/ /index.html; Common
expires expires 30d; Common
gzip gzip gzip on; Common
autoindex autoindex on; Advanced

Examples

SPA (React/Vue)

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

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

+

http {
    gzip on;
    gzip_types text/css application/javascript image/svg+xml;
    gzip_min_length 1000;

    server {
        location ~* \.(css|js|jpg|png|woff2)$ {
            expires 1y;
            add_header Cache-Control "public, immutable";
        }
    }
}
+ gzip

location /downloads/ {
    alias /data/files/;
    autoindex on;
    autoindex_exact_size off;
    autoindex_localtime on;
}
,

Common Errors

403 ForbiddenNginx, and (chmod/chown)
404 Not Found(SPA New)try_files $uri $uri/ /index.html;

Tips

Related Commands