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 }

で与えられた解決策:

リクエストを Web サーバーに渡さずに HAProxy でレスポンスを送信する方法は、クライアントのリクエストからすべての正しいヘッダーを設定すると機能しますが、動的ではないため、理想的なソリューションではありません。

どんな助けでも大歓迎です!

4

4 に答える 4

5

USE_LUALua を使用できますが、 をチェックして HAproxy が でビルドされていることを確認する必要があります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 に答える