nginx中有關(guān) location 模塊的配置示例說(shuō)明
當(dāng)前位置:點(diǎn)晴教程→知識(shí)管理交流
→『 技術(shù)文檔交流 』
一、匹配規(guī)則 1.基礎(chǔ)匹配 location / { ... }:這是最基本的匹配,匹配所有請(qǐng)求。因?yàn)樗鼪](méi)有指定具體的文件或目錄,所以通常作為后備選項(xiàng)出現(xiàn)。 2.精確匹配 location = /exact/path { ... }:精確匹配這個(gè)路徑。如果請(qǐng)求的 URI 完全等于/exact/path,則使用這個(gè)location塊的配置處理此請(qǐng)求。這具有最高的優(yōu)先級(jí)。 3.前綴匹配 location /prefix/ { ... }:前綴匹配請(qǐng)求的 URI 的開(kāi)始部分。如果請(qǐng)求的 URI 以/prefix/開(kāi)始,這個(gè)location塊將被用來(lái)處理請(qǐng)求。 location ^~ /prefix/ { ... }:前綴匹配,但它會(huì)停止正則表達(dá)式匹配,即使正則表達(dá)式可能會(huì)更具體匹配。如果該location匹配,Nginx 不會(huì)考慮之后的正則location塊。 4.正則表達(dá)式匹配 location ~ /regex/ { ... }:大小寫敏感的正則匹配。 location ~* /regex/ { ... }:大小寫不敏感的正則匹配。 location / { ... }正則表達(dá)式匹配會(huì)在普通字符串前綴匹配后進(jìn)行。如果有多個(gè)正則表達(dá)式location都匹配請(qǐng)求,則使用第一個(gè)匹配的location塊。 二、優(yōu)先級(jí)順序 Nginx 處理請(qǐng)求時(shí) location 匹配的優(yōu)先級(jí)順序如下: 首先進(jìn)行精確匹配location = 其次按文件中出現(xiàn)順序匹配所有正則表達(dá)式location ~和location ~* 然后進(jìn)行最長(zhǎng)的前綴匹配location ^~ 最后是通常的前綴匹配location /prefix/ 如果前面的匹配都沒(méi)有找到,就使用默認(rèn)的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 { #通過(guò) root 路徑,會(huì)將條件拼接進(jìn)來(lái) 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 { } } # 第一個(gè)和第二個(gè)location匹配條件一樣,都是/test.html # 但第二個(gè)為精準(zhǔn)匹配到靜態(tài)路徑,因此第一個(gè)不會(huì)執(zhí)行,會(huì)執(zhí)行第二個(gè)。 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 { #通過(guò)alias指定路徑,無(wú)需拼接條件 #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屬性指定的值是要加入到最終路徑中的,匹配條件會(huì)拼接到路徑中 # 用alias屬性指定的值不需要加入到最終路徑中 # 請(qǐng)求的條件為test.html,通過(guò)root指定的路徑為/usr/share/nginx/test_html,因此在匹配的時(shí)候,這個(gè)路徑下就必須要有test.html這個(gè)文件才可以,否則就會(huì)找不到而報(bào)錯(cuò),如果用alias,那么通過(guò)瀏覽器進(jìn)行請(qǐng)求的時(shí)候,alias也是指定到/usr/share/nginx/test_htm路徑下,但是會(huì)匹配默認(rèn)的index.html,而無(wú)須強(qiáng)制匹配test.html。 # 不能使用”=”來(lái)進(jìn)行精確匹配 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/; } } # 當(dāng)請(qǐng)求為 http://welcome.com/a/ 時(shí),訪問(wèn) /data/webroot/a/ 下的index.html文件 # 當(dāng)請(qǐng)求為 http://welcome.com/a/www/ 時(shí),訪問(wèn) /data/webroot/a/www/ 下的index.html文件 # 如果^~匹配成功了,那么表示阻斷正則表達(dá)式,不再進(jìn)行正則匹配 3.正則匹配 # 正則表達(dá)式匹配,大小寫敏感 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 { } } # 正則表達(dá)式匹配,大小寫不敏感 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.默認(rèn)匹配 第一種情況:proxy_pass最后面沒(méi)有斜杠,匹配路徑有斜杠(/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; #默認(rèn)匹配 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最后面沒(méi)有斜杠“/”,此時(shí)通過(guò)瀏覽器請(qǐng)求http://10.9.2.248/bbb/ # 那么實(shí)際訪問(wèn)的地址就是 http://127.0.0.1:9190/bbb/,會(huì)將匹配路徑/bbb一起加過(guò)去。 第二種情況: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; #默認(rèn)匹配 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最后面有斜杠”/”,此時(shí)通過(guò)瀏覽器請(qǐng)求http://10.9.2.248/bbb/ # 那么實(shí)際訪問(wèn)的地址就是 http://127.0.0.1:9190,會(huì)將/bbb拋棄的, 第三種情況:proxy_pass后面還有其他路徑但是最后沒(méi)有 “/”, 匹配路徑也有斜杠(/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; #默認(rèn)匹配 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; } } # 通過(guò)瀏覽器訪問(wèn)http://10.9.2.248/bbb/index.html # 實(shí)際請(qǐng)求的是http://127.0.0.1/index.html # 位置是默認(rèn)路徑下,不是ccc路徑下 # 如果proxy_pass的路徑為/ccc/ddd,那么實(shí)際請(qǐng)求的就是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; #默認(rèn)匹配 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; } } # 通過(guò)瀏覽器訪問(wèn):http://10.9.2.248/bbb/index.html, # 實(shí)際訪問(wèn)的是http://127.0.0.1/ccc/index.html 第五種情況:location匹配路徑末尾沒(méi)有 “/”,proxy_pass后面也沒(méi)有“/” 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; #默認(rèn)匹配 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后都沒(méi)有”/” # 訪問(wèn)http://10.9.2.248/bbb # 默認(rèn)將請(qǐng)求到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ò) |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |