Skip to content

Instantly share code, notes, and snippets.

@haproxytechblog
Last active May 10, 2023 20:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haproxytechblog/6e6b9db50a880ff72c98f070196b380a to your computer and use it in GitHub Desktop.
Save haproxytechblog/6e6b9db50a880ff72c98f070196b380a to your computer and use it in GitHub Desktop.
Introduction to Traffic Shaping Using HAProxy
filter bwlim-in <name> default-limit <size> default-period <time> [min-size <sz>]
filter bwlim-in <name> limit <size> key <pattern> [table <table>] [min-size <sz>]
filter bwlim-out <name> default-limit <size> default-period <time> [min-size <sz>]
filter bwlim-out <name> limit <size> key <pattern> [table <table>] [min-size <sz>]
tcp-request content set-bandwidth-limit <name> ... [ { if | unless } <condition> ]
tcp-response content set-bandwidth-limit <name> ... [ { if | unless } <condition> ]
http-request set-bandwidth-limit <name> ... [ { if | unless } <condition> ]
http-response set-bandwidth-limit <name> ... [ { if | unless } <condition> ]
frontend http
filter bwlim-in upload-per-stream default-limit 100K default-period 1s
http-request set-bandwidth-limit upload-per-stream if { path_beg /admin }
# ...
frontend http
filter bwlim-out download-per-src key src table down-per-src limit 1m
stick-table type ip size 1m expire 30m store gpc0,http_req_rate(10s)
http-request track-sc0 src
http-response set-bandwidth-limit download-per-src if { sc_http_req_rate(0) gt 20 }
# ...
backend down-by-src
# The stickiness table used by <dowload-per-src> filter
stick-table type ip size 1m expire 1h store bytes_out_rate(1s)
filter bwlim-in <name> default-limit <size> default-period <time> [min-size <sz>]
filter bwlim-out <name> default-limit <size> default-period <time> [min-size <sz>]
frontend primeflix
filter bwlim-out video-streaming default-limit 320k default-period 1s # 720p by default
# Detect the resolution by matching on the request path
http-request set-var(txn.resolution) int(360) if { path_beg /360p }
http-request set-var(txn.resolution) int(480) if { path_beg /480p }
http-request set-var(txn.resolution) int(720) if { path_beg /720p }
http-request set-var(txn.resolution) int(1080) if { path_beg /1080p }
http-request set-var(txn.resolution) int(4000) if { path_beg /4k }
acl is_mp4 res.hdr(content-type) -m beg video/mp4
acl is_360p var(txn.resolution) -m int 360
acl is_480p var(txn.resolution) -m int 480
acl is_720p var(txn.resolution) -m int 720
acl is_1080p var(txn.resolution) -m int 1080
acl is_4k var(txn.resolution) -m int 4000
http-response allow if !is_mp4 # Only set a bandwidth limit for mp4 video
http-response set-bandwidth-limit video-streaming # Set the default limit 720p => 320KB/s
http-response set-bandwidth-limit video-streaming limit 90K if is_360p # Override default limit 320p => 90KB/s
http-response set-bandwidth-limit video-streaming limit 140K if is_480p # Override default limit 480p => 140K/s
http-response set-bandwidth-limit video-streaming limit 625K if is_1080p # Override default limit 1080p => 625KB/s
http-response set-bandwidth-limit video-streaming limit 2500K if is_4k # Override default limit 4K => 2.5MB/s
# ...
listen http
bind *:80
mode http
filter bwlim-out my-limit default-period 1s default-limit 100m
http-request set-bandwidth-limit my-limit # Enable the limit for everyone
http-request set-bandwidth-limit my-limit limit 10k period 10s if { path_beg /limit1 } # Override it to 10KB per 10s for requests to /limit1
http-request set-bandwidth-limit my-limit limit 10m period 10s if { path_beg /limit2 } # Override it to 10MB per 10s for requests to /limit2
http-request set-bandwidth-limit my-limit limit 1m period 1s if { path_beg /limit3 } # Override it to 1MB per 1s for requests to /limit3
server www a.b.c.d:80
backend limit-by-src
stick-table type ipv6 size 1m expire 3600s store bytes_out_rate(1s)
filter bwlim-in <name> limit <size> key <pattern> [table <table>] [min-size <sz>]
filter bwlim-out <name> limit <size> key <pattern> [table <table>] [min-size <sz>]
listen http
bind *:80
mode http
filter bwlim-out limit-per-src key src,ipmask(32,64) table limit-by-src limit 1m min-size 2896
http-response set-bandwidth-limit limit-by-src
server www a.b.c.d:80
backend limit-by-src
stick-table type ipv6 size 1m expire 3600s store bytes_out_rate(1s)
frontend tcp
bind *:1234
mode tcp
filter bwlim-in global-up-limit key fe_nname table limit-by-front limit 100m min-size 2896
tcp-request content set-bandwidth-limit global-up-limit
# ...
backend limit-by-front
stick-table type string len 64 size 10 expire 3600s store bytes_in_rate(1s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment