Nginx multiple conditional judgment

problem description

A website binds two domain names to access (123.com abc.com), and then involves a jump problem (123.com to www.123.com abc.com to www.abc.com) and http to https

configuration of Server segment on PC:

set $flag 0;
if ($scheme = http) {

 set $flag "${flag}1";

}

if ($host = "www.123.com") {

set $flag "${flag}1";
}


if ($host = "www.abc.com"){

set $flag "${flag}0";

}

if ($flag ="011"){
 return 301 https://www.123.com$request_uri;
}

if ($flag ="010"){
 return 301 https://www.abc.com$request_uri;
}

configuration in mobile server:

set $flag 0;
if ($scheme = http) {

 set $flag "${flag}1";

}

if ($host = "m.123.com") {

set $flag "${flag}1";
}


if ($host = "m.abc.com"){

set $flag "${flag}0";

}

if ($flag ="011"){
 return 301 https://m.123.com$request_uri;
}

if ($flag ="010"){
 return 301 https://m.abc.com$request_uri;
}

then when you check the configuration, you will prompt
invalid condition "$flag" in Mobile profile

Aug.18,2021

  1. it is recommended to write the jump as 302 when debugging the configuration, otherwise it will be troublesome and misleading when the client cleans up the cache.
  2. does not understand the logic of the configuration. If you just xxx.com jump www.xxx.com , you can CNAME the past directly during DNS parsing (when there is no MX rule), or jump with URL, so that nginx only needs to deal with http jump to https (CNAME scheme), and the latter doesn't even have to do this;
  3. Mobile jump, judge the uA and then jump according to the conditions. It seems that there is no need to separate a configuration file. In addition, it is not recommended to use 301, otherwise you will always move the end after you use the browser to bring down the web page.
Menu