【问题标题】:Nginx - sending a static file for a specific locationNginx - 为特定位置发送静态文件
【发布时间】:2017-07-10 05:02:39
【问题描述】:

我使用 nginx 作为代理(使用location /)并尝试为另一个位置块下的热链接保护重定向提供静态图像。以下是我用来尝试提供图像的内容。我已将 root 指令移到 location 块之外,这是 nginx 出于某种原因构建正确路径所必需的。

location = /hotlink.png {
    autoindex off;
    try_files hotlink.png hotlink.png
}

但是,当我查看日志时,它仍在通过将 URI 附加到根路径:{root}/hotlink.png/index.html 来查找 index.html。 我只是希望它仅在 /hotlink.png 匹配时发送文件 {root}/hotlink.png ,仅此而已。

为什么它还在寻找带有autoindex off 的索引?我该如何解决这个问题,或者有没有更好的方法来处理这种情况?

【问题讨论】:

  • 删除try_files 声明。
  • @RichardSmith 这就是我最初拥有的。如果没有try_files,它似乎完全跳过了该块并退回了location /
  • hotlink.png 是真实文件吗?
  • 是的,这是一个真实的文件

标签: http nginx nginx-location


【解决方案1】:

在换档并回到这一点之后,我只需将autoindex off 指令也移到位置块之外就可以工作了。它适用于没有try_files 指令的。为了简洁和避免冗余,我把它省略了

root /path/to/static/files;
autoindex off;

location / {
   # proxy
}

location = /hotlink.png {}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-21
    • 2014-01-26
    • 2013-07-19
    • 2018-03-05
    • 2016-08-05
    • 1970-01-01
    • 1970-01-01
    • 2022-06-11
    相关资源
    最近更新 更多