http - 如何阻止 nginx 将图像 URL 视为实际文件?

标签 http url nginx

抱歉这个糟糕的标题;很难解释

我的网站内置了一个搜索功能,使用方式如下:

http://www.example.com/search/queryhere

问题是当我尝试搜索文件名时,例如:

http://www.example.com/search/hello.jpg

我得到一个 nginx 404 页面,据说是因为 search/hello.jpg 正试图打开。一种解决方法是在请求路径中添加一个正斜杠,但我想要一个更永久的解决方案。我该如何解决这个问题?

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
            expires max;
            log_not_found off;
            access_log off;
    }

最佳答案

告诉 nginx 仅对现有 文件执行额外指令:

location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
  if (-f $request_filename) {
    expires max;
    log_not_found off;
    access_log off;
  }
}

也许有更好的方法不需要 if...

According to nginx pitfalls :

Using if to ensure a file exists is horrible. It's mean. If you have any recent version of Nginx you should look at try_files which just made life much easier.

关于http - 如何阻止 nginx 将图像 URL 视为实际文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20530038/

相关文章:

http - 请求被拒绝,因为在 Grails 中没有找到多部分边界

http - 当用户同时离开页面时,http 请求会发生什么情况?

javascript - 从 URL 中搜索并删除带有可选字符的参数

nginx从静态服务(如果找不到)尝试反向代理

python - uwsgi 服务没有启动

nginx - 在Nginx入口 Controller 中添加重写规则301

http - 当没有返回与缓存相关的 header 时,浏览器是否会执行缓存响应?

http - 在 Rest 集合中分页

php - 忽略视频网址中的换行符

php - 如何在斜杠后取一个字符串?