1.准备两个静态资源目录,一个为手机版,一个为PC版,例如:
/www/mobile/dist
/www/pc/dist
2.在Nginx配置两个location,分别对应手机和PC
# 手机访问资源
location /mobile {
alias /www/mobile/dist;
}
# PC访问资源
location /pc {
alias /www/pc/dist;
}
3.通过User Agent区分手机和PC请求:
location / {
if ($http_user_agent ~* '(Android|webOS|iPhone|iPod|BlackBerry)') {
rewrite ^(.*)$ /mobile$1 break;
}
if ($http_user_agent ~* '(Windows|Linux|Mac)') {
rewrite ^(.*)$ /pc$1 break;
}
}