Our Web application utilizes ajax calls to a backend that operates on a different domain, requiring CORS. The backend setup includes an HAproxy 1.4.22 along with multiple Wildflys running on the OpenShift PaaS. During times when a Wildfly instance is unavailable (e.g., during maintenance), HAproxy responds with a 503 error to every request or serves the configured errorfile.
However, this poses a challenge for the Web application to accurately display "Maintenance Mode" based on a rejected backend request (503 error). The browser initially sends an OPTIONS request (preflight) and receives a 503 response, causing the browser to not relay this status code to the ajax call in JavaScript. This results in always receiving a status code of 0 as a response, as the browser interprets it as a fatal preflight failure and restricts access.
To address this issue, I propose implementing two distinct errorfiles in HAproxy - one to handle OPTIONS requests with a content of "HTTP/1.1 200 OK.... Access-Control-Allow-Origin: *...." to pass the preflight check in the browser, and another errorfile for processing POST requests with a content of "HTTP/1.1 503 ....." to ensure the browser accurately reflects the 503 status in the ajax response. However, I have encountered difficulties in implementing this solution.
global
maxconn 256
defaults
mode http
log global
option httplog
...
listen express 127.4.184.2:8080
acl is_options method OPTIONS
acl is_post method POST
errorfile 503 /var/lib/openshift/564468c90c1e66c7f2000077/app-root/runtime/repo/503.http if is_post
errorfile 503 /var/lib/openshift/564468c90c1e66c7f2000077/app-root/runtime/repo/options.http if is_options
option httpchk GET /
http-check expect rstatus 2..|3..|401
balance leastconn
server local-gear 127.4.184.1:8080 check fall 2 rise 3 inter 2000 cookie local-564468c90c1e66c7f2000077
It is important to note that this approach is limited by the constraints of the errorfile directive not supporting the if <condition>
syntax.
How can we achieve the desired behavior? If anyone has alternate solutions to address the "Maintenance Mode" and CORS challenge, we are open to suggestions and ideas.
Thank you in advance!