DEV Community

Cover image for How To Set HTTP-Request Header In Haproxy
πŸš€ Vu Dao πŸš€
πŸš€ Vu Dao πŸš€

Posted on • Updated on

How To Set HTTP-Request Header In Haproxy

  • 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


- Create SimpleHTTPServer to listen on port 8000 and dump header for any requests come
Enter fullscreen mode Exit fullscreen mode

!/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)
Enter fullscreen mode Exit fullscreen mode

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

httpd.serve_forever()


- Test by running `curl localhost` and the result
Enter fullscreen mode Exit fullscreen mode

~:/# 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


It shows `myheader: 123` as expected

**2. Apply to ALB**
Enter fullscreen mode Exit fullscreen mode

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/0hh3xde1gz5jxaazpgs5.png)

**More about haproxy**
- [HAProxy With Resolvers In Case Of AWS Application LoadBalancer
](https://dev.to/vumdao/haproxy-with-resolvers-in-case-of-aws-application-loadbalancer-d1n)
- [How To Block IP Addresses In HAProxy](https://dev.to/vumdao/how-to-block-ip-addresses-in-haproxy-3f84)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)