I have the following haproxy config that adds the access-control-allow-origin header on successful 200 requests with the below config. My problem is, when I hit timeouts or haproxy itself (not my upstream server) throws an error for some other reason, this header doesn't get added. How do I add this header on e.g. 504 responses as well?
global log /dev/log local0 log /dev/log local1 notice chroot /var/lib/haproxy stats socket /run/haproxy/admin.sock mode 660 level admin stats timeout 30s user haproxy group haproxy daemon ca-base /etc/ssl/certs crt-base /etc/ssl/private ssl-default-bind-ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA> ssl-default-bind-ciphersuites TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256 ssl-default-bind-options ssl-min-ver TLSv1.2 no-tls-tickets defaults log global mode http option httplog option dontlognull timeout connect 5s timeout client 30s timeout server 30s errorfile 400 /etc/haproxy/errors/400.http errorfile 403 /etc/haproxy/errors/403.http errorfile 408 /etc/haproxy/errors/408.http errorfile 500 /etc/haproxy/errors/500.http errorfile 502 /etc/haproxy/errors/502.http errorfile 503 /etc/haproxy/errors/503.http errorfile 504 /etc/haproxy/errors/504.http frontend www-https bind *:80 bind *:443 ssl crt /etc/ssl/cert.io.pem http-response add-header access-control-allow-origin "myCoolWebsite.com" redirect scheme https code 301 if !{ ssl_fc } mode http default_backend myBackend backend myBackend server myServer 123.456.789.101:2345 Edit: I've mostly solved this problem by adding a custom error file for 504 like below. Only problem is I couldn't figure out how to dynamically set the access-control-allow-origin to only the domain currently requesting instead of *. This is a problem, as some browsers have issues/throw on receiving responses with *. Any ideas how I can do this?
// 504.http HTTP/1.0 504 Gateway Time-out Cache-Control: no-cache Connection: close Content-Type: text/html Access-Control-Allow-Origin: * Access-Control-Allow-Methods:\ GET,\ HEAD,\ OPTIONS,\ POST,\ PUT Access-Control-Allow-Credentials:\ true Access-Control-Allow-Headers:\ Origin,\ Accept,\ X-Requested-With,\ Content-Type,\ Access-Control-Request-Method,\ Access-Control-Request-Headers,\ Authorization { "html": { "body": { "h1": "504 Gateway Time-out", "#text": "The server didn't respond in time." } } }