Search code examples
haproxy

Creating an API proxy using HAProxy and getting responses


I have an issue where an API vendor requires a static IP to connect to it and I do not have the ability to configure a static IP for the requests, so I am looking at using HAProxy as my gateway/proxy to the API.

It was very easy to configure HAProxy in a basic form and have it proxy my requests, but I'm finding that some requests return no response while others do.

The API requests will use PUT, POST and GET methods. My config is very similar to default. I'm using HAProxy 1.8.

Two questions; Is HAProxy the right tool for this? Will anything else work? I'm even happy to pay for a commercial tool that does the job.

If it is the right tool, is there any reason why some GET requests return responses and some dont? The response code is still 200 but I have no access to logs on the API vendor side to troubleshoot.

#---------------------------------------------------------------------
# Example configuration for a possible web application.  See the
# full configuration options online.
#
#   http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------

#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
    # to have these messages end up in /var/log/haproxy.log you will
    # need to:
    #
    # 1) configure syslog to accept network log events.  This is done
    #    by adding the '-r' option to the SYSLOGD_OPTIONS in
    #    /etc/sysconfig/syslog
    #
    # 2) configure local2 events to go to the /var/log/haproxy.log
    #   file. A line like the following can be added to
    #   /etc/sysconfig/syslog
    #
    #    local2.*                       /var/log/haproxy.log
    #
    #log         127.0.0.1 local2
    log         127.0.0.1:514  local0
    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon

    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats

#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
    mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000
    log-format "%ci:%cp [%tr] %ft %b/%s %TR/%Tw/%Tc/%Tr/%Ta %ST %B %CC %CS %tsc %ac/%fc/%bc/%sc/%rc %sq/%bq %hr %hs %{+Q}r"

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
#frontend  main *:5000
#    acl url_static       path_beg       -i /static /images /javascript /stylesheets
#    acl url_static       path_end       -i .jpg .gif .png .css .js
#
#    use_backend static          if url_static
#    default_backend             app

frontend api_proxy
    bind *:6109
    mode http
    # capture response header
    default_backend remote_api_server

backend remote_api_server
    #replace 10.10.10.10 with the actual Ip address
    mode http
    http-request set-header Host myhost.com.au
    server server1 myhost.com.au:443 ssl verify none
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
#backend static
#    balance     roundrobin
#    server      static 127.0.0.1:4331 check

#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
#backend app
#    balance     roundrobin
#    server  app1 127.0.0.1:5001 check
#    server  app2 127.0.0.1:5002 check
#    server  app3 127.0.0.1:5003 check
#    server  app4 127.0.0.1:5004 check

Solution

  • So, the use of HAProxy continued to not behave the way I expected so I tried doing what I needed in nginx and it was so easy.

    Ultimately, this block does exactly what I need.

    server {
        listen      6109;
    
        location / {
            proxy_redirect          off;
            proxy_pass_header       Server;
            proxy_set_header        X-Real-IP $remote_addr;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header        X-Scheme $scheme;
            proxy_set_header        Host myhost.com.au;
            proxy_set_header        X-NginX-Proxy true;
            proxy_connect_timeout   5;
            proxy_read_timeout      240;
            proxy_intercept_errors  on;
    
            proxy_pass              https://myhost.com.au:443;
        }
    }
    

    This is on an EC2 instance so I have a load balancer in front of it handling SSL.