Notice:
Location matching rules and priorities - key points
Variables commonly used in nginx - required to master
rewrite redirect function - grasp, understand
location matching rules and priorities
location match
Common nginx regular expressions (matched file content)
"." : any single character
^:: match the starting position of the input string
$: matches the end of the input string
*: Matches the preceding character zero or more times. For example, "ol*" can match "o" and "ol", "oll"
+: Matches the previous character one or more times. For example, "ol+" can match "ol" and "oll", "olll", but cannot match "o"
?: Match the preceding character zero or one time, for example, "do(es)?" can match "do" or "does", "?" is equivalent to "{0, 1}"
\: escape character, mark the following character as a special character or a literal character or a back reference, such as "\n" matches a newline character, and "\$" matches "$"
[a-zA-Z0-9]: Match all uppercase and lowercase letters or any number
[c]: matches a single character c
{n}: Repeat n times
{n,m}: Repeat n to m times
(): the beginning and end position of the expression
|: OR operator, logical or
URI: is a string identifier used to identify abstract or physical resources, such as files, pictures, videos, etc.
It consists of multiple components, including protocol, host name, port number, path, etc. For example, http://www.kgc.com:8080/index.html is a URI.
In Nginx, the matched object is usually part of the URI
www.ky30.com/images/1.jpg
matching /images/.jpg
The location matches the category of the URI
match category | display method | priority |
exact search | location = / {...} | highest priority |
regular match | location ~ / {...} | secondly |
general match | location / {...} | lowest priority |
locationPriority:
1. First exact match =
2, followed by prefix matching ^~
3. Followed by the regular matching in the order of the file ~or~*
4. Then match the prefix match without any modification
5. Finally, hand over/universal matching
location = > location ^~ > location ~* > location /test/ > location /
Matched rules:
=: perform an exact match of ordinary characters, that is, an exact match
^~: Indicates ordinary character matching, using prefix matching, if the match is successful, it will no longer match other locations
~: case-sensitive match
~*: case-insensitive match
~$: end position
!~: case-sensitive match negation
!~*: case-insensitive match negation
The more accurate the location matching rule is, the more it matches (the higher the priority)
Matching rules used in actual production
first mandatory rule
Directly match the root of the website. It is more frequent to access the homepage of the website through the domain name. Using this will speed up the processing. For example, the official website can be a static homepage, which can be directly forwarded to the back-end application server
location = / {
root html;
index index.html index. htm;
}
The second mandatory rule is to process static file requests, which is the strength of nginx as an http server
There are two configuration modes, directory matching or suffix matching, choose one or use it together
location ^~ /static/ {
root /webroot/static/;
}
The third rule: match the image and video used
location ~* \.(html|gif|jpg|jpeg|png|css|js|ico)$ {}
Fourth, general rules
It is used to forward dynamic requests with .php and .jsp suffixes to the backend application server. Non-static file requests are dynamic requests by default.
location / {
proxy_pass http://tomcat_server;
proxy_pass: specify proxy, forward dynamic request,
}
rewrite:
The role of rewrite:
Use the global variables provided by nginx or the variables set by yourself, combined with regular expressions and flags to realize url rewriting and redirection
Nginx's built-in variables:
$uri: Requested URI, excluding host and query parameters.
$request_uri: Request URI, including host and query parameters.
$args: Query parameter part, that is, the content after ?.
$query_string: The entire query string, including ?.
$host: The requested hostname.
$http_user_agent: The requested User-Agent header information, used to indicate the requested client browser and operating system.
$http_referer: Referer header information of the request, indicating the source URL of the current page.
$remote_addr: IP address of the client.
$remote_port: The port number of the client.
$server_addr: The IP address of the server.
$server_port: The port number of the server.
$request_method: HTTP method of the request, such as GET, POST, etc.
$content_type: The Content-Type header information of the request, indicating the MIME type of the request body.
$content_length: The Content-Length header information of the request, indicating the length of the request body.
$scheme: The protocol of the request, usually http or https.
$request_filename: The requested file name, which is used to specify the actual file path of the request.
$document_root: The root directory of the current request.
$server_name: server name, used to match the server_name directive of the server block.
default_ type text/plain
Indicates that if there is no other place to display, the header field of content type, the default response is text/plain
Plain text format:
text/html: the default response type, which is what we call a page
text/plain : plain text format, similar to txt files
text/css : type of css style sheet
text/javascript: java script, js: front-end file can also be understood as a program file parsed by java
The rewrite execution sequence is as follows
- Execute the rewrite instruction inside the server block.
- Perform location matching.
- Executes the rewrite command in the selected location.
rewrite syntax
rewrite <regex> <replacement> [flag]
- regex: Indicates regular matching rules
- replacement : Indicates the content after the jump
- flag : Indicates the flag tag supported by rewrite
Flag tag description
Flag bit:
1. permanent: permanent redirection, return code point 301,
Permanent variable url search engine will transfer his weight and ranking to the new URL
2. redirect: Temporary redirection, the displayed return code is 302
For short-term changes (site maintenance, or upgrade and update) whether the search engine transfers weight and ranking to the new URL
304: Get local cache
3, break: Function:
It is a redirect but will not change the uri, and it will only be requested once, and it can be terminated by jumping out of the current match
4. last: After the matching of this rule is completed, continue to match the new location URI rule downward as long as there is last, and continue to match. You need to pay attention when configuring to prevent an endless loop
"rewrite or internal redirection cycle while processing
It indicates that a rewrite or internal redirect loop occurred while processing the request