Nginx - 为特定位置发送静态文件

时间:2017-02-20 19:19:50

标签: http nginx nginx-location

我正在使用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的索引?我该如何解决这个问题,或者是否有更好的方法来处理这种情况呢?

1 个答案:

答案 0 :(得分:0)

在改变齿轮并回到此之后,我通过简单地将autoindex off指令移动到位置块之外来实现它。它适用于没有try_files指令的。我把它留给了简洁并避免冗余

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

location / {
   # proxy
}

location = /hotlink.png {}