14

通过我的 NGinx 设置,我能够拦截来自 ajax 预检的 OPTIONS 请求,并使用正确的 CORS 标头和 200 响应进行响应,以便请求可以继续进行。我正在尝试将我的前端代理整合到 HAProxy 中,并且在让这块拼图正常工作时遇到了一些问题。

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

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 }

中给出的解决方案:

当您从客户端的请求中设置所有正确的标头时,如何使用 HAProxy 发送响应而不将请求传递给 Web 服务器可以工作,但不是动态的,这不是理想的解决方案。

任何帮助,将不胜感激!

4

4 回答 4

7

根据anine.io 的出色回答,我提出了以下解决方案,它允许定义允许的来源列表,并且还Acccess-Control-Allow-Origin为所有 HTTP 请求添加了缺失的标头。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 预检,如果没有充分的理由,请不要限制方法或标头,因为您必须在haproxy.conf. 注意:目前浏览器不支持*标题的Access-Control-Allow-Methods通配符。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 标头,它将根据这些正则表达式进行验证,并且只有当它们匹配时,cors.lua才会返回 CORS Preflight from(对于HTTP OPTIONS请求),或者将Access-Control-Allow-Origin带有客户端请求的源头标头的值添加到响应。的内容示例cors-origins.lst可能是

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

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

有关 CORS 的更多详细信息,请参阅HTTP 访问控制 (CORS)

于 2017-09-06T23:02:52.950 回答
5

你可以使用 Lua,但你需要确保 HAproxy 是用 构建的USE_LUA,通过检查haproxy -vv.

这是一个示例配置,我自己没有尝试过,但它会让您了解您可以做什么:

# haproxy.cfg

global
    lua-load cors.lua

frontend foo
    ...
    http-request use-service lua.cors-response if METH_OPTIONS { req.hdr(origin) -m found } { ... }

# 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", "Origin, X-Requested-With, Content-Type, Origin, User-Agent, If-Modified-Since, Cache-Control, Accept")
    applet:add_header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
    applet:add_header("Access-Control-Max-Age", "1728000")
    applet:start_response()
end)
于 2016-07-04T21:55:25.450 回答
3

我想在这里提出我自己的答案。我们经历了相当多的痛苦才能达到工作设置。此处对这个问题的其他答案非常有帮助,但并没有为我们提供完整的工作配置。

我们希望允许任何来源。如果您需要列入白名单的来源,请参阅@Florian Feldhaus 的答案,了解有用的正则表达式技巧。我们不使用白名单,而是回显位置标头:

http-request set-header Access-Control-Allow-Origin %[capture.req.hdr(0)] if { capture.req.hdr(0) -m found }

我们还需要显式设置Access-Control-Allow-Headersand Access-Control-Expose-Headers浏览器对这些标头中的通配符的支持还不完全存在。

所以这就是我们的配置所做的:

  1. 使用这个 cors.lua 脚本处理预检请求
  2. http-response set-header使用添加 Access-Control-Allow-* 标头处理正常请求
  3. 调整tune.maxrewrite以适合我们的 CORS 标头(> 1 KB)

1) 和 2) 的步骤在此处的其他答案中进行了解释,但步骤 3) 花了我们很长时间才弄清楚。我在这篇博文中记录了完整的配置和带我们到达那里的旅程。该帖子包含指向 github 上的要点的链接。

于 2017-09-22T18:01:33.987 回答
0

为了像我一样拦截 OPTIONS,我想在Access-Control-Max-Age响应中添加标头。我修改我的haproxy.cfg并将这一行添加到backend块中。

backend api
  balance roundrobin
  http-response set-header Access-Control-Max-Age 600 if METH_OPTIONS

HAProxy 版本:2.2.6

参考文档:https ://cbonte.github.io/haproxy-dconv/2.2/configuration.html#http-response%20set-header

于 2021-07-09T05:31:43.670 回答