在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
nginx location介绍 Nginx 中的 Location 指令 是NginxHttpCoreModule中重要指令。Location 指令,是用来为匹配的 URI 进行配置,URI 即语法中的”/uri/”,可以是字符串或正则表达式。但如果要使用正则表达式,则必须指定前缀。 nginx location语法 基本语法:location [=|~|~*|^~] /uri/ { … } = 严格匹配。如果这个查询匹配,那么将停止搜索并立即处理此请求。 nginx location应用实例 location = / { # 只匹配 / 查询。 } location / { # 匹配任何查询,因为所有请求都已 / 开头。但是正则表达式规则和长的块规则将被优先和查询匹配。 } location ^~ /images/ { # 匹配任何已 /images/ 开头的任何查询并且停止搜索。任何正则表达式将不会被测试。 } location ~* \.(gif|jpg|jpeg)$ { # 匹配任何已 gif、jpg 或 jpeg 结尾的请求。 } location ~* \.(gif|jpg|swf)$ { valid_referers none blocked start.igrow.cn sta.igrow.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; } 以下是补充: 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.igrow.cn; index index.html index.php; root html; if ($http_host !~ "^star\.igrow\.cn$" { rewrite ^(.*) http://star.igrow.cn$1 redirect; } } 五、防盗链 复制代码 代码如下: location ~* \.(gif|jpg|swf)$ { valid_referers none blocked start.igrow.cn sta.igrow.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; } |
请发表评论