DEV Community

loading...
Cover image for How To Set HTTP-Request Header In Haproxy

How To Set HTTP-Request Header In Haproxy

Vu Dao
Awesome Devops || AWS SA || CloudOpz
Updated on ・1 min read
  • This post describes how haproxy forwards request with header to AWS Application Loadbalancer (ALB).

  • The purpose of adding header to http-request is to create listen rule in ALB

  • Use SimpleHTTPServer to dump header for testing

1. Test dump header

  • haproxy.cfg
frontend test-header
        bind *:80
        default_backend testheader

backend testheader
        http-request set-header myheader 123
        server disabled-server 127.0.0.1:1 disabled
        server localhost localhost:8000
Enter fullscreen mode Exit fullscreen mode
  • Create SimpleHTTPServer to listen on port 8000 and dump header for any requests come
#!/usr/bin/env python3

import http.server as SimpleHTTPServer
import socketserver as SocketServer
import logging

PORT = 8000

class GetHandler(
        SimpleHTTPServer.SimpleHTTPRequestHandler
        ):

    def do_GET(self):
        logging.error(self.headers)
        SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)


Handler = GetHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)

httpd.serve_forever()
Enter fullscreen mode Exit fullscreen mode
  • Test by running curl localhost and the result
~:/# python3 3serv.py                                                                                                                                                                
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36
Accept-Encoding: gzip                               
Connection: close                                   
X-Forwarded-Proto: https                            
myheader: 123                                       
Enter fullscreen mode Exit fullscreen mode

It shows myheader: 123 as expected

2. Apply to ALB

backend appsvc
        http-request set-header myheader 123 
        server disabled-server 127.0.0.1:1 disabled
        server myapp reader-1111111111.us-east-1.elb.amazonaws.com:80
Enter fullscreen mode Exit fullscreen mode

Alt Text

More about haproxy

Discussion (0)

Forem Open with the Forem app