NGINX - 我将如何为所有虚拟主机设置过期标题? [英] NGINX - How would I set expires headers for all virtual hosts?

查看:129
本文介绍了NGINX - 我将如何为所有虚拟主机设置过期标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法设置服务器范围的过期标题,以便所有现有的或新的虚拟主机都将配置相同。不幸的是,我不能在每个虚拟主机的conf文件中这样做,所以我正在寻找一种方法来使服务器的以下指令成为可能。

  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 块中,因此可以被所有服务器块及其位置块继承。



本手册显示了一个使用 expires 指令并使用 map 变量。请参阅此文档



您可以使用 $ request_uri 而不是 $ sent_http_content_type 来匹配您现有的正则表达式,并通过以下方式实现相同的行为:

  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;

这两个指令位于 http 块内,但在任何服务器块之外



请参阅此文档以了解详情。


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;
}

解决方案

The expires directive can be placed in the http block and is therefore inherited by all server blocks and their location blocks.

The manual shows an example of using the expires directive with a map variable. See this document.

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;

Both directives placed inside the http block but outside any server block.

See this document for details.

这篇关于NGINX - 我将如何为所有虚拟主机设置过期标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