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

Nginx rewrite enterprise actual combat operation

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

Share

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

Practical Operation of Enterprise rewrite

Nginx rewrite configuration (the article is written by the team members)

Our public account

Our official website https://wuguiyunwei.com

QQ group 602183872

I believe that if you use nginx as the front-end reverse proxy server in your daily operation and maintenance work, you will love and hate nginx's rewrite. You love it because you have taken care of it, and you will feel good and powerful after completing the developer's jump requirements. Hate it because when you have some strange needs, you will have no idea, do all kinds of debugging, and you can't even go online to pray for God and Buddha. Of course, there are many classic examples of nginx rewrite on the Internet, but I don't think it's very helpful to my work. Here are some examples of rewrite that I have encountered in my work. To share with you.

1. Access to http://www.abc.com is normal. Now you need to access abc.com and jump to http://www.abc.com directly.

Server {listen 80th serverroomname abc.com;access_log off;rewrite (. +) $scheme://www.$host$1 permanent;}

Second, if your domain name is http://www.abc.com, now you want to change the domain name http://www.def.com, and do not want to lose the number of visits to the previous domain name, you need to configure redirection.

First, you need to write both www.abc.com and www.def.cn in your server_name.

Then make the following configuration:

Location / {if ($host = 'www.abc.com') {rewrite ^ / (. *) $http://www.def.com/$1 permanent;}}

Third, jump to the maintenance page, such as today's maintenance, want to hang a maintenance page, hope that except for some specific IP addresses can be accessed normally, because it needs to be tested. The rest of the IP addresses are to visit the maintenance page, which I seem to have written before, but today is a little different. Add the following to the server:

Set $rewrite true;if ($remote_addr = "xxx.xxx.xxx.xxx") {set $rewrite false;} if ($rewrite = true) {rewrite (. +) http://www.abc.com/maintenance.html;}location = / maintenance.html {root / var/vhost;}

4. For example, if your domain name is http://bbs.abc.com, now redirect all the posts below this domain name to http://www.abc.com/bbs, and note that the parameters after the jump should be the same.

For example: http://bbs.abc.com/post/addpost/913.shtml jumps to http://www.abc.com/bbs/post/addpost/913.shtml

Location / post {rewrite (. +) http://www.abc.com/bbs$1 permanent;}

Fifth, jump based on uri

If ($request_uri ~ * ^ / note\ .php\? product_code= (. *) $) {rewrite (. *) http://www.abc.com permanent;} if ($request_uri ~ ^ / forum-) {rewrite (. *) http://www.abc.com/list permanent;}

6. based on the jump of the files under the directory

Location ~ * / upload/.*\ .php ${return 404;}

The most common jump from url to url

Location ~ * ^ / ab/maintain/main.html {rewrite (. +) http://www.abc.com/maintain/main.html permanent;}

8. The parameters after the directory remain unchanged.

Rewrite ^ / assets/upload/ (. *) $http://assets.kgc.cn/upload/$1 permanent

9. Multiple matches

If ($request_uri ~ ^ / baseuiue (.shtml) | ^ / zhuanti/baseuiue (.shtml)) {rewrite (. +) http://123.abc.cn/zhuanti/baseuiue.shtml permanent;}

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: 0

*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