【nginx運維基礎(5)】Nginx的location攻略

概述

location 有"定位"的意思, 根據Uri來進行不一樣的定位.
在虛擬主機的配置中,是必不可少的,location能夠把網站的不一樣部分,定位到不一樣的處理方式上.僞靜態,反向代理,負載均衡等等都離不開location.css

語法

location [=|~|~*|^~] patt {}

中括號能夠不寫任何參數,此時稱爲通常匹配,也能夠寫參數.所以,大類型能夠分爲3種:html

location = patt {} [精準匹配]
location patt{}  [通常匹配]
location ~ patt{} [正則匹配]

匹配說明

精準匹配 =

徹底匹配指定的 pattern ,且這裏的 pattern 被限制成簡單的字符串,也就是說這裏不能使用正則表達式.nginx

server {
    server_name website.com;
    location = /abcd {
    […]
    }
}

http://website.com/abcd        # 正好徹底匹配
http://website.com/ABCD        # 若是運行 Nginx server 的系統自己對大小寫不敏感,好比 Windows ,那麼也匹配
http://website.com/abcd?param1    # 忽略查詢串參數(query string arguments),也一樣匹配
http://website.com/abcd/    # 不匹配,由於末尾存在反斜槓(trailing slash),Nginx 不認爲這種狀況是徹底匹配
http://website.com/abcde    # 不匹配,由於不是徹底匹配

通常匹配 (None)

能夠理解爲左前綴匹配(like pattern%),這種狀況下,匹配那些以指定的 patern 開頭的 URI,注意這裏的 URI 只能是普通字符串,不能使用正則表達式.web

server {
    server_name website.com;
    location /abcd {
    […]
    }
}

http://website.com/abcd        # 正好徹底匹配
http://website.com/ABCD        # 若是運行 Nginx server 的系統自己對大小寫不敏感,好比 Windows ,那麼也匹配
http://website.com/abcd?param1     # 忽略查詢串參數(query string arguments),這裏就是 /abcd 後面的 ?param1
http://website.com/abcd/    # 末尾存在反斜槓(trailing slash)也屬於匹配範圍內
http://website.com/abcde    # 仍然匹配,由於 URI 是以 pattern 開頭的

正則匹配 ~

對大小寫敏感(在window上無效),且 pattern 須是正則表達式正則表達式

server {
    server_name website.com;
    location ~ ^/abcd$ {
    […]
    }
}


http://website.com/abcd        # 徹底匹配

http://website.com/ABCD        # 不匹配,~ 對大小寫是敏感的

http://website.com/abcd?param1     # 忽略查詢串參數(query string arguments),這裏就是 /abcd 後面的 ?param1 

http://website.com/abcd/    # 不匹配,由於末尾存在反斜槓(trailing slash),並不匹配正則表達式 ^/abcd$

http://website.com/abcde    # 不匹配正則表達式 ^/abcd$

正則匹配 ~*

不區分大小寫,pattern 須是正則表達式apache

server {
    server_name website.com;
    location ~* ^/abcd$ {
    […]
    }
}

http://website.com/abcd        # 徹底匹配

http://website.com/ABCD        # 匹配,這就是它不區分大小寫的特性

http://website.com/abcd?param1     # 忽略查詢串參數(query string arguments),這裏就是 /abcd 後面的 ?param1 

http://website.com/abcd/    # 不匹配,由於末尾存在反斜槓(trailing slash),並不匹配正則表達式 ^/abcd$

http://website.com/abcde    # 不匹配正則表達式 ^/abcd$

正則匹配 ^~

匹配狀況相似通常匹配,以指定匹配模式開頭的 URI 被匹配後端

!~和!~*

分別爲區分大小寫不匹配及不區分大小寫不匹配的正則服務器

通用匹配 /

任何請求都會匹配到.cookie

特殊匹配 @

用於定義一個 Location 塊,且該塊不能被外部 Client 所訪問,只能被 Nginx 內部配置指令所訪問,好比 try_files or error_page負載均衡

匹配優先級

http://www.test.com/ 從域名後面(uri:http請求行的第二列)開始匹配,也就是/,匹配原則通常都是左前綴匹配,location / {} 可以匹配全部HTTP 請求,由於任何HTTP 請求都必然是以'/'開始的,可是,正則location 和其餘任何比'/'更長的普通location (location / {} 是普通location 裏面最短的,所以其餘任何普通location 都會比它更長,固然location = / {} 和 location ^~ / {} 是同樣長的)會優先匹,因而可知匹配的優先級能夠總結爲:

越詳細就越優先

是否是有點像css的選擇器?

Example1

# 首先看有沒有精準匹配,若是有,則中止匹配過程.
location = patt {
  config A
}
location / {
  root   /usr/local/nginx/html;
  index  index.html index.htm;
}

