Skip to content

Instantly share code, notes, and snippets.

@amslezak
Last active January 8, 2022 16:55
Show Gist options
  • Save amslezak/6573767376860ed59a74878fbff6e61f to your computer and use it in GitHub Desktop.
Save amslezak/6573767376860ed59a74878fbff6e61f to your computer and use it in GitHub Desktop.
Example HAProxy 1.6+ config for serving static content from AWS S3 bucket
# This config will serve up: http://<haproxy>/foo/index.html
#
# This post was indispensable
# https://stackoverflow.com/questions/39964607/haproxy-forward-request-to-s3-hosted-site
global
#debug # Uncomment this line to debug and then comment out daemon
daemon
maxconn 256
defaults
mode http
log global
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
frontend http-in
bind *:80
# URL ACL
acl s3 path_beg /foo
# BACKEND SERVER RULES
use_backend S3 if s3
backend S3
# must be in "Virtual Hosted–Style Method" format as per S3 docs - https://docs.aws.amazon.com/AmazonS3/latest/dev/VirtualHosting.html
http-request set-header Host <s3-bucketname>.s3.<s3-location>.amazonaws.com
# required for stripping and rewriting pathname
http-request set-path %[path,regsub(^/foo,)]
# cannot be s3.amazonaws.com
server CDN s3.<s3-location>.amazonaws.com:80
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment