nginx在请求路径时返回一个文件

nginx在请求路径时返回一个文件,nginx,Nginx,如标题所述,当我请求“”时,我希望得到真正的文件“/home/vagrant/example/resources/views/posters.html” 以下是我的nginx配置: server_name example.com; root /home/vagrant/example/public; index index.html; location / { return 404; } location ~ ^/test/poster/?$ { default_type t

如标题所述,当我请求“”时,我希望得到真正的文件“/home/vagrant/example/resources/views/posters.html”

以下是我的nginx配置:

server_name example.com;
root /home/vagrant/example/public;
index index.html;

location / {
    return 404;
}

location ~ ^/test/poster/?$ {
    default_type text/html;
    index posters.html;
    alias /home/vagrant/example/resouces/views/;
}
但是当我发出请求时,我得到了404响应。我搜索nginx错误日志,得到以下信息:

2018/10/20 11:54:41 [debug] 4690#4690: *180 http script copy: "/home/vagrant/example/resouces/views/"
2018/10/20 11:54:41 [debug] 4690#4690: *180 open index "/home/vagrant/example/resouces/views/posters.html"
2018/10/20 11:54:41 [debug] 4690#4690: *180 internal redirect: "/test/poster/posters.html?"
2018/10/20 11:54:41 [debug] 4690#4690: *180 rewrite phase: 1
这里是我的问题,当打开索引时,它已经找到了文件,为什么它会充当一个内部重定向

我最终通过使用try_文件解决了这个问题,我只想知道为什么在一个位置块中使用带索引的alias时它不起作用。谢谢

执行一个多级过程

如果URI以
/
结尾,并且被发现是当前正在处理请求的
位置
中的目录,Nginx将在
索引
指令的值中检查与名称匹配的文件

如果找到匹配的文件,URI将通过附加该名称进行更改,并开始搜索
位置来处理新的URI

在您的情况下,URI
/test/poster/posters.html
与原始
位置不匹配,因此找不到
posters.html
文件

  • 如果uri以“/”结尾,nginx将把索引文件和别名路径合并为完整路径索引文件,并检查此文件是否存在
  • 如果存在,则将开始内部重定向;如果不存在,nginx将检查别名路径是否为dir
  • 如果别名路径是dir,那么nginx将尝试其他索引文件,直到返回403;如果别名路径不是dir,nginx将直接返回404

  • 如果uri不以“/”结尾,nginx将直接使用别名路径
  • 如果别名路径为文件但不存在,nginx将返回404;如果别名路径是dir,nginx将返回一个301,其尾部斜杠附加了originuri

  • 如前所述,“发现是一个目录”,您是指“/test/poster/”还是alias指令后的目录?
    /test/poster/
    是一个URI,但
    alias
    指令将其映射到目录
    /home/vagrant/example/resources/views/