抱歉这个糟糕的标题;很难解释
我的网站内置了一个搜索功能,使用方式如下:
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
...
Using
if
to ensure a file exists is horrible. It's mean. If you have any recent version of Nginx you should look attry_files
which just made life much easier.
关于http - 如何阻止 nginx 将图像 URL 视为实际文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20530038/