Skip to content

Instantly share code, notes, and snippets.

@HuakunShen
Last active September 29, 2021 23:23
Show Gist options
  • Save HuakunShen/f0e7759a975850adbf3182f53ba056f3 to your computer and use it in GitHub Desktop.
Save HuakunShen/f0e7759a975850adbf3182f53ba056f3 to your computer and use it in GitHub Desktop.
Nginx host static files

Nginx host static files

server {
    listen 8080 default_server;
    listen [::]:8080 default_server;

    root /var/www/html;

    index index.html index.htm index.nginx-debian.html;

    server_name <domain.com>;

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

    # option 1
    location /data {
        root /var/www/;
        autoindex on;
    }

    # option 2
    location /static {
        alias /var/www/data/;
        autoindex on;
    }

    # option 3
    location ~ ^/(images|css|js) {
        root /var/www/data/;
        autoindex on;
    }
}

Put some files under /data/static

Access http://domain.com/static/<filename>

Access http://domain.com/images/<image filename>

There is a difference between root and alias.

root

The path in location will be concatenated to the root.

alias

location will not be concatenated to root, but using location directly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment