nginx中有關(guān) location 模塊的配置示例說明
當前位置:點晴教程→知識管理交流
→『 技術(shù)文檔交流 』
一、匹配規(guī)則 1.基礎(chǔ)匹配 location / { ... }:這是最基本的匹配,匹配所有請求。因為它沒有指定具體的文件或目錄,所以通常作為后備選項出現(xiàn)。 2.精確匹配 location = /exact/path { ... }:精確匹配這個路徑。如果請求的 URI 完全等于/exact/path,則使用這個location塊的配置處理此請求。這具有最高的優(yōu)先級。 3.前綴匹配 location /prefix/ { ... }:前綴匹配請求的 URI 的開始部分。如果請求的 URI 以/prefix/開始,這個location塊將被用來處理請求。 location ^~ /prefix/ { ... }:前綴匹配,但它會停止正則表達式匹配,即使正則表達式可能會更具體匹配。如果該location匹配,Nginx 不會考慮之后的正則location塊。 4.正則表達式匹配 location ~ /regex/ { ... }:大小寫敏感的正則匹配。 location ~* /regex/ { ... }:大小寫不敏感的正則匹配。 location / { ... }正則表達式匹配會在普通字符串前綴匹配后進行。如果有多個正則表達式location都匹配請求,則使用第一個匹配的location塊。 二、優(yōu)先級順序 Nginx 處理請求時 location 匹配的優(yōu)先級順序如下: 首先進行精確匹配location = 其次按文件中出現(xiàn)順序匹配所有正則表達式location ~和location ~* 然后進行最長的前綴匹配location ^~ 最后是通常的前綴匹配location /prefix/ 如果前面的匹配都沒有找到,就使用默認的location / 三、示例 1.精確匹配 server { listen 80; listen [::]:80; server_name welcome.com; access_log /etc/nginx/vhost/logs/access_welcome.log; error_log /etc/nginx/vhost/logs/error_welcome.log; location /test.html { root /usr/share/nginx/html; } location = /test.html { #通過 root 路徑,會將條件拼接進來 root /var/www/html; index index.html; } error_page 404 /404.html; location = /404.html { root /usr/share/nginx/test_html; } error_page 500 502 503 504 /50x.html; location = /50x.html { } } # 第一個和第二個location匹配條件一樣,都是/test.html # 但第二個為精準匹配到靜態(tài)路徑,因此第一個不會執(zhí)行,會執(zhí)行第二個。 server { listen 80; listen [::]:80; server_name welcome.com; access_log /etc/nginx/vhost/logs/access_welcome.log; error_log /etc/nginx/vhost/logs/error_welcome.log; location /test.html { #通過alias指定路徑,無需拼接條件 #alias指定的路徑結(jié)尾要加“/” alias /usr/share/nginx/test_html/; } error_page 404 /404.html; location = /404.html { root /usr/share/nginx/test_html; } error_page 500 502 503 504 /50x.html; location = /50x.html { } } # 指定靜態(tài)資源路徑除了使用關(guān)鍵字root,還可以用alias。 # 那么root和alias的區(qū)別是什么? # 用root屬性指定的值是要加入到最終路徑中的,匹配條件會拼接到路徑中 # 用alias屬性指定的值不需要加入到最終路徑中 # 請求的條件為test.html,通過root指定的路徑為/usr/share/nginx/test_html,因此在匹配的時候,這個路徑下就必須要有test.html這個文件才可以,否則就會找不到而報錯,如果用alias,那么通過瀏覽器進行請求的時候,alias也是指定到/usr/share/nginx/test_htm路徑下,但是會匹配默認的index.html,而無須強制匹配test.html。 # 不能使用”=”來進行精確匹配 2.前綴匹配 server { listen 80; listen [::]:80; server_name welcome.com; access_log /etc/nginx/vhost/logs/access_welcome.log; error_log /etc/nginx/vhost/logs/error_welcome.log; location ^~ /a/ { root /data/webroot/; } location ^~ /a/www/ { root /data/webroot/; } } # 當請求為 http://welcome.com/a/ 時,訪問 /data/webroot/a/ 下的index.html文件 # 當請求為 http://welcome.com/a/www/ 時,訪問 /data/webroot/a/www/ 下的index.html文件 # 如果^~匹配成功了,那么表示阻斷正則表達式,不再進行正則匹配 3.正則匹配 # 正則表達式匹配,大小寫敏感 server { listen 80; listen [::]:80; server_name welcome.com; access_log /etc/nginx/vhost/logs/access_welcome.log; error_log /etc/nginx/vhost/logs/error_welcome.log; location ~ .(gif|jpg|png)$ { root /var/www/images; } error_page 404 /404.html; location = /404.html { root /usr/share/nginx/test_html; } error_page 500 502 503 504 /50x.html; location = /50x.html { } } # 正則表達式匹配,大小寫不敏感 server { listen 80; listen [::]:80; server_name welcome.com; access_log /etc/nginx/vhost/logs/access_welcome.log; error_log /etc/nginx/vhost/logs/error_welcome.log; location ~* .(css|js)$ { root /var/www/assets; } error_page 404 /404.html; location = /404.html { root /usr/share/nginx/test_html; } error_page 500 502 503 504 /50x.html; location = /50x.html { } } 4.默認匹配 第一種情況:proxy_pass最后面沒有斜杠,匹配路徑有斜杠(/bbb/) server { listen 80; listen [::]:80; server_name welcome.com; access_log /etc/nginx/vhost/logs/access_welcome.log; error_log /etc/nginx/vhost/logs/error_welcome.log; #默認匹配 location /bbb/ { proxy_pass http://127.0.0.1:9190; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } # proxy_pass最后面沒有斜杠“/”,此時通過瀏覽器請求http://10.9.2.248/bbb/ # 那么實際訪問的地址就是 http://127.0.0.1:9190/bbb/,會將匹配路徑/bbb一起加過去。 第二種情況:proxy_pass最后面有斜杠 “/”,匹配路徑也有斜杠(/bbb/) server { listen 80; listen [::]:80; server_name welcome.com; access_log /etc/nginx/vhost/logs/access_welcome.log; error_log /etc/nginx/vhost/logs/error_welcome.log; #默認匹配 location /bbb/ { proxy_pass http://127.0.0.1:9190/; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } # proxy_pass最后面有斜杠”/”,此時通過瀏覽器請求http://10.9.2.248/bbb/ # 那么實際訪問的地址就是 http://127.0.0.1:9190,會將/bbb拋棄的, 第三種情況:proxy_pass后面還有其他路徑但是最后沒有 “/”, 匹配路徑也有斜杠(/bbb/) server { listen 80; listen [::]:80; server_name welcome.com; access_log /etc/nginx/vhost/logs/access_welcome.log; error_log /etc/nginx/vhost/logs/error_welcome.log; #默認匹配 location /bbb/ { proxy_pass http://127.0.0.1:9190/ccc; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } # 通過瀏覽器訪問http://10.9.2.248/bbb/index.html # 實際請求的是http://127.0.0.1/index.html # 位置是默認路徑下,不是ccc路徑下 # 如果proxy_pass的路徑為/ccc/ddd,那么實際請求的就是ccc路徑下的cccindex.html 第四種情況:proxy_pass后面還有其他路徑但是最后有 “/”, 匹配路徑也有斜杠(/bbb/) server { listen 80; listen [::]:80; server_name welcome.com; access_log /etc/nginx/vhost/logs/access_welcome.log; error_log /etc/nginx/vhost/logs/error_welcome.log; #默認匹配 location /bbb/ { proxy_pass http://127.0.0.1:9190/ccc/; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } # 通過瀏覽器訪問:http://10.9.2.248/bbb/index.html, # 實際訪問的是http://127.0.0.1/ccc/index.html 第五種情況:location匹配路徑末尾沒有 “/”,proxy_pass后面也沒有“/” server { listen 80; listen [::]:80; server_name welcome.com; access_log /etc/nginx/vhost/logs/access_welcome.log; error_log /etc/nginx/vhost/logs/error_welcome.log; #默認匹配 location /bbb { proxy_pass http://127.0.0.1:9190; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } # 匹配路徑和proxy_pass后都沒有”/” # 訪問http://10.9.2.248/bbb # 默認將請求到http://127.0.0.1:9190/bbb/index.html的內(nèi)容? 轉(zhuǎn)自https://www.cnblogs.com/hahaha111122222/p/18835262 該文章在 2025/7/1 16:22:31 編輯過 |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |