【发布时间】:2014-10-14 12:56:39
【问题描述】:
对于我的网站,我通常有两个 server{} 块:一个为网站提供服务,另一个为来自不同域的该网站提供静态内容。因为总是从主域而不是静态域请求favicon.ico 文件,所以我必须在站点的server{} 块而不是静态文件块中处理该特定文件。
当找不到favicon.ico 文件时,Nginx 会返回我定义的 404 错误页面,以及 404 HTTP 状态码。但是,我不想为我漂亮的 404 页面发送这些位。 我想发送一个带有 204 状态码的完全空的响应。以下是我尝试过的,但它不起作用。有没有正确的方法来做到这一点?这个想法是 204 表示找到的文件,但是一个完全空白的图像。
另外,我尝试保存位实际上是一个坏主意吗? 如果实际上返回 204 而不是 404 是一个坏主意,那么有没有更优雅的方法?如何返回一个空的 404 页面而不创建一个新的实际空文件并将其设置为该位置块内的 error_page 指令?
server {
...
error_page 404 /../static/404.html;
location @return_204 {
return 204;
}
# Case-insensitive matching of .txt and .xml files.
# (Example: robots.txt, crossdomain.xml, and sitemap.xml)
location ~* \.(txt|xml)$ {
try_files $uri /../static/$uri;
}
location = /favicon.ico {
log_not_found off;
error_page 404 = @return_204;
try_files $uri /../static/$uri;
}
...
}
【问题讨论】:
标签: nginx http-status-code-404 http-status-code-204