最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

nginx location語(yǔ)法使用介紹

 更新時(shí)間:2015年01月23日 14:36:29   投稿:mdxy-dxy  
Nginx 中的 Location 指令 是NginxHttpCoreModule中重要指令。Location 指令,是用來(lái)為匹配的 URI 進(jìn)行配置,URI 即語(yǔ)法中的”/uri/”,可以是字符串或正則表達(dá)式。但如果要使用正則表達(dá)式,則必須指定前綴

nginx location介紹

Nginx 中的 Location 指令 是NginxHttpCoreModule中重要指令。Location 指令,是用來(lái)為匹配的 URI 進(jìn)行配置,URI 即語(yǔ)法中的”/uri/”,可以是字符串或正則表達(dá)式。但如果要使用正則表達(dá)式,則必須指定前綴。

nginx location語(yǔ)法

基本語(yǔ)法:location [=|~|~*|^~] /uri/ { … }

= 嚴(yán)格匹配。如果這個(gè)查詢匹配,那么將停止搜索并立即處理此請(qǐng)求。
~ 為區(qū)分大小寫匹配(可用正則表達(dá)式)
~* 為不區(qū)分大小寫匹配(可用正則表達(dá)式)
!~和!~*分別為區(qū)分大小寫不匹配及不區(qū)分大小寫不匹配
^~ 如果把這個(gè)前綴用于一個(gè)常規(guī)字符串,那么告訴nginx 如果路徑匹配那么不測(cè)試正則表達(dá)式。

nginx location應(yīng)用實(shí)例

location = / {
	# 只匹配 / 查詢。
	
}
location / {
	# 匹配任何查詢,因?yàn)樗姓?qǐng)求都已 / 開頭。但是正則表達(dá)式規(guī)則和長(zhǎng)的塊規(guī)則將被優(yōu)先和查詢匹配。
	
}
location ^~ /images/ {
	# 匹配任何已 /images/ 開頭的任何查詢并且停止搜索。任何正則表達(dá)式將不會(huì)被測(cè)試。
	
}
location ~* \.(gif|jpg|jpeg)$ {
	# 匹配任何已 gif、jpg 或 jpeg 結(jié)尾的請(qǐng)求。
	
}
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) {
  #根據(jù)文件類型設(shè)置過(guò)期時(shí)間
  expires  1h;
  break;
}
}
 
location ~* \.(txt|doc)${ 
	#禁止訪問(wèn)某個(gè)目錄
  root /data/www/wwwroot/linuxtone/test;
  deny all;
}

以下是補(bǔ)充:

Nginx Location基本語(yǔ)法

location

syntax: location [=|~|~*|^~] /uri/ { … }
語(yǔ)法:location [=|~|~*|^~] /uri/ { … }

default: no
默認(rèn):否

context: server
上下文: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.
這個(gè)指令隨URL不同而接受不同的結(jié)構(gòu)。你可以配置使用常規(guī)字符串和正則表達(dá)式。如果使用正則表達(dá)式,你必須使用 ~* 前綴選擇不區(qū)分大小寫的匹配或者 ~ 選擇區(qū)分大小寫的匹配。

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.
確定 哪個(gè)location 指令匹配一個(gè)特定指令,常規(guī)字符串第一個(gè)測(cè)試。常規(guī)字符串匹配請(qǐng)求的開始部分并且區(qū)分大小寫,最明確的匹配將會(huì)被使用(查看下文明白 nginx 怎么確定它)。然后正則表達(dá)式按照配置文件里的順序測(cè)試。找到第一個(gè)比配的正則表達(dá)式將停止搜索。如果沒(méi)有找到匹配的正則表達(dá)式,使用常規(guī)字符串的結(jié)果。

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 “l(fā)ocation = /” will expedite the processing of this request.
有兩個(gè)方法修改這個(gè)行為。第一個(gè)方法是使用 “=”前綴,將只執(zhí)行嚴(yán)格匹配。如果這個(gè)查詢匹配,那么將停止搜索并立即處理這個(gè)請(qǐng)求。例子:如果經(jīng)常發(fā)生”/”請(qǐng)求,那么使用 “l(fā)ocation = /” 將加速處理這個(gè)請(qǐng)求。

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, “l(fā)ocation ^~ /images/” would halt searching if the query begins with /images/ - all regular expression directives would not be checked.
第二個(gè)是使用 ^~ 前綴。如果把這個(gè)前綴用于一個(gè)常規(guī)字符串那么告訴nginx 如果路徑匹配那么不測(cè)試正則表達(dá)式。

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.
而且它重要在于 NGINX 做比較沒(méi)有 URL 編碼,所以如果你有一個(gè) URL 鏈接'/images/%20/test' , 那么使用 “images/ /test” 限定location。

