Nginx location configuration method summary

Source: Internet
Author: User
Tags http request regular expression

1. location [= | ~ | ~ * | ^ ~ | @]/Uri /{...}

1 ,~ Case-sensitive matching
2 ,~ * Case-insensitive match
3 ,!~ Case-insensitive
4 ,!~ * Case-insensitive
5. = indicates exact match.
6./Universal Match. All requests will be matched

If there are multiple location configurations, first =, then ^ ~, Others are matched in the order of writing, followed by general match /;

The code is as follows: Copy code

Nginx does not encode the url. For example,/20%/can be // matched. Example: Match all requests because the request starts with a slash (/).
Location /{
# Rule
}

Match only/
Location = /{
# Rule
}
Match URLs ending with .jpg).png).gif
Location ^ ~ . (Jpg | png | gif) $ {
# Rule
}

The matching process is as follows:

Step 1: perform string matching to find the most appropriate one
Step 2: perform regular expression matching and find the first matching
Step 3: If the second step matches, use the result of the second step. Otherwise, use the result of the first step.
It can be seen from the above that the priority of the regular expression is higher.

II. rewrite syntax

Last-this Flag is basically used.
Break-abort Rewirte and do not continue matching
Redirect-return the HTTP status 302 of the temporary redirect
Permanent-returns the HTTP status 301 for permanent redirection
1. The following is a judgment expression:
-F and! -F is used to determine whether a file exists.
-D and! -D is used to determine whether a directory exists.
-E and! -E is used to determine whether a file or directory exists.
-X and! -X is used to determine whether a file is executable.

Global variables:

2. The following is an example of global variables that can be used for judgment.

The code is as follows: Copy code

Example: http://www.111cn.net/post. php
$ Host: www.111cn.net
$ Server_port: 80
$ Request_uri: http://www.111cn.net/post. php
$ Document_uri:/post. php
$ Document_root:/home/web
$ Request_filename:/home/web/blog/post. php

Configuration example:

The code is as follows: Copy code

Server {
Listen: 80;
Server_name: www.111cn.net;
Root/home/web/blog;
Location ~ *. (Jpg | png | css | js) $ {
Expires 10 h;
Break;
    }
}

Example

The code is as follows: Copy code

Location = /{
# Match/query only.
[Configuration A]
}
Location /{
# Match any query because all requests start. However, regular expression rules and long block rules are preferentially matched with queries.
[Configuration B]
}

Location ^ ~ /Images /{
# Match any queries starting with/images/and stop searching. No regular expression will be tested.
[Configuration C]
}

Location ~ *. (Gif | jpg | jpeg) $ {
# Match any request that has ended with gif, jpg, or jpeg. However, Configuration C is used for all/images/directory requests.
[Configuration D]
}

In the actual test, the following is a complete nginx domain name configuration. return is used to differentiate which location to match.

The code is as follows: Copy code

Server {
Listen 80;
Server_name want.abc.com;
Charset gbk;
Access_log logs/want_access.log main;
Error_log logs/want_error.log;

Location =/{return 100 ;}
Location/{return 101 ;}
Location ~ /Images/{return 102 ;}
Location ^ ~ /Images/{return 103 ;}
Location ~ *. (Gif | jpg | jpeg) $ {return 105 ;}
Location/a {return 106 ;}
Location ~ /A {return 107 ;}
}

Explanation: perform a full match of "=" first. If it matches, the search will stop.
~ # Wget want.abc.com/
-- 16:09:23 -- http://want.abc.com/
Parsing host want.abc.com... 127.0.0.1
Connecting want.abc.com | 127.0.0.1 |: 80... connected.
An HTTP request has been sent and is waiting for response... 100
16:09:23 error 100: (not described ).

Explanation: regular expressions do not match, that is, strings are used for matching.
# Wget want.abc.com/document/document.html
-- 16:12:03 -- http://want.abc.com/document/document.html
Parsing host want.abc.com... 127.0.0.1
Connecting want.abc.com | 127.0.0.1 |: 80... connected.
An HTTP request has been sent and is waiting for response... 101
16:12:03 error 101: (not described ).


Explanation: The string is matched, and "^ ~ ", Stop searching
# Wget want.abc.com/images/a
-- 16:12:25 -- http://want.abc.com/images/a
Parsing host want.abc.com... 127.0.0.1
Connecting want.abc.com | 127.0.0.1 |: 80... connected.
An HTTP request has been sent and is waiting for response... 103
16:12:25 error 103: (not described ).

Explanation: The regular expression matches and the search is stopped.
# Wget want.abc.com/abc/1.gif
-- 16:14:23 -- yun_qi_img/1.gif
Parsing host want.abc.com... 127.0.0.1
Connecting want.abc.com | 127.0.0.1 |: 80... connected.
An HTTP request has been sent and is waiting for response... 105
16:14:23 error 105: (not described ).

Explanation: regular expressions match strings, but regular expressions match strings first.
# Wget want.abc.com/
-- 16:18:15 -- http://want.abc.com/
Parsing host want.abc.com... 127.0.0.1
Connecting want.abc.com | 127.0.0.1 |: 80... connected.
An HTTP request has been sent and is waiting for response... 107
16:18:15 error 107: (not described)


Example request:

The code is as follows: Copy code
/-> Configuration
/Documents/document.html-> configuration B
/Images/1.gif-> configuration C
/Documents/1.jpg-> configuration D

Note: The results of defining the four configurations in any order will remain the same.

(Location =)> (full location path)> (location ^ ~ Path)> (location ~ *,~ Regular expressions)> (location part Start Path) regular expressions affect matching based on the preference order in the configuration file. The preference matches the preference. Others match the preference based on the matching length.

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.