nginx 的 if 逻辑运算判断PC或移动端

“proxy_pass” cannot have URI part in location given by regular expression, or inside named location

nginx无法在proxy_pass指令中处理所需的URI部分,因为位于指定的位置(因此是错误消息)。这是因为nginx是以模块化的方式构建的,每个配置块都是由各个模块在各个阶段读取的。proxy_pass在以下情况下,指令中不能有URI :

  1. 正则表达式位置
  2. 命名的地点
  3. if 块

正确示例:

  1. server {
  2.     listen       81;
  3.     server_name  localhost;
  4.     location / {
  5.         proxy_pass https://www.baidu.com;
  6.     }
  7. }
  8. server {
  9.     listen       80;
  10.     server_name  localhost;
  11.     location / {
  12.         root   e:\www;
  13.         index  index.html index.htm;
  14.     }
  15.     location ^~ /s {
  16.         set $is_mobile 0;
  17.         if ($http_user_agent ~* (mobile|nokia|iphone|ipad|android|samsung|htc|blackberry)) {
  18.             set $is_mobile 1;
  19.         }
  20.         # PC端
  21.         if ($is_mobile = 0) {
  22.             proxy_pass http://localhost:81;
  23.         }
  24.         # 移动端
  25.         if ($is_mobile = 1) {
  26.             proxy_pass http://170.106.148.50;
  27.         }
  28.     }
  29. }

PC端:

移动端:

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注