107 nginx rewrite rules and aliases

background: Since I killed apache and replaced it with nginx, I have to enter a long list of urls every day, which is really forced to get out of safezone.....anyway, in order to solve this problem today, I decided to modify the nginx configuration and write aliases, It turns out that things are not as simple as I thought....

1.nginx rewrite rules

The main function of Rewrite is to implement URL rewriting. Nginx's Rewrite rules use Pcre, perl-compatible regular expression syntax rules to match

 

if directive
rule syntax:

  1. if ($http_user_agent ~MSIE){  
  2.   rewrite ^(.*)$/msie/$1 break;  
  3.  }  
  4.   
  5. if (!-f$request_filename){  
  6.     rewrite ^/img/(.*)$/site/$host/images/$1 last;  
  7.   }

rewrite syntax rules:
variable names:
    variable names can use the "=" or "!=" operators
     ~ symbol for case-sensitive matching
     ~* symbol for case-insensitive matching
     !~ and !~ are the opposite of ~ !~
     - f and !-f are used to determine whether a file exists
     -d and !-d are used to determine whether a directory exists
     -e and !-e are used to determine whether a file or directory exists
     -x and !-x are used to determine whether a file can be executed

      $1 to $9 positional parameters are also supported   

$1 represents the content within the preceding ( ). rewrite ^/(.*)$ http://www.test.cn/$1 last;

 

 

 

Return instruction
Example: If the accessed URL ends with .sh .bash, return status code 403

 

  1. location ~ .*\.(sh|bash)?$  
  2.  {  
  3.   return 403;  
  4.  }       

rewrite directive

  1. The last parameter of the rewrite command is the flag flag. The supported flag flags are as follows:  
  2. last : Equivalent to the Apache Reed (L) mark, indicating completion of rewrite;  
  3. break; After the matching of this rule is completed, the matching is terminated, and the following rules are no longer matched.  
  4. redirect: return 302 temporary redirection, the browser address will display the redirected URL address  
  5. permanent: Returns a 301 permanent redirect, and the browser address bar will display the redirected URL  
  6. Last and break are used to implement URL rewriting, and the URL address of the browser address bar remains unchanged.  

Example: Jump to the /bbs directory by accessing /b:

 

 

location /b   {  

  1.     autoindex is;  
  2.    alias /usr/local/nginx/html/redhat;         
  3.    rewrite ^/b/?$ /bbs permanent;  
  4.   
  5.    }  
  6.   location /bbs {  
  7.      autoindex is;  
  8.   alias /usr/local/nginx/html/bbs;  
  9.   }  

Writing example 1 of the rewrite rule , rewrites
the original directory to access /b to the
     core statement of /bbs:

  1. rewrite ^/b/?$ /bbs permannet;  



2, according to different browsers will get different results. if ($http_user_agent ~ Firefox) {  

  1.   rewrite ^(.*)$ /firefox/$1 break;  
  2.   }  
  3.   
  4.   if ($http_user_agent ~ MSIE) {  
  5.     rewrite ^(.*)$ /msie/$1 break;  
  6.    }  
  7.   
  8.  if ($http_user_agent ~ Chrome) {  
  9.       rewrite ^(.*)$ /chrome/$1 break;  
  10.  }   

 

3. Prevent hotlinking:

According to the Referer information to prevent hotlinking, the code is as follows:

  1. location ~*\.(gif|jpg|png|swf|flv)${  
  2. valid_referers none blocked www.cheng.com*.test.com;  
  3. if ($invalid_referer)  
  4.  rewrite ^/(.*) http://www.cheng.com/error.html           
  5.   }  


4. Realize domain name jump:

  1. server {  
  2.          listen       80;  
  3.          server_name  cheng.example.com;  
  4.          write ^(.*)$  http://zhang.example.com/$1 permanent;  
  5.          location / {  
  6.              root   html;  
  7.              index  index.html index.htm;  
  8.          }  

 

https://segmentfault.com/q/1010000006136366

I've been looking at nginx recently, but I'm not quite sure what is the use of alias? Both root and rewrite can achieve it, why use alias?

root /data/mydomain/main/web/;
location ~* ^/app(|/)$ {
        #alias /data/mydomain/main/public/app/;
        rewrite  ^/app(|/)$ /public/app/ last;
        expires 30s;
}

This is a matter of your own understanding. First of all, rewrite has nothing to do with root and alias.
Rewrite is for http requests, and the path in it is the address in the URL, which is the conversion between addresses.
While root and alias are settings for system file paths.
In the zone, root is used to set the root directory, and alias is used to reset the directory of the current file.

location /img/ {
    alias /var/www/image/;
}
#若按照上述配置的话,则访问/img/目录里面的文件时,ningx会自动去/var/www/image/目录找文件
location /img/ {
    root /var/www/image;
}
#若按照这种配置的话,则访问/img/目录下的文件时,nginx会去/var/www/image/img/目录下找文件。]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324389328&siteId=291194637