1
votes

in a development environment I need to access my client and all of my backend services using localhost:80. Therefore I want to use haproxy to map the requests to the right services.

I created the following Dockerfile to start an haproxy:

FROM haproxy:1-alpine
COPY haproxy.cfg /usr/local/etc/haproxy/haproxy.cfg

My haproxy.cfg file looks like this:

defaults
  timeout client        30s
  timeout server        30s
  timeout connect       30s

frontend MyFrontend
  bind  *:80
  acl url_api path_reg ^/api-.*
  use_backend api-backend if url_api
  default_backend web-backend

backend api-backend
    mode            http
    server      backend host.docker.internal:8080

backend web-backend
    mode            http
    server      client host.docker.internal:4200

I start the docker machine using the following command:

docker build -t haproxy-local . && docker run --rm -p80:80 haproxy-local

My angular client is started on the host machine on port 4200. My backend service on port 8080 is running to. If I access http://localhost:80 my web client is opened in the web browser. Unfortunately if I try to access the backend using http://localhost/api-my-backend-service/123 it does not work. If I change it to

default_backend api-backend

I can access the backend via http://localhost/api-my-backend-service/12 but not the client.

So the access to both backends does seem to work because if I change the default backend I can access both client and api backend. But the use_backend does not seem to match and I am unable to figure out why.

Any ideas?

Thanks Meinert

2

2 Answers

0
votes

As a workaround I switched to nginx which can be used as an reverse proxy and which is enough for my needs.

But I would still be interested in the cause of my problems above.

0
votes

I have a similar problem statement, and this is how i resolved it:

Ex:

Angular routes supported in my app :
'login', 'h/dashboard' in which case my url would be 
localhost:4200/#/login, localhost:4200/#/h/dashboard

These routes will make subsequent http calls to my springboot in their respective angular components.

localhost:4200/#/login -> localhost:8082/login
localhost:4200/#/h/dashboard -> localhost:8082/api/dashboard

with @Restcontroller endpoints on 8082 as '/login', '/api/**'

frontend myApp
  bind 127.0.0.1:9090
  mode http
  acl acl_proxyUi path_reg ^login$
  acl acl_proxyUi2 path_reg ^h/..*$  
  acl acl_proxyWeb path_reg ^/api/..*$
  acl acl_proxyWeb2 path_reg ^/login$

  use_backend app-web if acl_proxyWeb or acl_proxyWeb2
  use_backend app-ui if acl_proxyUi or acl_proxyUi2 
#  default_backend app-web
  default_backend app-ui

backend app-web
  mode http
  balance roundrobin
  server webserver 127.0.0.1:8082 


backend app-ui
  mode http
  balance roundrobin
  server nodeserver 127.0.0.1:4200 

Please note that in angular app - > before using the haproxy , i was using the url endpoint for springboot calls as localhost:8082/login. After including the haproxy, that has been changed to localhost:9090/login and the proxy will take care of routing to springboot app.