服务器正在接收数千条OPTIONS请求由于CORS(跨源资源共享)。现在,每个选项请求都被发送到其中一台服务器,这有点浪费,因为知道 HAProxy 可以添加CORS标头本身无需网络服务器的帮助。
frontend https-in
...
use_backend cors_headers if METH_OPTIONS
...
backend cors_headers
rspadd Access-Control-Allow-Origin:\ https://www.example.com
rspadd Access-Control-Max-Age:\ 31536000
但是,为了使其工作,我需要在中指定至少一台实时服务器cors_headers后端和该服务器仍将接收请求。
如何在不指定任何服务器的情况下在后端处理请求?如何停止向服务器传播请求,同时将响应发送到浏览器并保持连接处于活动状态?
好消息,HAProxy 2.2刚刚推出 https://www.mail-archive.com/haproxy@formilux.org/msg37852.html“本机响应生成器”功能。它与http-request return https://cbonte.github.io/haproxy-dconv/2.2/configuration.html#http-request%20return指令,可用于提供静态文件或文本字符串,包括动态参数。
目标是避免常见的黑客行为errorfile
.
利用 2.2 版本中引入的另一个指令(http-after-response https://cbonte.github.io/haproxy-dconv/2.2/configuration.html#4.2-http-after-response),OP目标可以通过以下方式实现:
backend cors_headers
# http-response won't work here as the response is generated by HAP
http-after-response set-header Access-Control-Allow-Origin \
"%[req.hdr(Origin)]"
http-after-response set-header Access-Control-Max-Age "31536000"
http-request return status 200 content-type "text/plain" string "" if TRUE
The set-header
and http-request return
可以用以下条件设置if
基于请求标头或来源的子句,具体取决于您的需求(请参阅文档以获取示例)。
通过这种技术,标头和响应可以使用变量:
http-request return status 200 content-type "text/plain" \
lf-string "Hello, you are: %[src]"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)