Nginx Rewrite common example _php tutorial

Source: Internet
Author: User

Nginx Rewrite common examples


I believe that everyone in the daily operation of the work if you use Nginx as the front-end reverse proxy server, you will be the rewrite of nginx and love and hate, love it is because you have done it, completed the developer's jump demand you will feel very cool, feel really strong, Hate it is because when some strange jump demand when you will not have the clue, a variety of debugging, the Internet praying are uncertain when really want to die of heart have, of course, there are many classic examples of nginx rewrite, but I feel that my work is not much help. Here are some examples of rewrite that I have encountered in my work. To share with you.
First, Access http://www.abc.com Normal, now need to visit abc.com, jump directly to http://www.abc.com.
 
  
  
  1. server {
  2. Listen 80;
  3. server_name abc.com;
  4. Access_log off;
  5. Rewrite (. +) $scheme://www. $host $ permanent;
  6. }
Second, 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 previous domain access volume, need to configure jump.
The first thing you need to do is write www.abc.com and www.def.cn in your server_name.
Then make the following configuration:
 
  
  
  1. Location/{
  2. if ($host = ' www.abc.com ')
  3. {
  4. Rewrite ^/(. *) $ http://www.def.com/$1 permanent;
  5. }
  6. }
Third, jump maintenance pages, such as today's maintenance, want to hang a maintenance page, I hope that in addition to certain specific IP address can be accessed normally, because the need to test. The rest of the IP addresses are Access maintenance pages, which I've written before, but it's a little 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. }
Four, for example, now your domain name is http://bbs.abc.com, now will be the domain name below the post access to Http://www.abc.com/bbs, pay attention to the parameters after the jump to remain consistent.
For example: http://bbs.abc.com/post/addpost/913.shtml jump to Http://www.abc.com/bbs/post/addpost/913.shtml
 
  
  
  1. Location/post {
  2. Rewrite (. +) http://www.abc.com/bbs$1 permanent;
  3. }
V. URI-based jumps
 
  
  
  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. }
Vi. jumps based on the file below the directory
 
  
  
  1. Location ~*/upload/.*\.php$ {
  2. return 404;
  3. }
Seven, the most common URL to the URL jump
 
  
  
  1. Location ~* ^/ab/maintain/main.html {
  2. Rewrite (. +) http://www.abc.com/maintain/main.html permanent;
  3. }
Although examples are not exhaustive, there may be some other better jump examples to add later. There is no place to welcome everyone to shoot bricks.







http://www.bkjia.com/PHPjc/1119674.html www.bkjia.com true http://www.bkjia.com/PHPjc/1119674.html techarticle Nginx Rewrite Common examples believe that everyone in the daily operation and maintenance work if you use Nginx as the front-end reverse proxy server, you will be the rewrite of Nginx love and Hate, love it is because you engage in ...

  • 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.