Get the App
SLTechnology News&Howtos  ›  Servers  › 

An example of how nginx implements if nesting

Shulou Source: shulou.com Published: 2022-06-02 03:01:21 10月12日 Update

Nginx does not support if nesting, nor does it allow logical judgment in if. The following error will be reported:

Nginx: [emerg] "if" directive is not allowed

When the business needs multiple conditional judgments, it can be realized with the help of intermediate variables.

For example, our website has multiple sub-domain names on the pc side, while there is only one domain name on the mobile end. The corresponding relationship is as follows:

Www.test.com-- > m.test.comsub1.test.com-- > m.test.com/sub1sub2.test.com-- > m.test.com/sub2sub3.test.com-- > m.test.com/sub3

Effect to be achieved: 301 jumps to the corresponding mobile domain name when the pc domain name is accessed on the mobile side

The rewriting rules for nginx are as follows:

# whether it is the mobile set $mobile 0 if ($http_user_agent ~ * (mobile | nokia | iphone | ipad | android | samsung | htc | blackberry) {set $mobile 1;} # acquire the subdomain set $prefix 1 if ($host ~ * "sub1.test.com") {set $prefix 2;} if ($host ~ * "sub2.test.com") {set $prefix 3;} if ($host ~ * "sub3.test.com") {set $prefix 4;} set $sign "$mobile} ${prefix}" If ($sign = 11) {rewrite ^ (. *) http://m.test.com$1 permanent;} if ($sign = 12) {rewrite ^ (. *) http://m.test.com/sub1$1 permanent;} if ($sign = 13) {rewrite ^ (. *) http://m.test.com/sub2$1 permanent;} if ($sign = 14) {rewrite ^ (. *) http://m.test.com/sub3$1 permanent;}

The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.

Tags: Mobile domain name multiple subdomain support business content variables only that is effects conditions websites rules logic errors reporting learning help methods Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Apple macOS Redmi Shulou Technology