Nginx-location Configuration Guide

Source: Internet
Author: User

Syntax rules: 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.

Multiple location configuration in the case of the matching order (reference, not actually verified, try to know, do not have to rigidly, for reference only):

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:

1

Location =/{Exact match, must be 127.0.0.1/

#规则A

}

Location =/login {Exact match, must be 127.0.0.1/login

#规则B

}

Location ^~/static/{Non-exact match and case insensitive, such as 127.0.0.1/STATIC/JS.

#规则C

}

Location ~. (GIF|JPG|PNG|JS|CSS) $ {case-sensitive, ending with Gif,jpg,js

#规则D

}

Location ~*. png$ {case-insensitive, matches the. png End of

#规则E

}

Location!~. xhtml$ {case-sensitive, matching is not. xhtml ends with

#规则F

}

Location!~*. xhtml$ {

#规则G

}

Location/{Anything is possible

#规则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.

So in actual use, the individual feels that there are at least three matching rule definitions, as follows:

#这里是直接转发给后端应用服务器了, can also be a static home page

# First Required rule

Location =/{

Proxy_pass Http://tomcat:8080/index

}

# The second required rule is to handle a static file request, which is Nginx's strength as an HTTP server

# There are two configuration modes, directory matching or suffix matching, either one or use

Location ^~/static/{

root/webroot/static/;

}

Location ~*. (Gif|jpg|jpeg|png|css|js|ico) $ {

root/webroot/res/;

}

#第三个规则就是通用规则, used to forward dynamic requests to backend application servers

#非静态文件请求就默认是动态请求, according to the actual grasp

#毕竟目前的一些框架的流行, with a. php,.jsp suffix is rare.

Location/{

Proxy_pass http://tomcat:8080/

}

#直接匹配网站根, through the domain name to visit the site home more frequently, using this will speed up processing, the official website said.

#这里是直接转发给后端应用服务器了, can also be a static home page

# First Required rule

Location =/{

Proxy_pass Http://tomcat:8080/index

}

# The second required rule is to handle a static file request, which is Nginx's strength as an HTTP server

# There are two configuration modes, directory matching or suffix matching, either one or use

Location ^~/static/{

root/webroot/static/;

}

Location ~*. (Gif|jpg|jpeg|png|css|js|ico) $ {

root/webroot/res/;

}

#第三个规则就是通用规则, used to forward dynamic requests to backend application servers

#非静态文件请求就默认是动态请求, according to the actual grasp

#毕竟目前的一些框架的流行, with a. php,.jsp suffix is rare.

Location/{

Proxy_pass http://tomcat:8080/

}

Other information that has not been tested:

Third, rewrite grammar
last– basically use this flag.
break– abort Rewirte, do not continue to match
redirect– returns the HTTP status of a temporary REDIRECT 302
permanent– returns the HTTP status of permanent redirection 301
1. The following expressions can be used to judge:
-F and!-f to determine if a file exists
-D and!-d to determine if a directory exists
-E and!-e to determine if a file or directory exists
-X and!-x to determine if a file is executable
2. The following is a global variable that can be used as a judge
Example: http://localhost:88/test1/test2/test.php
$host: localhost
$server _port:88
$request _uri:http://localhost:88/test1/test2/test.php
$document _uri:/test1/test2/test.php
$document _root:d:nginx/html
$request _filename:d:nginx/html/test1/test2/test.php
Iv. redirect Grammar
server {
Listen 80;
server_name start.igrow.cn;
Index index.html index.php;
root HTML;
if ($http _host!~ "^star.igrow.cn$&quot {
Rewrite ^ (. *) http://star.igrow.cn$1 redirect;
}
}
Five, the anti-theft chain location ~*. (gif|jpg|swf) $ {
Valid_referers none blocked start.igrow.cn sta.igrow.cn;
if ($invalid _referer) {
Rewrite ^/http:/$host/logo.png;
}
}
Vi. setting the expiration time based on the file type
Location ~*. (js|css|jpg|jpeg|gif|png|swf) $ {
if (-f $request _filename) {
Expires 1h;
Break
}
}
Vii. prohibit access to a directory
Location ~*. (Txt|doc) ${
Root/data/www/wwwroot/linuxtone/test;
Deny all;
}

+ + Some of the available global variables
$args
$content _length
$content _type
$document _root
$document _uri
$host
$http _user_agent
$http _cookie
$limit _rate
$request _body_file
$request _method
$remote _addr
$remote _port
$remote _user
$request _filename
$request _uri
$query _string
$scheme
$server _protocol
$server _addr
$server _name
$server _port
$uri

Nginx-location Configuration Guide

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.