简体   繁体   English

如何在 Nginx 中为根 url 提供特定文件?

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

I need to serve file from my filesystem for GET / request.我需要从我的文件系统中为GET /请求提供文件。 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.但是当我执行请求时,nginx 不是从文件系统提供文件,而是向后端询问/page2257160.html ,后端返回 404,nginx 将此 404 发送回客户端。

How can I fix this?我怎样才能解决这个问题?

The index directive performs an internal redirect, so you will need a second location to match the rewritten URI. index指令执行内部重定向,因此您需要第二个位置来匹配重写的 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.您可以使用一个location块和一个try_files指令来实现相同的目的。 For example:例如:

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

See this document for more.有关更多信息,请参阅此文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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