HAProxy URL 重寫給出 400 Bad Request 和 NOSRV

HAProxy URL 重寫給出 400 Bad Request 和 NOSRV

我正在嘗試使用 reqrep 重寫 uri 的一部分。

haproxy.cfg(原始)

global
  log 127.0.0.1 local2
  daemon
  maxconn 256
defaults
  mode http
  log global
  option httplog
  timeout connect 5000ms
  timeout client 50000ms
  timeout server 50000ms

frontend haproxy_in
  bind *:8080

  default_backend myapp


backend myapp

  server app1 localhost:8123 check

當我這樣做時,我看到 haproxy 記錄了我所期望的內容。請注意,我使用的是無法處理 POST 的虛擬 python 伺服器,因此 501 並不重要,只是請求已路由這一事實正是我正在尋找的。

haproxy_in myapp/app1 0/0/0/1/5 501 379 - - ---- 0/0/0/0/0 0/0 "POST /please/rewrite/this/ HTTP/1.1

我還看到我的伺服器記錄以下內容。

 "POST /please/rewrite/this/ HTTP/1.1" 501 -

haproxy.cdf(帶有 reqrep)

global
  log 127.0.0.1 local2
  daemon
  maxconn 256
defaults
  mode http
  log global
  option httplog
  timeout connect 5000ms
  timeout client 50000ms
  timeout server 50000ms

frontend haproxy_in
  bind *:8080

  default_backend myapp


backend myapp

  reqrep ^([^\ :]*\ /) "POST\ /test\ HTTP/1.1"

  server app1 localhost:8123 check

新增 reqrep 後,我從 haproxy 收到錯誤訊息

haproxy_in myapp/<NOSRV> -1/-1/-1/-1/1 400 187 - - PR-- 0/0/0/0/3 0/0 "POST /please/rewrite/this/ HTTP/1.1"

看來我的請求無效?看來該請求並不是從 haproxy 發出的。我怎麼才能看到請求被重寫成什麼?我已經使用不同的正規表示式嘗試了幾個不同的 reqrep 命令,並且所有命令似乎都給了我相同的錯誤。

我的haproxy版本是1.5.16。

答案1

請求代表採用三個參數<search> <string> <cond>

<search>: 是 URL,例如 www.somedomain.com/please/rewrite/this/

<string>: 是替換字串,例如 /test

<cond>: 如有特殊情況,暫時忽略。

在範例中,替換將執行以下操作:

FROM: http://www.somedomain.com/please/rewrite/this/

TO: /test

以下將進行替換:

reqrep http://www.somedomain.com/please/rewrite/this/ http://www.somedomain.com/test

如果您想要取代忽略域,您需要使用正規表示式進行設定:

reqrep ^([^\ :]*)\/please/rewrite/this/ \1\/test

相關內容