1. 目標
nginx 反向代理,路徑映射的過程是什么?如何配置路徑映射規則?
2、location 路徑匹配
2.1 匹配規則:
location 路徑正則匹配:
符號 | 說明 |
---|---|
~ |
正則匹配,區分大小寫 |
~* |
正則匹配,不區分大小寫 |
^~ |
普通字符匹配,如果該選項匹配,則,只匹配改選項,不再向下匹配其他選項 |
= |
普通字符匹配,精確匹配 |
@ |
定義一個命名的 location,用於內部定向,例如 error_page,try_files |
2.2 匹配優先級:
路徑匹配,優先級:(跟 location 的書寫順序關系不大)
-
精確匹配:
=
前綴的指令嚴格匹配這個查詢。如果找到,停止搜索。
-
普通字符匹配:
所有剩下的常規字符串,最長的匹配。
如果這個匹配使用
^〜
前綴,搜索停止。 -
正則匹配:
正則表達式,在配置文件中定義的順序,匹配到一個結果,搜索停止;
-
默認匹配:
如果第3條規則產生匹配的話,結果被使用。
否則,如同從第2條規則被使用。
2.3 舉例
通過一個實例,簡單說明一下匹配優先級:
location = / { # 精確匹配 / ,主機名后面不能帶任何字符串 [ configuration A ] } location / { # 因為所有的地址都以 / 開頭,所以這條規則將匹配到所有請求 # 但是正則和最長字符串會優先匹配 [ configuration B ] } location /documents/ { # 匹配任何以 /documents/ 開頭的地址,匹配符合以后,還要繼續往下搜索 # 只有后面的正則表達式沒有匹配到時,這一條才會采用這一條 [ configuration C ] } location ~ /documents/Abc { # 匹配任何以 /documents/ 開頭的地址,匹配符合以后,還要繼續往下搜索 # 只有后面的正則表達式沒有匹配到時,這一條才會采用這一條 [ configuration CC ] } location ^~ /images/ { # 匹配任何以 /images/ 開頭的地址,匹配符合以后,停止往下搜索正則,采用這一條。 [ configuration D ] } location ~* \.(gif|jpg|jpeg)$ { # 匹配所有以 gif,jpg或jpeg 結尾的請求 # 然而,所有請求 /images/ 下的圖片會被 config D 處理,因為 ^~ 到達不了這一條正則 [ configuration E ] } location /images/ { # 字符匹配到 /images/,繼續往下,會發現 ^~ 存在 [ configuration F ] } location /images/abc { # 最長字符匹配到 /images/abc,繼續往下,會發現 ^~ 存在 # F與G的放置順序是沒有關系的 [ configuration G ] } location ~ /images/abc/ { # 只有去掉 config D 才有效:先最長匹配 config G 開頭的地址,繼續往下搜索,匹配到這一條正則,采用 [ configuration H ] } location ~* /js/.*/\.js
按照上面的location寫法,以下的匹配示例成立:
-
/
-> config A:精確完全匹配,即使/index.html也匹配不了
-
/downloads/download.html
-> config B:匹配B以后,往下沒有任何匹配,采用B
-
/images/1.gif
-> configuration D:匹配到F,往下匹配到D,停止往下
-
/images/abc/def
-> config D:最長匹配到G,往下匹配D,停止往下你可以看到 任何以/images/開頭的都會匹配到D並停止,FG寫在這里是沒有任何意義的,H是永遠輪不到的,這里只是為了說明匹配順序
-
/documents/document.html
-> config C:匹配到C,往下沒有任何匹配,采用C
-
/documents/1.jpg
-> configuration E:匹配到C,往下正則匹配到E
-
/documents/Abc.jpg
-> config CC:最長匹配到C,往下正則順序匹配到CC,不會往下到E
延伸。。。
1. 靜態資源緩存:
# 緩存設置 (注意if的用法) location ~ .*\.(gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ { root /usr/share/nginx/html; access_log off; set $flag 0; if ($request_uri ~ "(version.js|envConsole.js)") { set $flag 1; } if ($flag = 0) { expires 1h; } if ($flag = 1) { add_header Cache-Control no-cache; } }
location ^~ /xxx/ {
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
alias /usr/share/nginx/html/xxx/;
# 注意這里的順序 break 后,不在往下找,不再匹配下面的if,否則還會走下面的 if(目的是如果匹配到abc.js或者efg.js則走此配置,不緩存,不在往下找;
if ($request_filename ~ "(abc.js|efg.js)") {
add_header Cache-Control "private, no-store, no-cache, must-revalidate, proxy-revalidate";
break;
}
#如果是其他名的js則繼續走下面if,緩存7天)
if ($request_filename ~* .*\.(?:gif|jpg|jpeg|bmp|png|ico|txt|css|js|svg)$) {
expires 7d;
}
try_files $uri $uri/ /index.html;
}
2、error_page 自定義error.conf (自定義路徑,我放在了/etc/nginx/user.d/目錄下了,user.d是新建的目錄)
# *****************錯誤頁面定義****************** # # 只需在 server 里引入: # include /etc/nginx/user.d/*.conf; 即可 error_page 404 @404error; location @404error { #rewrite ^(.*) $scheme://$host:$server_port/errorpage/40x break; rewrite ^(.*) $scheme://$host/errorpage/40x/ break; } error_page 500 502 503 504 @500error; location @500error { #rewrite ^(.*) $scheme://$host:$server_port/errorpage/50x break; rewrite ^(.*) $scheme://$host/errorpage/50x/ break; } location ^~ /errorpage/40x/ { alias /usr/share/nginx/html/error_page/; index 404.html; expires 30d; } location ^~ /errorpage/50x/ { alias /usr/share/nginx/html/error_page/; index 50x.html; expires 30d; }
server中引入:
server {
listen 80;
server_name X.X.X.X;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
include /etc/nginx/user.d/error.conf;
location / {
add_header Access-Control-Allow-Origin http://xxx.xx.com;
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
root /opt/xxx/app;
try_files $uri $uri/ /index.html;
}
location ~^/(images|image|javascript|js|css|static|json|templets|upload)/ {
root /opt/xxx/app;
access_log off;
expires 30d;
}
}