简体   繁体   中英

How can I serve a particular file for root url in Nginx?

I need to serve file from my filesystem for GET / request. I tried the following:

location = / {
  index page2257160.html;
  root /var/www/site/;
}

The rest of requests are proxied to backend:

location / {
  proxy_pass http://localhost:1234;
}

But when I do the request, instead of serving the file from filesystem, nginx asks backend about /page2257160.html , backend returns 404, nginx sends this 404 back to client.

How can I fix this?

The index directive performs an internal redirect, so you will need a second location to match the rewritten URI. For example:

root /var/www/site/;
location = / {
    index page2257160.html;
}
location = /page2257160.html {
}

See this document for details.


You can achieve the same thing with one location block and a try_files directive. For example:

location = / {
    root /var/www/site/;
    try_files /page2257160.html =404;
}

See this document for more.

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