技术解析
WordPress 安装了一个 WPTouch 插件,同一个网址,当移动设备和 PC 访问的时候会显示不同的页面。
Nginx 启用了 FastCGI 缓存,根据 UA 生成不同的缓存文件。
现在的问题是,当 A 手机访问的时候会生成缓存文件 A , B 手机访问的时候会生成缓存文件 B ;而我希望所有的移动设备只生成一个缓存文件 A , PC 端访问生成文件 B 。
Nginx 配置如下:
fastcgi_cache_path /home/www/wp-content/cache levels=1:2 keys_zone=WORDPRESS:250m inactive=7d max_size=1G;
#fastcgi_temp_path /home/www/wp-content/temp;
fastcgi_cache_key "$scheme$request_method$host$request_uri$mobile_request";
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
server {
listen 80;
server_name abc.com抗投诉服务器;
index index.html index.htm index.php;
root /home/www;
set $skip_cache 0;
set $mobile_request 0;
if ($http_user_agent ~* '(iPhone|iPod|incognito|webmate|Android|dream|CUPCAKE|froyo|BlackBerry|webOS|s8000|bada|IEMobile|Googlebot\-Mobile|AdsBot\-Google)') {
set $mobile_request 1;
}
if ($request_method = POST) {
set $skip_cache 1;
}
if ($query_string != "") {
set $skip_cache 1;
}
if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
set $skip_cache 1;
}
if ($http_cookie ~* "wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
set $skip_cache 1;
}
location / {
try_files $uri $uri/ /index.php?$args;
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
}
location ~ [^/]\.php(/|$) {
try_files $uri =404;
fastcgi_pass unix:/dev/shm/php-fpm.sock;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
fastcgi_cache WORDPRESS;
fastcgi_cache_valid 200 301 302 7d;
add_header X-UA-Detect $scheme$request_method$host$request_uri$mobile_request;
add_header X-Frame-Options deny;
add_header X-Content-Type-Options nosniff;
add_header X-Cache "$upstream_cache_status From $host";
}
location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
access_log off; log_not_found off; expires max;
}
location ~ /\. { deny all; access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log /home/www/wp-content/logs/access.log;
error_log /home/www/wp-content/logs/error.log;
}
求各位大能帮忙看看该如何配置,感激不尽!