
我有多個在不同連接埠上運行的服務,每個服務都使用相同的 URI 路徑。例如:
New York Housing Service
127.0.0.1:8080/homes
127.0.0.1:8080/prices
Las Vegas Housing Service
127.0.0.1:8081/homes
127.0.0.1:8081/prices
到目前為止一切都很好,但我現在需要設定 haproxy 來平衡服務的負載。因此,我顯然需要能夠區分它們以進行內容切換。我想我要做的就是在 ACL 中的路徑上新增一個參數來區分兩個後端,在本例中,在 ACL 中加入一個 url 參數,後面跟著應用程式的實際路徑參數:
frontend http
maxconn 2000
bind 0.0.0.0:5000
acl new-york path_reg -i /newyork.*
use_backend nyc-server if new-york
acl las-vegas path_reg -i /lasvegas.*
use_backend lv-server if las-vegas
backend nyc-server
server www.test.com 127.0.0.1:8080 maxconn 100
backend lv-server
server www.test.com 127.0.0.1:8081 maxconn 100
在此設定中,前往 127.0.0.1:5000/newyork/home 將帶我到 127.0.0.1:8080/home,而 127.0.0.1:5000/lasvegas/home 將我帶到 127.0.0.1:8081/家。到目前為止我的嘗試只是回傳了 404 錯誤。我一直在瀏覽文檔,但沒有看到任何與我的用例完全匹配的內容,因此任何幫助將不勝感激。
編輯:我忘了提及我正在使用 haproxy 1.5.18
答案1
後端服務正在回答 HTTP 404 錯誤,因為您的 HAPROXY 設定目前會原樣轉送 URL 路徑。例如,HTTP 請求http://127.0.0.1:5000/newyork/home/wtc.png
將作為 轉送到nyc-server
後端http://127.0.0.1:8000/newyork/home/wtc.png
。
$ wget -4O /dev/null http://localhost:5000/newyork/homes/wtc.png
--2020-02-01 13:00:09-- http://localhost:5000/newyork/homes/wtc.png
Resolving localhost (localhost)... 127.0.0.1, 127.0.0.1
Connecting to localhost (localhost)|127.0.0.1|:5000... connected.
HTTP request sent, awaiting response... 404 File not found
2020-02-01 13:00:09 ERROR 404: File not found.
$ python -m http.server 8080
Serving HTTP on 0.0.0.0 port 8080 (http://0.0.0.0:8080/) ...
127.0.0.1 - - [01/Feb/2020 13:00:09] code 404, message File not found
127.0.0.1 - - [01/Feb/2020 13:00:09] "GET /newyork/homes/wtc.png HTTP/1.1" 404 -
HAproxy 應配置為透過從後端取得資源時剝離第一個路徑元件來轉換 URL 路徑。我建議您reqrep
在前端的定義中添加一個指令,該指令旨在操作 HTTP 請求標頭的第一行並將類似內容轉換GET /newyork/homes/wtc.png HTTP/1.1
為GET /homes/wtc.png HTTP/1.1
.
frontend http
reqrep ^([A-Z]+)\ /+[^/]+(/+.*)?$ \1\ \2
但是,它不起作用,因為 HAproxy在指令reqrep
之前評估指令use-backend
,從而破壞了後端評估:
$ wget -4O /dev/null http://localhost:5000/newyork/homes/wtc.png
--2020-02-01 13:54:55-- http://localhost:5000/newyork/homes/wtc.png
Resolving localhost (localhost)... 127.0.0.1, 127.0.0.1
Connecting to localhost (localhost)|127.0.0.1|:5000... connected.
HTTP request sent, awaiting response... 503 Service Unavailable
2020-02-01 13:54:55 ERROR 503: Service Unavailable.
第二種工作方法是在後端定義中重寫 URL 路徑。然而,根據服務中後端的數量,編寫reqrep
指令和配置每個後端來執行 URL 重寫很容易出錯。
backend nyc-server
server www.test.com 127.0.0.1:8080 maxconn 100
reqrep ^([A-Z]+)\ /+[^/]+(/+.*)?$ \1\ \2
backend lv-server
server www.test.com 127.0.0.1:8081 maxconn 100
reqrep ^([A-Z]+)\ /+[^/]+(/+.*)?$ \1\ \2
$ wget -4O /dev/null http://localhost:5000/newyork/homes/wtc.png
--2020-02-01 14:02:29-- http://localhost:5000/newyork/homes/wtc.png
Resolving localhost (localhost)... 127.0.0.1, 127.0.0.1
Connecting to localhost (localhost)|127.0.0.1|:5000... connected.
HTTP request sent, awaiting response... 200 OK
Length: 456892 (446K) [image/png]
Saving to: ‘/dev/null’
/dev/null 100%[===============>] 446.18K --.-KB/s in 0s
2020-02-01 14:02:29 (1.83 GB/s) - ‘/dev/null’ saved [456892/456892]
$ python -m http.server 8080
Serving HTTP on 0.0.0.0 port 8080 (http://0.0.0.0:8080/) ...
127.0.0.1 - - [01/Feb/2020 14:02:29] "GET /homes/wtc.png HTTP/1.1" 200 -