2012-07-11 118 views
1

现场,favicon.ico保存在/images/目录中,而不是根目录。我该如何告诉nginx去那里看?与nginx,你如何重写请求到不同的目录?

尝试 - 这看起来正确,但不工作:

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

回报404 Not Found

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

回答

6
+0

作品!凉!有没有办法做到这一点使用路​​径相对做_document_根?顺便说一句,伟大的链接 – 2012-07-11 12:56:11

+0

你可以使用相对于服务器根目录的路径,它是在编译时通过“prefix =”参数或“p =”命令行参数或%% PREFIX %% env变量设置的。 http://nginx.org/en/docs/install.html – VBart 2012-07-11 13:54:15

+0

但我建议留在绝对路径。显式配置比任何隐含的东西都要好。 – VBart 2012-07-11 13:58:40

1

我们也可以重写解决这个问题。 (我不得不使用重写来解决这个问题,因为我使用的是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。由于这是一个favicon特定的位置,我们可以对其进行硬编码,而不是使用正则表达式捕获和$1