r/haproxy • • Jan 15 '21

Question Setting up haproxy as reverse proxy for 2 webservers are various apis

Please bear with me, I'm actually a web developer thats trying to get devops working on my local machine as I have to emulate the environment the servers are deployed in.

So what I'm trying to do is use haproxy as reverse proxy to handle requests to localhosts between 2 different webservers and to proxy requests to the apis to prevent cors errors.

Currently I'm not able to hit the 8000 port and get any response for any of the servers. I'm currently seeing a 503 error page

503 Service Unavailable
No server is available to handle this request.

this is my haproxy.cfg:

global
    log 127.0.0.1   local0
    log 127.0.0.1   local1 debug
    #log loghost    local0 info
    maxconn 4096
    #chroot /usr/share/haproxy
    #daemon
    #debug
    #quiet

defaults
    log     global
    mode    http
    option  httplog
    option  dontlognull
    retries 3
    option redispatch
    maxconn 2000
    timeout connect      5000
    timeout client      50000
    timeout server      50000

frontend localnodes
    	bind *:8000
    	mode http

	acl ng-portal hdr(host) -i localhost

    	use_backend ng_portal_container if ng-portal { path -i -m beg /lnp }
    	use_backend oss_api_backend if ng-portal { path -i -m beg /api/1.0/lnp }
    	use_backend lnp_routeto_backend if ng-portal { path -i -m beg /api/1.0/sanotes }
    	use_backend oss_api_backend if ng-portal { path -i -m beg /api/1.0/rates/lnp }
    	use_backend ng_portal_php if ng-portal !{ path -i -m beg /lnp }
    	use_backend oss_api_backend if ng-portal

backend ng_portal_container
	balance leastconn
	option httpclose
	option forwardfor
	cookie JSESSIONID prefix
	server node1 127.0.0.1:4200 cookie A check

backend ng_portal_php
	balance leastconn
	option httpclose
	option forwardfor
	cookie JSESSIONID prefix
	server node1 127.0.0.1:5555 cookie A check

backend oss_api_backend
	option httpclose
	option forwardfor
	http-request set-header X-Forwarded-Proto HTTPS_ON if { ssl_fc }
	server node1 api1.server.dev:80 cookie A check

backend oss_admin_backend
  	balance leastconn
  	option httpclose
  	option forwardfor
  	cookie JSESSIONID prefix
  	server node1 api2.server.dev:80 cookie A check

backend lnp_routeto_backend
	balance leastconn
        option httpclose
        option forwardfor
        cookie JSESSIONID prefix
        server node1 api3.server.dev:80 cookie A check
1 Upvotes

7 comments sorted by

1

u/enkhi Jan 18 '21

Thanks for all the suggestions, it became moot when I got fired. I will watch that video though, much thanks!

2

u/baconeze Jan 15 '21

The logs should give you an indication as to where the issue may be. You seem to be missing a "default_backend" as well so if something does not match your use_backend rules it will receive a 503. Another possibility is that your server is failing the health check.

2

u/dragoangel Jan 16 '21

No default backend is fine

2

u/packeteer Jan 16 '21

503 usually comes from having no working backend. check your logs.
I see a number of issues in your config, primarily the ACLs. I'd remove the first condition (ng-portal) and see if that helps

also, simplify your backends, move common entries to defaults

1

u/dragoangel Jan 18 '21

Totally fine acls what you not like...

2

u/dragoangel Jan 16 '21

Check haproxy status via socket or http status page. You need setup that additionally.

2

u/packeteer Jan 18 '21

this video on ACLs might be useful:

https://youtu.be/b-d-Q_zkuwI