关于nginx:Nginx区分移动PC端-响应不同页面

25次阅读

共计 396 个字符,预计需要花费 1 分钟才能阅读完成。

背景:


/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;
}

正文完
 0