首先
Nginx不支持 and、or、&&、|| 這類語法;且不支持if的多重嵌套,例如:
if (aaa) { if (bbb) { exec @ccc; } }
多重判斷如何實現呢?
最近要做個配置,將移動設備訪問網頁時跳轉到手機版面。需要判斷2個部分才做跳轉:一、客戶端來源為移動設備;二、訪問指定域名業務時。
實現方法一
每個域名配置單獨的server{},這樣配置比較簡明;但缺點是配置文件會寫很長,要修改多次。(比較啰嗦)
實現方法二
全部域名配置一個server{},進行多重判斷;這樣配置可能稍微復雜一點,但配置文件不會那么啰嗦。
server { listen 80; server_name _; set $domain $host; root /webapp/$domain; ...其它略 if ($http_user_agent ~* "UCBrowser|Android|Iphone|Ipad|Ipod|BlackBerry|Windows Phone|Symbian(.*)Series60/3|Symbian(.*)Series60/5") { set $mobile '1'; } if ($host !~* ^(app1|app2|app3)\.google\.com) { set $mobile '0'; } if ($mobile = 1) { rewrite "^/index.html$" /mobile/index.html last; } ...其它略 }
后續
移動設備的 http_user_agent 還不全,慢慢收集~