HAProxy - URL은 동일하지만 포트가 다른 서비스를 구별하기 위해 ACL에 URL 매개변수를 추가해야 합니다.

HAProxy - URL은 동일하지만 포트가 다른 서비스를 구별하기 위해 ACL에 URL 매개변수를 추가해야 합니다.

서로 다른 포트에서 실행되는 여러 서비스가 있으며 각 서비스는 동일한 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

HAPROXY 구성이 현재 URL 경로를 그대로 전달하고 있기 때문에 백엔드 서비스가 HTTP 404 오류로 응답하고 있습니다. 예를 들어 에 대한 HTTP 요청이 백엔드 http://127.0.0.1:5000/newyork/home/wtc.png로 전달되고 있습니다 .nyc-serverhttp://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 -

백엔드에서 리소스를 가져오는 동안 첫 번째 경로 구성 요소를 제거하여 URL 경로를 변환하도록 HAproxy를 구성해야 합니다. reqrep HTTP 요청 헤더의 첫 번째 줄을 조작하고 다음과 같은 내용 GET /newyork/homes/wtc.png HTTP/1.1GET /homes/wtc.png HTTP/1.1.

frontend http
  reqrep ^([A-Z]+)\ /+[^/]+(/+.*)?$  \1\ \2

reqrep그러나 HAproxy는 지시문을 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 -

관련 정보