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

时间:2018-02-12 10:23:15

标签: nginx

我需要为我的文件系统提供GET /请求的文件。我尝试了以下方法:

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

其余请求代理到后端:

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

但是当我执行请求时,nginx不是从文件系统提供文件,而是向后端询问/page2257160.html,后端返回404,nginx将此404发送回客户端。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

index指令执行内部重定向,因此您需要第二个位置来匹配重写的URI。例如:

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

有关详细信息,请参阅this document

您可以使用一个location块和try_files指令实现相同的功能。例如:

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

有关详情,请参阅this document