gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 06:29:55 25 4
gpt4 key购买 nike

如标题描述,当我请求“http://example.com/test/poster/”时,我想获取真实文件“/home/vagrant/example/resouces/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_files 解决了它,我只是想知道为什么当我在一个位置 block 中使用带索引的别名时它不起作用。谢谢!

最佳答案

  1. 如果uri以'/'结尾,nginx会将索引文件和别名路径合并为一个全路径索引文件,并检查该文件是否存在。
  2. 如果存在,则开始内部重定向;如果不存在,nginx 将检查别名路径是否为目录。
  3. 如果别名路径是目录,nginx 会尝试其他索引文件,直到返回 403;如果别名路径不是目录,nginx 会直接返回 404。

  1. 如果uri不以'/'结尾,nginx将直接使用别名路径。
  2. 如果别名路径是一个文件但不存在,nginx 将返回一个 404;如果别名路径是一个目录,nginx 将返回一个 301,尾部斜杠附加原始 uri。

关于nginx 在请求路径时返回一个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52902409/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com