Common nginx rewrite examples

Source: Internet
Author: User

Common nginx rewrite examples
I believe that in daily O & M, if you use nginx as the front-end reverse proxy server, you will love and hate nginx rewrite because you have done it, after the developer's jump needs are met, you will feel very good and really powerful, hate it because when you have some strange jump needs, you will have no clue, all kinds of debugging, and you may want to go online and worship Buddha, of course, there are also many classic examples of nginx rewrite on the Internet, but I don't feel very helpful to my work. Below are some rewrite examples I encountered at work. To share with you.
1. Access to the http://www.abc.com is normal, now need to access abc.com, directly jump to the http://www.abc.com.

 
 
  1. server {
  2. listen 80;
  3. server_name abc.com;
  4. access_log off;
  5. rewrite (.+) $scheme://www.$host$1 permanent;
  6. }
2, if your domain name is http://www.abc.com, now want to change a domain name http://www.def.com, and do not want to lose the access to the previous domain name, you need to configure the jump.
First, you need to write both www.abc.com and www.def.cn in your server_name.
Perform the following Configuration:
 
 
  1. location / {
  2. if ($host = 'www.abc.com')
  3. {
  4. rewrite ^/(.*)$ http://www.def.com/$1 permanent;
  5. }
  6. }
3. Jump to the maintenance page. For example, if you want to mount a maintenance page today, you want to access the page normally except for some specific IP addresses because you need to test the page. The rest of the IP addresses are used to access the maintenance page. This requirement seems to have been written before, but it is a bit different today. Add the following content to the server:
 
 
  1. set $rewrite true;
  2. if ($remote_addr = "xxx.xxx.xxx.xxx") {
  3. set $rewrite false;
  4. }
  5. if ($rewrite = true) {
  6. rewrite (.+) http://www.abc.com/maintenance.html;
  7. }
  8. location = /maintenance.html {
  9. root /var/vhost;
  10. }
4. For example, now your domain name is a http://bbs.abc.com, and now the access to the post under this domain name are directed to http://www.abc.com/bbs, and the token behind the domain name must also be consistent.
For example, logging
 
 
  1. location /post {
  2. rewrite (.+) http://www.abc.com/bbs$1 permanent;
  3. }
5. uri-based jump
 
 
  1. if ($request_uri ~* ^/note\.php\?product_code=(.*)$) {
  2. rewrite (.*) http://www.abc.com permanent;
  3. }
 
 
  1. if ($request_uri ~ ^/forum-(140|141|142|143|144|145|150|151|152|153|154|155|156|157|158|159|200|222|223|224|225|226|227|228)-(\d+).html$) {
  2. rewrite (.*) http://www.abc.com/list permanent;
  3. }
6. Redirection Based on files under the Directory
 
 
  1. location ~* /upload/.*\.php$ {
  2. return 404;
  3. }
7. jump from the most common url to the url
 
 
  1. location ~* ^/ab/maintain/main.html {
  2. rewrite (.+) http://www.abc.com/maintain/main.html permanent;
  3. }
Although the examples are incomplete, there may be some other better jump examples, which will be added later. If something is wrong, you are welcome to make a picture.







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.