Introduction to Nginx

Source: Internet
Author: User

1, Nginx How to handle a request

IP, Domain name processing

server {

Listen default_server; Adding Default_server is a default server

server_name nginx.org www.nginx.org

.....

}

server {

Listen 80;

server_name Nginx.net www.nginx.net

}

Nginx only tests the host in the request header to determine which server the request is routed to, and if the host does not match or the host does not exist, it is handled by the default server

Matching rules for server_name

The domain name follows the following priority rules:
1, the name of the complete match.
2. The name begins with a file wildcard: *.example.com.
3, the name ends with a file wildcard: www.example.*.
4. Use the name of the regular expression.

Block processing of requests for ambiguous host names

If the client request header, there may be a case of host ambiguity, if you do not want to handle such requests, you can define the default server to discard

server{

Listen default_server;

server_name _;

return 444;

}

1 Select a nonexistent server name and return a special nonstandard code 444 to close this connection.

If listen 192.168.1.45:80 the IP match first, then server_name match

Handling of URIs

Location [=|~|~*|^~]/uri/{...}
= Start with exact match
^~ begins by indicating that the URI begins with a regular string and is understood to match the URL path. Nginx does not encode the URL, so the request is/STATIC/20%/AA, which can be matched to the rule ^~/static//aa (note is a space).
~ Start indicates a case-sensitive regular match
~* start indicates a case-insensitive regular match
!~ and!~* are case-insensitive and match-insensitive.
/generic match, any request will be matched to.
In the case of multiple location configurations, the matching order is:
First match =, next match ^~, followed by the regular match in the order of the file, and finally to the/General match. When a match succeeds, the match is stopped and the request is processed according to the current matching rule.
example, there are the following matching rules:
Location =/{
#规则A
}
Location =/login {
#规则B
}
Location ^~/static/{
#规则C
}
Location ~ \. (GIF|JPG|PNG|JS|CSS) $ {
#规则D
}
Location ~* \. (gif|jpg|js|css|png) $ {
#规则E
}
Location!~ \.xhtml$ {
#规则F
}
Location!~* \.xhtml$ {
#规则G
}
Location/{
#规则H
}
The resulting effect is as follows:
Access root directory/, e.g. http://localhost/will match rule a
Access Http://localhost/login will match rule b,http://localhost/register then match rule h
Access http://localhost/static/a.html will match rule C
Accessing Http://localhost/a.gif, Http://localhost/b.jpg will match rule D and rule e, but rule D order takes precedence and rule e does not work, and http://localhost/static/c.png First match to rule C
Access to Http://localhost/a.PNG matches rule e without matching rule d because rule e is not case sensitive.
Access http://localhost/a.xhtml does not match rule f and rule g,http://localhost/a.xhtml does not match rule G because it is case insensitive. Rule f, rule g belongs to the exclusion method, conforms to the matching rule but does not match, so think about where the actual application will be used.
Access to the http://localhost/category/id/1111 is finally matched to the rule H, because the above rules do not match, this time should be nginx forwarding request to the backend application server, such as fastcgi (PHP), Tomcat (JSP), Nginx as the direction proxy server exists.

Rewrite rules:

Implements rewriting or redirection of URLs. Rewrite can only be placed in server{},loacation{}, if{}, and only the string that is behind the domain name is used, for example http://www.facebank.cn/a/we/index.php?userid=1 &password=222

can only work on/a/we/index.php. Grammarrewrite regex replacement [flag]

Execution order:

    1. Execute the rewrite directive for the server block
    2. Perform a location match
    3. Executes the rewrite directive in the selected location

If one of the step URIs is overridden, the repeating loop executes 1-3 until the actual file is found and the loop exceeds 10 times, then the internal server error error is returned

HTTP {# define Image Log formatLog_format Imagelog‘[$time _local] '$image _file‘ ‘$image _type‘ ‘$body _bytes_sent‘ ‘$status;# Turn on rewrite logRewrite_logOnserver {root/home/www;Location/{# Rewrite rule informationError_log Logs/rewrite.logNotice# Note that there is a ' single quote ' to avoid {}Rewrite' ^/images/([a-z]{2})/([a-z0-9]{5})/(. *) \. (png|jpg|gif) $ '/data?file=$.$4;# Note that you cannot add the "last" argument to the above rule, otherwise the following set instruction will not execute set $image _file $ $ ; set $image _type $4;} Location/data { # Specifies the log format for the picture to analyze the picture type and size access_log logs/images.log mian; root/data/images; # Apply the variables defined earlier. Judge the first file is not in, not to judge the directory is not in, if not yet jump to the last URL try_files/$arg _file/image404.html;} Location =/image404.html { # picture does not exist returns specific information return 404 "image not found\n";}}   


to form as/images/ef/uh7b3/test.pngThe request, rewritten to/data?file=test.png, and then match tolocation /data, look first/data/images/test.pngThe file does not exist, if there is a normal response, if it does not exist then rewrite tryfiles to the new image404 location and return the 404 status code directly.

Introduction to Nginx

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.