To summarize, the order in which directives are checked is as follows:
總結(jié),指令按下列順序被接受:

1. Directives with the = prefix that match the query exactly. If found, searching stops.
1. = 前綴的指令嚴(yán)格匹配這個(gè)查詢。如果找到,停止搜索。
2. All remaining directives with conventional strings, longest match first. If this match used the ^~ prefix, searching stops.
2. 剩下的常規(guī)字符串,長(zhǎng)的在前。如果這個(gè)匹配使用 ^~ 前綴,搜索停止。
3. Regular expressions, in order of definition in the configuration file.
3. 正則表達(dá)式,按配置文件里的順序。
4. If #3 yielded a match, that result is used. Else the match from #2 is used.
4. 如果第三步產(chǎn)生匹配,則使用這個(gè)結(jié)果。否則使用第二步的匹配結(jié)果。

Example:
例子:

location = / {
# matches the query / only.
# 只匹配 / 查詢。
[ configuration A ]
}
location / {
# matches any query, since all queries begin with /, but regular
# expressions and any longer conventional blocks will be
# matched first.
# 匹配任何查詢,因?yàn)樗姓?qǐng)求都已 / 開頭。但是正則表達(dá)式規(guī)則和長(zhǎng)的塊規(guī)則將被優(yōu)先和查詢匹配。
[ configuration B ]
}
location ^~ /images/ {
# matches any query beginning with /images/ and halts searching,
# so regular expressions will not be checked.
# 匹配任何已 /images/ 開頭的任何查詢并且停止搜索。任何正則表達(dá)式將不會(huì)被測(cè)試。
[ configuration C ]
}
location ~* ".(gif|jpg|jpeg)$ {
# matches any request ending in gif, jpg, or jpeg. However, all
# requests to the /images/ directory will be handled by
# Configuration C.
# 匹配任何已 gif、jpg 或 jpeg 結(jié)尾的請(qǐng)求。然而所有 /images/ 目錄的請(qǐng)求將使用 Configuration C。
[ configuration D ]
}

Example requests:
例子請(qǐng)求:

*

/ -> 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.
注意:按任意順序定義這4個(gè)配置結(jié)果將仍然一樣。

Nginx Location 語(yǔ)法,與簡(jiǎn)單配置

一、介紹Nginx是俄羅斯人編寫的十分輕量級(jí)的HTTP服務(wù)器,Nginx,它的發(fā)音為“engine X”, 是一個(gè)高性能的HTTP和反向代理服務(wù)器,同時(shí)也是一個(gè)IMAP/POP3/SMTP 代理服務(wù)器.
二、Location語(yǔ)法語(yǔ)法:location [=|~|~*|^~] /uri/ { … }
注:
1、~   為區(qū)分大小寫匹配
2、~* 為不區(qū)分大小寫匹配
3、!~和!~*分別為區(qū)分大小寫不匹配及不區(qū)分大小寫不匹配

示例一:

location  / { }
匹配任何查詢,因?yàn)樗姓?qǐng)求都以 / 開頭。但是正則表達(dá)式規(guī)則將被優(yōu)先和查詢匹配。

示例二:

location =/ {}
僅僅匹配/

示例三:

location ~* \.(gif|jpg|jpeg)$ {
rewrite \.(gif|jpg)$ /logo.png;

注:不區(qū)分大小寫匹配任何以gif,jpg,jpeg結(jié)尾的文件

三、ReWrite語(yǔ)法

last - 基本上都用這個(gè)Flag。
break - 中止Rewirte,不在繼續(xù)匹配
redirect - 返回臨時(shí)重定向的HTTP狀態(tài)302
permanent - 返回永久重定向的HTTP狀態(tài)301
1、下面是可以用來(lái)判斷的表達(dá)式:
-f和!-f用來(lái)判斷是否存在文件
-d和!-d用來(lái)判斷是否存在目錄
-e和!-e用來(lái)判斷是否存在文件或目錄
-x和!-x用來(lái)判斷文件是否可執(zhí)行
2、下面是可以用作判斷的全局變量

例:http://localhost:88/test1/test2/test.php

復(fù)制代碼 代碼如下:

$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語(yǔ)法

復(fù)制代碼 代碼如下:

    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;
    }
    }

五、防盜鏈

復(fù)制代碼 代碼如下:

location ~* \.(gif|jpg|swf)$ {
  valid_referers none blocked start.igrow.cn sta.igrow.cn;
  if ($invalid_referer) {
  rewrite ^/ http://$host/logo.png;
  }
}

六、根據(jù)文件類型設(shè)置過(guò)期時(shí)間

復(fù)制代碼 代碼如下:

