Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Nginx中为根url提供特定文件?_Nginx - Fatal编程技术网

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

如何在Nginx中为根url提供特定文件?,nginx,Nginx,我需要为GET/请求提供文件系统中的文件。我尝试了以下方法: location = / { index page2257160.html; root /var/www/site/; } 其余请求将代理到后端: location / { proxy_pass http://localhost:1234; } 但当我执行请求时,nginx不是从文件系统提供文件,而是向后端询问/page2257160.html,后端返回404,nginx将此404发送回客户端 如何修复此问题?指令执行

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

location = / {
  index page2257160.html;
  root /var/www/site/;
}
其余请求将代理到后端:

location / {
  proxy_pass http://localhost:1234;
}
但当我执行请求时,nginx不是从文件系统提供文件,而是向后端询问
/page2257160.html
,后端返回404,nginx将此404发送回客户端


如何修复此问题?

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

root /var/www/site/;
location = / {
    index page2257160.html;
}
location = /page2257160.html {
}
location = / {
    root /var/www/site/;
    try_files /page2257160.html =404;
}
有关详细信息,请参阅


您可以使用一个
位置
块和一个
try\u files
指令实现相同的功能。例如:

root /var/www/site/;
location = / {
    index page2257160.html;
}
location = /page2257160.html {
}
location = / {
    root /var/www/site/;
    try_files /page2257160.html =404;
}
更多信息,请参阅