Ray Lee | 李宗叡
Learn or Die
Published in
Apr 16, 2024

# 前言

紀錄 Nginx location rule

# 語法

Syntax:	location [ = | ~ | ~* | ^~ ] uri { ... }
location @name { ... }
Default: —
Context: server, location

# 簡易流程圖

# 範例

location = / {
[ configuration A ]
}

location / {
[ configuration B ]
}

location /documents/ {
[ configuration C ]
}

location ^~ /images/ {
[ configuration D ]
}

location ~* \.(gif|jpg|jpeg)$ {
[ configuration E ]
}
  • 請求 / 將會符合 A
  • 請求 /index.html 將會符合 B
  • 請求 /documents/document.html 將會符合 C
  • 請求 /images/1.gif 將會符合 D
  • 請求 /documents/1.jpg 將會符合 E
  • 請求 @name: 只用於內部重新導向,不用於正常使用, 例如:
location /img/ {
not_found 404 @not_found;
}

location @not_found {
# 规则
}

# 參考資源

參考官方文件

--

--

Ray Lee | 李宗叡
Learn or Die

It's Ray. I do both backend and frontend, but more focus on backend. I like coding, and would like to see the whole picture of a product.