Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An example of how nginx implements if nesting

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 272

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report