HAProxy 2.5.5: Handle CORS dan Preflight Requests

Masalah

Frontend buat request ke backend yang lain domain atau port, browser akan hantar preflight request dulu (HTTP OPTIONS). Kalau HAProxy tak handle request tu, browser akan reject dan API call tak jalan.

HAProxy sendiri tak ada built-in CORS handler. Kita kena tulis logic tu dalam config.

Config HAProxy untuk CORS

Aku guna approach dari gist TimWolla. Letak logic ni dalam frontend atau backend block.

frontend www-https
    bind *:443 ssl crt /etc/haproxy/certs/
    option httplog

    # Capture Origin header
    http-request set-var(txn.cors_origin) req.hdr(Origin)

    # Log bila ada Origin header
    http-request capture var(txn.cors_origin) len 100 if { var(txn.cors_origin) -m found }

    # Check allowed origins
    acl cors_allowed_origin var(txn.cors_origin) -m str http://127.0.0.1:8080

    # Handle preflight
    acl is_options method OPTIONS

    http-request return status 204 \
        hdr Access-Control-Allow-Origin %[var(txn.cors_origin)] \
        hdr Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS" \
        hdr Access-Control-Allow-Headers "Authorization, Content-Type" \
        hdr Access-Control-Max-Age 86400 \
        if is_options cors_allowed_origin

    # Inject CORS headers untuk request biasa
    http-response set-header Access-Control-Allow-Origin %[var(txn.cors_origin)] \
        if cors_allowed_origin

HAProxy akan return HTTP 204 terus untuk OPTIONS request, tanpa forward ke backend. Backend tak perlu tahu pasal CORS langsung.

Setup Logging

Untuk tengok apa yang HAProxy buat, kena enable logging dulu. Kalau guna CentOS/RHEL, setup rsyslog.

Edit /etc/rsyslog.conf, uncomment atau tambah baris ni:

module(load="imudp")
input(type="imudp" port="514")

Lepas tu buat file baru /etc/rsyslog.d/haproxy.conf:

local2.*    /var/log/haproxy.log
& stop

Dalam haproxy.cfg, bahagian global:

global
    log 127.0.0.1:514 local2

Dalam frontend block, tambah:

    option httplog

Restart kedua-dua service:

systemctl restart rsyslog
systemctl restart haproxy

Output Log

Bila preflight request masuk, log akan nampak macam ni:

Apr 23 00:29:29 haproxy[29060]: CORS: Got 'Origin' header: http://127.0.0.1:8080
Apr 23 00:29:29 haproxy[29060]: CORS: preflight request received
Apr 23 00:29:29 haproxy[29060]: CORS: http://127.0.0.1:8080 allowed
Apr 23 00:29:29 haproxy[29060]: CORS: Returning reply to preflight request
Apr 23 00:29:29 haproxy[29060]: 192.168.1.173:56733 [23/Apr/2022:00:29:29.734] www-https~ www-https/<NOSRV> 0/-1/-1/-1/0 204 189 - - LR-- 3/3/0/0/0 0/0 "OPTIONS /api/v4/clients HTTP/1.1"

<NOSRV> tu confirm HAProxy respond sendiri, tak forward ke mana-mana backend. Status 204, preflight berjaya.

Request GET yang lepas tu pula forward ke backend sebenar:

Apr 23 00:29:29 haproxy[29060]: 192.168.1.173:56734 [...] www-https~ dashboard_backend/dashboard 0/0/1/8/9 500 150 [...] "GET /api/v4/clients HTTP/1.1"

Status 500 tu dah problem backend, bukan CORS. Maksudnya CORS dah settle, tinggal debug API.

Rujukan

Leave a Comment