Nginx区分移动/PC端, 响应不同页面
文章目录
背景:
有
/www/wwwroot/example-h5
/www/wwwroot/example-pc
两个目录, 需要根据用户端, 响应不同的页面
解决方案
根据用户UA不同, 响应正确的页面给用户
# 默认响应移动端静态资源
set $root_dir /www/wwwroot/example-h5;
# !~* 是一条正则表达式,!表示取反, ~*表示不区分大小写
if ($http_user_agent !~* "(mobile|nokia|iphone|ipad|android|samsung|htc|blackberry)") {
# 非移动端UA, 响应PC静态资源
set $root_dir /www/wwwroot/example-pc;
}
location / {
# root 使用上面定义的变量
root $root_dir;
try_files $uri $uri/ /index.html =404;
#index index.html;
}
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论