On the rules of Nginx rewrite

Source: Internet
Author: User

nginx rewrite (nginx URL address rewrite)
Rewrite main function is to implement the URL rewrite, nginx Rewrite rules Pcre,perl compatible Regular expression syntax rules matching, if you need nginx Rewrite function, before compiling nginx, need to compile and install Pcre Library.  Through the rewrite rule, you can implement the URL of the specification, and make the URL steering and select configuration according to the variables. If directive
Rule syntax: [Plain]View PlainCopy
    1. if ($http _user_agent ~msie) {
    2. Rewrite ^ (. *) $/msie/$1 break;
    3. }
    4. if (!-f$request_filename) {
    5. Rewrite ^/img/(. *) $/site/$host/images/$1 last;
    6. }


Rewrite syntax rules:
Variable name:
Variable names can use the "=" or "! =" operator
~ symbol indicates case-sensitive letter matching
The ~* symbol indicates a case-insensitive letter match
!~ and!~ are the opposite of ~!~
-F and!-f to determine if a file exists
-D and!-d to determine if a directory exists
-E and!-e to determine whether a file or directory exists
-X and!-x to determine if a file can be executed
Also supports the $ to $9 positional parameter
return instruction
Example: If you access a URL that ends with a. Sh. Bash, return a status code of 403
[Plain]View PlainCopy
    1. Location ~. *\. (Sh|bash)? $
    2. {
    3. return 403;
    4. }

Rewrite directive [Plain]View PlainCopy
    1. The last parameter of the rewrite directive is the flag flag, and the supported flag flags are mainly in the following categories:
    2. Last: Equivalent to Apache Reed (L) mark, indicating completion of rewrite;
    3. break; After the rule match is complete, the match is terminated and no longer matches the following rule
    4. Redirect: Returns 302 temporary redirect, the browser address will show the URL address after the jump
    5. Permanent: Return 301 Permanent Redirect, the browser address bar will show the URL address after the jump
    6. Last and break are used to implement URL rewriting, browser address bar URL address unchanged

Example: the access/b jumps to the/bbs directory:
[Plain]View PlainCopy
    1. location/b {
    2. AutoIndex on;
    3. Alias/usr/local/nginx/html/redhat;
    4. Rewrite ^/b/?$/bbs permanent;
    5. }
    6. Location/bbs {
    7. AutoIndex on;
    8. Alias/usr/local/nginx/html/bbs;
    9. }
Rewrite rule Writing instance
1, rewrite the original directory to access/b to/bbs
Core statement: [Plain]View PlainCopy
    1. Rewrite ^/b/?$/bbs permannet;


2, depending on the browser will be different results.
[Plain]View PlainCopy
    1. if ($http _user_agent ~ Firefox) {
    2. Rewrite ^ (. *) $/firefox/$1 break;
    3. }
    4. if ($http _user_agent ~ MSIE) {
    5. Rewrite ^ (. *) $/msie/$1 break;
    6. }
    7. if ($http _user_agent ~ Chrome) {
    8. Rewrite ^ (. *) $/chrome/$1 break;
    9. }
3. Prevent hotlinking: According to referer information to prevent hotlinking, the code is as follows:
[Plain]View PlainCopy
    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. Implementing the Domain name jump:
[Plain]View PlainCopy
    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. }

http://blog.csdn.net/xifeijian/article/details/20955253

On the rules of Nginx rewrite (turn)

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.