若是訪問http://test.com/
定位流程是 
1: 精準匹配中"/" ,獲得index頁爲index.htm
2: 再次訪問 /index.htm , 這次內部轉跳uri已是"/index.htm",根目錄爲/usr/local/nginx/html
3: 最終結果,訪問了 /usr/local/nginx/html/index.htm

Example2

location / {
   root   /usr/local/nginx/html;
   index  index.html index.htm;
}

location /foo {
    root /var/www/html;
    index index.html;
}

咱們訪問 http://test.com/foo
對於uri "/foo", 兩個location的patt,都能匹配他們
即 '/'能從左前綴匹配 '/foo', '/foo'也能左前綴匹配'/foo',
此時, 真正訪問 /var/www/html/index.html
緣由:'/foo'匹配的更長,所以使用之;

Example3

location ~ image {
    root /var/www/image;
    index index.html;
}

若是咱們訪問 http://test.com/image/logo.png
此時, "/" 與"/image/logo.png" 匹配
同時,"image"正則 與"image/logo.png"也能匹配,誰發揮做用?
正則表達式的成果將會使用.由於此時的正則表達式更詳細
圖片真正會訪問 /var/www/image/logo.png

再次總結優先級序以下:

1. =
2. (None)    前提是 pattern 徹底匹配 URI 的狀況(不是隻匹配 URI 的頭部,這點很重要)
3. ^~
4. ~ 或 ~*
5. (None) pattern 匹配 URI 的頭部

貌似與location的書寫順序無關? 但實際上仍是有關係的

# 配置一
server {

 listen       9090;
 server_name  localhost;

 location ~ \.html$ {
     allow all; 
 }  

 location ~ ^/prefix/.*\.html$ {
     deny all;  
 }  

}


# 配置二
server {

   listen       9090;
   server_name  localhost;

   location ~ ^/prefix/.*\.html$ {
       deny all;  
   }  

  location ~ \.html$ {
       allow all; 
   } 
}
URI 請求 配置一 配置二
curl http://localhost:9090/regextest.html 404 Not Found 404 Not Found
curl http://localhost:9090/prefix/regextest.html 404 Not Found 403 Forbidden

Location ~ ^/prefix/.*.html$ {deny all;} 表示正則 location 對於以 /prefix/ 開頭, .html 結尾的全部 URI 請求,都拒絕訪問; location ~.html${allow all;} 表示正則 location 對於以 .html 結尾的 URI 請求,都容許訪問. 實際上,prefix 的是 ~.html$ 的子集.

在"配置一 "下,兩個請求都匹配上 location ~.html$ {allow all;} ,而且中止後面的搜索,因而都容許訪問, 404 Not Found ;在"配置二 "下, /regextest.html 沒法匹配 prefix ,因而繼續搜索 ~.html$ ,容許訪問,因而 404 Not Found ;然而 /prefix/regextest.html 匹配到 prefix ,因而 deny all , 403 Forbidden .

優先級最終總結

1. =
2. (None)    前提是 pattern 徹底匹配 URI 的狀況(不是隻匹配 URI 的頭部,這點很重要)
3. ^~
4. ~ 或 ~*
5. (None) pattern 匹配 URI 的頭部

越詳細就越優先,可是同優先級的狀況下,按書寫順序誰先出現就以誰爲準(就近原則)

依然和css選擇器的優先級很像...

root&alias文件路徑配置

http://www.ttlsa.com/nginx/ng...

推薦必須的location

#直接匹配網站根,經過域名訪問網站首頁比較頻繁,使用這個會加速處理,官網如是說.
#這裏是直接轉發給後端應用服務器了,也能夠是一個靜態首頁
# 第一個必選規則
location = / {
    proxy_pass http://127.0.0.1:88; 
}

# 第二個必選規則是處理靜態文件請求,這是nginx做爲http服務器的強項
# 有兩種配置模式,目錄匹配或後綴匹配,任選其一或搭配使用
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
    expires      30d;
}

location ~ .*\.(js|css)?$
{
    expires      12h;
}

location ~ /\.  
{
    deny all; # 其餘的任意後綴都不讓其訪問;
}

#第三個規則就是通用規則,用來轉發動態請求到後端應用服務器
location /
{
    try_files $uri @apache; #try_files 將嘗試你列出的文件並設置內部文件指向
}

location @apache
{
    internal; # internal指令指定某個location只能被「內部的」請求調用,外部的調用請求會返回」Not found」
    proxy_pass http://127.0.0.1:88;
    
    proxy_connect_timeout 300s;
    proxy_send_timeout   900;
    proxy_read_timeout   900;
    proxy_buffer_size    32k;
    proxy_buffers     4 32k;
    proxy_busy_buffers_size 64k;
    proxy_redirect     off;
    proxy_hide_header  Vary;
    proxy_set_header   Accept-Encoding '';
    proxy_set_header   Host   $host;
    proxy_set_header   Referer $http_referer;
    proxy_set_header   Cookie $http_cookie;
    proxy_set_header   X-Real-IP  $remote_addr;
    proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
}
相關文章
相關標籤/搜索