简体   繁体   中英

serve file from virtual directory using nginx

How do I serve a static file (let's say foo.html ) from location /foo using NGINX.

I've been trying with alias but I haven't been successful. Here is what I tried:

location /foo {
    alias foo.html;
}

alias is similar to a root directive and specifies the full path to a file or directory. See this document for details.

For example:

location /foo {
    alias /path/to/foo.html;
}

You could achieve the same behaviour with try_files . See this document for details.

For example:

root /path/to/root;

location /foo {
    try_files /foo.html =404;
}

You're essentially rewriting URL from /foo to /foo.html , so the rewrite is meant exactly for cases like this:

rewrite ^/foo$ /foo.html; 

No need to be scared of a regular expression to be slower - it's not.

Consider that try_files approach will have to make stat call (check file existence), which makes it slower.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM