当前位置:首页>WordPress教程>Nginx分开代理缓存WordPress网站的移动端与PC端页面

Nginx分开代理缓存WordPress网站的移动端与PC端页面

如果不是完全的,也就是说移动端访问的内容与PC端访问的内容不完全一致时,可能希望将端 nginx 缓存分开,由浏览器确定使用移动端或PC端(桌面)的缓存。本文将介绍如何配置 nginx fastcgi 或反向缓存,以便为移动端和桌面端分开缓存。

帮助我找到了一个用户代理字符串列表,这相当完整,所以用这个作为分离移动,平板电脑,手持和桌面的判断基础。

我们将使用 nginx 指令来检测用户代理并将其归到移动和桌面分类中。

然后,我们将使用变量并调整缓存(相关教程请阅读前文:为Apache配置Nginx反向代理WordPress缓存), 或使用 nginx.proxy_cachefastcgi_cache

下面是判断指令:

# map the list of user agents must escape ( with \(
map $http_user_agent $mobile_user {
   default "ZHANZHANGB_DESKTOP";
   # Android
   "~Mozilla/5.0 \(Linux; Android" ZHANZHANGB_MOBILE;
   # Opera
   "~Opera Mini" ZHANZHANGB_MOBILE;
   # iOS
   "~Mozilla/5.0 \(iPhone" ZHANZHANGB_MOBILE;
   # Windows Phone
   "~Mozilla/5.0 \(Windows Phone" ZHANZHANGB_MOBILE;
   }

现在,需要修改缓存密钥,在逻辑上区分桌面和移动用户。fastcgi_cache_key$mobile_user

location ~ \.$ {
	try_files $uri =404;
	# add cache status
	add_header WP-Zhanzhangb-Fastcgi-Cache $upstream_cache_status;
	# add the cache skip reason if relevant
	add_header WP-Zhanzhangb-Skip $skip_reason;
	add_header X-Exception $exception;
	include fastcgi_params;
	fastcgi_pass unix:/run/php/php7.3-fpm.sock;
	fastcgi_split_path_info ^(.+\.php)(.*)$;
	fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
	fastcgi_cache_bypass $http_secret_header $skip_cache;
	fastcgi_no_cache $skip_cache;
	fastcgi_cache WORDPRESS;
        fastcgi_cache_key "$scheme$request_method$host$mobile_user$request_uri";
	fastcgi_cache_valid 404 1m;
	fastcgi_cache_valid 60m;
}

还可以在Nginx的自定义指令中使用此变量,以便在块中使用类似的代码来跟踪移动访问者$ mobile_useraccess_loghttp {

log_format customuseragent '[$time_local] $remote_addr $upstream_cache_status '
                           '$mobile_user $request_method '
                           '"$request_uri" $upstream_response_time';

然后在nginx虚拟主机配置中,可以自定义

access_log /var/log/nginx/customuseragent.log customuseragent;

重启nginx,然后可以查看自定义访问日志文件

tail -f /var/log/nginx/customuseragent.log

应该看到类似这样的输出

[04/Apr/2020:13:41:10 +0000] 123.119.135.210 MISS ZHANZHANGB_MOBILE GET "/" 0.272
[04/Apr/2020:13:41:11 +0000] 123.119.135.210 MISS ZHANZHANGB_MOBILE GET "/wordpress?lang=" 0.076
[04/Apr/2020:13:41:18 +0000] 123.119.135.210 - ZHANZHANGB_DESKTOP POST "/wp-admin/admin-ajax.php" 0.072
[04/Apr/2020:13:41:19 +0000] 86.101.236.78 HIT ZHANZHANGB_MOBILE GET "/" -
[04/Apr/2020:13:41:20 +0000] 86.101.236.78 HIT ZHANZHANGB_MOBILE GET "/wordpress?lang=" -
[04/Apr/2020:13:41:20 +0000] 86.101.236.78 MISS ZHANZHANGB_MOBILE GET "/851.?is_single=" 0.112
[04/Apr/2020:13:41:23 +0000] 2804:1b2:81:b271:85d2:778c:a336:fd28 MISS ZHANZHANGB_DESKTOP GET "/851.html" 0.140
[04/Apr/2020:13:41:27 +0000] 2804:1b2:81:b271:85d2:778c:a336:fd28 HIT ZHANZHANGB_DESKTOP GET "/625.html?lang=" -
[04/Apr/2020:13:41:27 +0000] 2804:1b2:81:b271:85d2:778c:a336:fd28 MISS ZHANZHANGB_DESKTOP GET "/625.html?is_single=808" 0.060
[04/Apr/2020:13:41:28 +0000] 10.164.151.46 MISS ZHANZHANGB_DESKTOP GET "/blog/" 0.144
[04/Apr/2020:13:41:30 +0000] 10.164.151.46 HIT ZHANZHANGB_DESKTOP GET "//blog/5?lang=" -
[04/Apr/2020:13:41:30 +0000] 10.164.151.46 MISS ZHANZHANGB_DESKTOP GET "/blog/3?is_single=6539" 0.076

原文地址:https://www.zhanzhangb.com/879.html

WordPress教程

如何禁用WordPress的自动更新

2024-1-24 2:40:49

WordPress教程

语音搜索将进一步改变网购的未来

2024-1-24 2:40:51

个人中心
今日签到
有新私信 私信列表
搜索