Nginx 关闭Google weblight 访问

Google weblight 说的很好听,可以让较慢网速的人访问你的网站,但是也有他自身的不足: 严重影响你的广告收入.

因此很多人都会想法去关闭Google weblight的访问.

Google weblight的网站给出方案,就是在header里面添加Cache-Control: no-transform. Google 看到这个header,就会展示原网页.

一个小知识, 按照RFC 7230, 3.2.2. Field Order里面所说,你可以在header里面添加多个Cache-Control,只要他们的field-value pair是不一样的,多个Cache-Control就是有效的.

比如说:

Cache-Control: no-cache, no-store, private
Cache-Control: no-cache

这是有效的,会被浏览器认为是:

Cache-Control: no-cache, no-store, private, no-cache

这个明白了以后, 在nginx中关闭Google Weblight 访问就很简单了. Google weblight使用的UA为

Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko; googleweblight) Chrome/38.0.1025.166 Mobile Safari/535.19

关键字符”googleweblight”, 因此我们只需要对含有googleweblight字符的UA的访问,添加header “Cache-Control: no-transform”就好了

下面上配置代码:

在Nginx http block 里面添加map 命令:

map $http_user_agent $cache_no_transform { 
"~*googleweblight" "no-transform"; 
}

然后在server block 或者 location block 里面添加:

add_header Cache-Control $cache_no_transform;

就可以了. 

一个小知识, Nginx 的add_header命令,如果value为空的话,默认是会忽略的. 因此

add_header Cache-Control '';

其实是会被nginx忽略的.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.