nginx location介绍 Nginx 中的 Location 指令 是NginxHttpCoreModule中重要指令。Location 指令,是用来为匹配的 URI 进行配置,URI 即语法中的”/uri/”,可以是字符串或正则表达式。但如果要使用正则表达式,则必须指定前缀。 nginx location语法 基本语法:location [=|~|~*|^~] /uri/ { … }
= 严格匹配。如果这个查询匹配,那么将停止搜索并立即处理此请求。 nginx location应用实例
以下是补充: Nginx Location基本语法 location
syntax: location [=|~|~*|^~] /uri/ { … }
default: no
context: server
This directive allows different configurations depending on the URI. It can be configured using both conventional strings and regular expressions. To use regular expressions, you must use the prefix ~* for case insensitive match and ~ for case sensitive match.
To determine which location directive matches a particular query, the conventional strings are checked first. Conventional strings match the beginning portion of the query and are case-sensitive - the most specific match will be used (see below on how nginx determines this). Afterwards, regular expressions are checked in the order defined in the configuration file. The first regular expression to match the query will stop the search. If no regular expression matches are found, the result from the convention string search is used.
There are two ways to modify this behavior. The first is to use the prefix “=”, which matches an exact query only. If the query matches, then searching stops and the request is handled immediately. For example, if the request “/” occurs frequently, then using “location = /” will expedite the processing of this request.
The second is to use the prefix ^~. This prefix is used with a conventional string and tells nginx to not check regular expressions if the path provided is a match. For instance, “location ^~ /images/” would halt searching if the query begins with /images/ - all regular expression directives would not be checked.
Furthermore it is important to know that NGINX does the comparison not URL encoded, so if you have a URL like “/images/%20/test” then use “/images/ /test” to determine the location.
To summarize, the order in which directives are checked is as follows:
1. Directives with the = prefix that match the query exactly. If found, searching stops.
Example:
location = / {
Example requests: *
/ -> configuration A
/documents/document.html -> configuration B
/images/1.gif -> configuration C /documents/1.jpg -> configuration D
Note that you could define these 4 configurations in any order and the results would remain the same. Nginx Location 语法,与简单配置
一、介绍Nginx是俄罗斯人编写的十分轻量级的HTTP服务器,Nginx,它的发音为“engine X”, 是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/POP3/SMTP 代理服务器. 示例一:
location / { } 示例二:
location =/ {} 示例三:
location ~* \.(gif|jpg|jpeg)$ { 三、ReWrite语法
last - 基本上都用这个Flag。 例: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
四、Redirect语法
server { listen 80; server_name start.it.net.cn; index index.html index.php; root html; if ($http_host !~ "^star\.igrow\.cn$" { rewrite ^(.*) http://star.it.net.cn$1 redirect; } }
五、防盗链
location ~* \.(gif|jpg|swf)$ { valid_referers none blocked start.it.net.cn sta.it.net.cn; if ($invalid_referer) { rewrite ^/ http://$host/logo.png; } }
六、根据文件类型设置过期时间
location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ { if (-f $request_filename) { expires 1h; break; } }
七、禁止访问某个目录
location ~* \.(txt|doc)${ root /data/www/wwwroot/linuxtone/test; deny all; } (责任编辑:IT) |