dockernginxhaproxynginx-reverse-proxyportainer

How to convert this from nginx to haproxy?


I'm new to haproxy and still trying to grasp how it works, how would something like this translates to haproxy? I have a dockerized services hence the name of the ip

upstream client_upstream {
        server client:3000;
    }

  
    upstream strapi_upstream {
        server strapi:1337;
    }

    server {
        listen 8080;
        
         location ~ ^/api/(news|sponsors|landing-page) {
            rewrite /api/(.*)   /$1  break;
            proxy_pass         http://strapi_upstream;
            proxy_redirect     off;
            proxy_set_header   Host $host;
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Host $server_name;
        }

        location / {
            proxy_pass         http://client_upstream;
            proxy_redirect     off;
            proxy_set_header   Host $host;
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Host $server_name;
        }

I've managed to do this and the client seems to work with the reverse proxy but the strapi gives an error "Your connection is not private" but with incognito it gives error 503 Service Temporarily Unavailable

frontend frontend
  bind :8080
  default_backend webserver
  
  acl client-url url_reg ^\/.*
  acl strapi-url url_reg ^\/api/.*

  use_backend webserver  if  client-url
  use_backend strapi  if  strapi-url


backend webserver
  server s1 client:3000 check

backend strapi
  server s1 strapi:1337 check

I'm trying to access the servers with a domain using a virtual host env variable assigned to the services

Could it be that an SSL is needed?


Solution

  • The order of the use_backend is imporant.

    Here my suggestion to solve the config

    frontend frontend
      bind :8080
    
      # X-Forwarded-For: is handled by "option forwardfor"
      option forwardfor
    
      acl client-url url_reg ^\/.*
      acl strapi-url url_reg ^\/api/.*
    
      # use add-header or set-header
      # https://docs.haproxy.org/2.6/configuration.html#http-request%20add-header
      # https://docs.haproxy.org/2.6/configuration.html#4.2-http-request%20set-header
      http-request set-header X-Real-IP %ci
      http-request add-header X-Forwarded-Host %[req.hdr(host)]
          
      # change order as order counts
      use_backend strapi    if strapi-url
      use_backend webserver if client-url
    
      default_backend webserver
    
    
    backend webserver
      server s1 client:3000 check
    
    backend strapi
      server s1 strapi:1337 check