date: 2020-03-03title: nginx根据http_useragent判断是手机端还是PC端 #标题
tags: nginx判断PC或手机端 #标签
categories: nginx # 分类

nginx的location判断用户端是手机还是PC端配置。

配置文件如下:

  1. # 判断 pc 和 mobile 的 H5
  2. location / {
  3. set $is_mobile false; #设置一个初始值
  4. if ( $http_cookie ~* "ACCESS_TERMINAL=mobile" ) { #判断匹配手机端
  5. set $is_mobile true;
  6. }
  7. if ($http_user_agent ~* (android|ip(ad|hone|od)|kindle|blackberry|windows\s(ce|phone))) { #匹配手机端类型
  8. set $is_mobile true;
  9. }
  10. if ($is_mobile = true) {
  11. root /usr/local/openresty/nginx/html/mobile/;
  12. break;
  13. }
  14. root /usr/local/openresty/nginx/html/pc/;
  15. }