location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
if (-f $request_filename) {
   expires    1h;
   break;
}
}

七、禁止訪問(wèn)某個(gè)目錄

復(fù)制代碼 代碼如下:

location ~* \.(txt|doc)${
      root /data/www/wwwroot/linuxtone/test;
    deny all;
}

相關(guān)文章

  • nginx反向代理服務(wù)器及負(fù)載均衡服務(wù)配置方法

    nginx反向代理服務(wù)器及負(fù)載均衡服務(wù)配置方法

    正向代理一般是在客戶端設(shè)置代理服務(wù)器,通過(guò)代理服務(wù)器轉(zhuǎn)發(fā)請(qǐng)求,最終訪問(wèn)到目標(biāo)服務(wù)器,這篇文章主要介紹了nginx反向代理服務(wù)器及負(fù)載均衡服務(wù)配置方法,需要的朋友可以參考下
    2023-12-12
  • Nginx中反向代理+負(fù)載均衡+服務(wù)器宕機(jī)解決辦法詳解

    Nginx中反向代理+負(fù)載均衡+服務(wù)器宕機(jī)解決辦法詳解

    這篇文章主要介紹了Nginx中反向代理+負(fù)載均衡+服務(wù)器宕機(jī)解決辦法詳解,反向代理保證系統(tǒng)安全,不暴露服務(wù)器IP,利用nginx服務(wù)器,利用內(nèi)網(wǎng)ip進(jìn)行訪問(wèn),避免出現(xiàn)攻擊服務(wù)器的情況,需要的朋友可以參考下
    2024-01-01
  • nginx關(guān)閉favicon.ico、robots.txt日志記錄配置

    nginx關(guān)閉favicon.ico、robots.txt日志記錄配置

    這篇文章主要介紹了nginx關(guān)閉favicon.ico、robots.txt日志記錄配置,同時(shí)提供了不允許訪問(wèn)某些隱藏文件的配置方法,需要的朋友可以參考下
    2014-05-05
  • 在阿里云Centos下如何安裝Nginx

    在阿里云Centos下如何安裝Nginx

    這篇文章主要介紹了阿里云Centos下如何安裝Nginx,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
    2016-10-10
  • 解決nginx“504?Gateway?Time-out”錯(cuò)誤

    解決nginx“504?Gateway?Time-out”錯(cuò)誤

    這篇文章介紹了解決nginx“504?Gateway?Time-out”錯(cuò)誤的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-12-12
  • nginx服務(wù)器的下載安裝與使用詳解

    nginx服務(wù)器的下載安裝與使用詳解

    這篇文章主要介紹了nginx服務(wù)器的下載安裝與使用,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-07-07
  • Nginx中server_name的超詳細(xì)使用指南

    Nginx中server_name的超詳細(xì)使用指南

    這篇文章主要介紹了Nginx的server_name指令及其在DNS解析和多域名配置中的應(yīng)用,包括如何使用通配符和正則表達(dá)式進(jìn)行匹配,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2025-03-03
  • nginx多域名轉(zhuǎn)發(fā)的實(shí)現(xiàn)

    nginx多域名轉(zhuǎn)發(fā)的實(shí)現(xiàn)

    本文主要介紹了nginx多域名轉(zhuǎn)發(fā)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • 詳解常用的nginx rewrite重寫規(guī)則

    詳解常用的nginx rewrite重寫規(guī)則

    這篇文章主要介紹了詳解常用的nginx rewrite重寫規(guī)則,Nginx的rewrite功能是使用nginx提供的全局變量或自己設(shè)置的變量,結(jié)合正則表達(dá)式和標(biāo)志位實(shí)現(xiàn)url重寫以及重定向。感興趣的可以一起來(lái)了解一下
    2019-03-03
  • nginx proxy_set_header的具體實(shí)現(xiàn)

    nginx proxy_set_header的具體實(shí)現(xiàn)

    proxy_set_header?是 Nginx 配置中的一個(gè)重要指令,本文主要介紹了nginx proxy_set_header的具體實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-07-07

最新評(píng)論

阳城县| 甘泉县| 乌鲁木齐县| 勐海县| 北川| 佳木斯市| 福泉市| 观塘区| 肥西县| 孟津县| 隆化县| 霞浦县| 改则县| 大冶市| 蒙自县| 房山区| 鸡西市| 乐安县| 都匀市| 秭归县| 辽宁省| 阿克苏市| 定兴县| 墨竹工卡县| 乐业县| 天门市| 丰镇市| 新绛县| 剑阁县| 廊坊市| 宝兴县| 宝山区| 安阳县| 彝良县| 宁蒗| 五大连池市| 新建县| 神农架林区| 武义县| 葫芦岛市| 舒兰市|