NGINX - 如何为所有虚拟主机设置过期标头? [英] NGINX - How would I set expires headers for all virtual hosts?
问题描述
我正在寻找一种方法来设置服务器范围的过期标头,以便所有现有或新的虚拟主机都配置相同.不幸的是,我无法在每个虚拟主机的 conf 文件中执行此操作,因此我正在寻找一种方法来在服务器范围内设置以下指令.
I'm looking for a way to set server wide expires headers so that all existing or new virtual hosts will be configured the same. Unfortunately, I can not do this in each virtual host's conf file, so I am looking for a way to make the following directives server wide.
location ~* .(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg
|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid
|midi|wav|bmp|rtf)$ {
expires max;
log_not_found off;
access_log off;
}
推荐答案
expires
指令可以放在 http
块中,因此被所有服务器块继承,并且他们的位置块.
The expires
directive can be placed in the http
block and is therefore inherited by all server blocks and their location blocks.
手册展示了一个使用 expires
指令和 map
变量的例子.请参阅本文档.
The manual shows an example of using the expires
directive with a map
variable. See this document.
您可以使用 $request_uri
而不是 $sent_http_content_type
来匹配您现有的正则表达式并实现与以下相同的行为:
You could use $request_uri
rather than $sent_http_content_type
to match your existing regex and achieve identical behaviour with:
map $request_uri $expires {
default off;
~*.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)(?|$) max;
}
expires $expires;
两个指令都放在 http
块内,但在任何 server
块外.
Both directives placed inside the http
block but outside any server
block.
有关详细信息,请参阅本文档.
See this document for details.
这篇关于NGINX - 如何为所有虚拟主机设置过期标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!