The backend services are answering with HTTP 404 errors because your HAPROXY configuration is currently forwarding URL's path as they are. For example, a HTTP request for http://127.0.0.1:5000/newyork/home/wtc.png
is being forwarded to nyc-server
backend as 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 should be configured to translate URLs' paths by stripping the first path component while fetching resources from backends. I would suggest you to add a reqrep
directive into frontend's definition, which is meant to manipulate the first line of the HTTP request header and translate something like GET /newyork/homes/wtc.png HTTP/1.1
into GET /homes/wtc.png HTTP/1.1
.
frontend http
reqrep ^([A-Z]+)\ /+[^/]+(/+.*)?$ \1\ \2
However, it won't work because HAproxy evaluates reqrep
directives before use-backend
ones, thus breaking backend evaluation:
$ 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.
A second working approach is rewriting URL path in backend definitions. However, depending on the number of backends in service, writing reqrep
directives and configuring each backend to perform URL rewrite are prone to error.
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 -