上周把blog开启了cache php结果后,主要是为了解决假想的一种短时间内请求过大php-fpm性能跟不上的问题。但是今天发现了一个比较奇怪的问题,就是打开的blog首页的时候页面是空白的。然后想起这个可能是和我针对dnspod的监控有特殊配置的原因。因为dnspod的访问比较频繁,所以我设置了直接返回200的特殊配置,避免无谓消耗机器的性能。42 location / {4344 # First attempt to serve request as file, then45 # as directory, then fall back to displaying a 404.46 try_files $uri $uri/ /index.php;47 # Uncomment to enable naxsi on this location48 # include /etc/nginx/naxsi.rules49 if ($http_user_agent ~ monitor ) {50 return 200;51 access_log off;52 }53 fastcgi_cache blog;54 } 自己当时配置的时候只是去连续刷新几次,看是否成功cache住了页面。但是实际平时都是dnspod的监控在访问,直接返回了200,这样如果这样的页面被cache,自己打开的时候就啥东西都看不到了。解决的方式就是修改一下dnspod的监控页面,比如监控的URL改成/favicon.ico之类的。
但是这样修改也会有个漏洞,比如我手动设置user-agent为monitor,使劲访问/,则首页缓存住的一直是一个空页面,所以想了个办法,把user-agent单独map到一个变量上,然后cache_key里把这个变量加上25 map $http_user_agent $agent {26 default ‘normal’;27 ~monitor ‘dnspod’;28 }
curl -I -A “monitor” http://blog.gnuers.orgHTTP/1.1 200 OKServer: nginx/1.2.7Date: Wed, 03 Apr 2013 12:11:55 GMTContent-Type: application/octet-streamContent-Length: 0Connection: keep-alive
– – ::ffff:101.226.68.137:35145 – – [03/Apr/2013:13:46:19 +0000] blog.gnuers.org “HEAD / HTTP/1.1” 200 0 “-” “Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; QQDownload 713; .NET CLR 2.0.50727; InfoPath.2)” normal “-” “unix:/var/run/php5-fpm.sock” “- -” 0.214 0.214 log_format main ‘$http_orig_client_ip – $remote_addr:$remote_port – $remote_user [$time_local] $host “$request” $status $body_bytes_sent “$http_referer” ‘‘"$http_user_agent – $agent – $hitstatus" “$http_x_forwarded_for” “$upstream_addr” “$ssl_protocol $ssl_cipher” $request_time $upstream_response_time’;