1

在现场,favicon.ico保存在/images/目录中,而不是根目录中。我怎么告诉nginx看那里?

已经尝试过 - 看起来正确但不起作用:

location = /favicon.ico$ { rewrite /(.*) /images/$1 last; }

返回404 Not Found

文件在那里:请求http://www.example.com/images/favicon.ico成功

4

2 回答 2

2

我们也可以通过重写来解决这个问题。(我必须使用重写来解决它,因为我使用的是try_files指令。)

这是我的配置:

# Long cache times for static content
location ~ /_/static/(.+)$  {
    # hold the last five versions of our static content
    try_files
        /_/static_477526f-master/$1
        /_/static_05c8613-release/$1
        /_/static_05c8613-master/$1
        /_/static_db26497-release/$1
        /_/static_db26497-master/$1;
    expires  365d;
    add_header Cache-Control public;
    add_header Access-Control-Allow-Origin *;
}

location = /favicon.ico {
    rewrite . /_/static/favicon.ico;
    expires  14d;
    add_header Cache-Control public;
}

.则表达式匹配任何内容,第二个条目重写 url。由于这是一个特定于网站图标的位置,我们可以只对其进行硬编码,而不是使用正则表达式捕获和$1.

于 2015-09-19T05:39:44.400 回答