gpt4 book ai didi

ajax - HAProxy CORS OPTIONS header 拦截设置

转载 作者:行者123 更新时间:2023-12-03 00:08:38 27 4
gpt4 key购买 nike

通过我的 NGinx 设置,我能够拦截来自 ajax 预检的 OPTIONS 请求,并使用正确的 CORS header 和 200 响应进行响应,以便请求可以继续向前。我正在尝试将我的前端代理整合到 HAProxy 中,但在解决这一难题时遇到了一些问题。

我的特殊问题是,虽然当服务器能够正确响应 OPTIONS 请求时,我能够添加正确的 CORS 选项,但当预检请求被处理时,一些后端无法处理/响应 405 错误。发布。我的 haproxy.cfg 包含以下用于添加 header 的行:

capture request header origin len 128
http-response add-header Access-Control-Allow-Origin %[capture.req.hdr(0)] if { capture.req.hdr(0) -m found }
rspadd Access-Control-Allow-Credentials:\ true if { capture.req.hdr(0) -m found }
rspadd Access-Control-Allow-Headers:\ Origin,\ X-Requested-With,\ Content-Type,\ Origin,\ User-Agent,\ If-Modified-Since,\ Cache-Control,\ Accept if { capture.req.hdr(0) -m found }
rspadd Access-Control-Allow-Methods:\ GET,\ POST,\ PUT,\ DELETE,\ OPTIONS if { capture.req.hdr(0) -m found }
rspadd Access-Control-Max-Age:\ 1728000 if { capture.req.hdr(0) -m found }

给出的解决方案:

How to send a response with HAProxy without passing the request to web servers当您根据客户端请求设置所有正确的 header 时有效,但不是动态的,这不是理想的解决方案。

如有任何帮助,我们将不胜感激!

最佳答案

基于great answer from anine.io我提出了以下解决方案,它允许定义允许的来源列表,并且还为所有 HTTP 请求添加缺少的 Access-Control-Allow-Origin header 。 anine.io 的回答只显示了 CORS 预检,没有考虑正常请求。

haproxy.cfg中加载全局部分的cors.lua文件(如有必要,调整路径)

global
lua-load /usr/local/etc/haproxy/cors.lua

将 CORS 配置添加到您的前端定义

frontend http-in
# CORS configuration
# capture origin HTTP header
capture request header origin len 128
# add Access-Control-Allow-Origin HTTP header to response if origin matches the list of allowed URLs
http-response add-header Access-Control-Allow-Origin %[capture.req.hdr(0)] if !METH_OPTIONS { capture.req.hdr(0) -m reg -f /usr/local/etc/haproxy/cors-origins.lst }
# if a preflight request is made, use CORS preflight backend
http-request use-service lua.cors-response if METH_OPTIONS { capture.req.hdr(0) -m reg -f /usr/local/etc/haproxy/cors-origins.lst }

创建一个名为 cors.lua 的文件并将其存储在您上面指定的路径下。该文件包含 CORS 预检,如果没有充分的理由,请不要限制方法或 header ,因为您必须在 haproxy.conf 中的 CORS 配置中定义的 ACL 中包含有关方法或 header 的任何限制>。 Note: Currently Browsers do not support wildcard * for the Access-Control-Allow-Methods header. cors.lua 文件应包含以下内容

core.register_service("cors-response", "http", function(applet)
applet:set_status(200)
applet:add_header("Content-Length", "0")
applet:add_header("Access-Control-Allow-Origin", applet.headers["origin"][0])
applet:add_header("Access-Control-Allow-Credentials", "true")
applet:add_header("Access-Control-Allow-Headers", "*")
applet:add_header("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, DELETE, PATCH, OPTIONS")
applet:add_header("Access-Control-Max-Age", "1728000")
applet:start_response()
end)

创建一个名为 cors-origins.lst 的文件,并将其存储在您上面在 CORS 配置中指定的路径下。该文件应包含正则表达式(或只是简单的字符串)。如果客户端发送 Origin header ,则会根据这些正则表达式对其进行验证,并且仅当它们匹配时,才会返回来自 cors.lua 的 CORS 预检(对于 HTTP OPTIONS requests) 或带有客户端请求的原始 header 值的 Access-Control-Allow-Origin 将添加到响应中。 cors-origins.lst 的内容示例可能是

example.com
localhost.*
.*\.mydomain\.com:[8080|8443]

使用 http://test-cors.org/ 测试配置。对于 GET 请求,不应进行 CORS 预检。对于 GET 以外的请求,客户端应首先执行 CORS 预检请求(例如 HTTP OPTIONS 调用),以检查是否允许预期的方法、 header 和授权。

参见HTTP access control (CORS)有关 CORS 的更多详细信息。

关于ajax - HAProxy CORS OPTIONS header 拦截设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32749520/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com