如何在不将请求传递到Web服务器的情况下使用HAProxy发送响应

编程入门 行业动态 更新时间:2024-10-27 04:30:22
本文介绍了如何在不将请求传递到Web服务器的情况下使用HAProxy发送响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

由于 CORS (跨源资源共享),服务器正在接收成千上万个 OPTIONS 请求。现在,每个选项请求都被发送到其中一个服务器,这是有点浪费,知道HAProxy可以添加 CORS 头本身没有web服务器的帮助。

前端https-in ... use_backend cors_headers如果METH_OPTIONS ... backend cors_headers rspadd Access-Control-Allow-Origin:\ www.example rspadd访问控制 - 最大年龄:\ 31536000

然而,为了这个工作,我需要在 cors_headers 后端指定至少一个活动服务器服务器仍将接收请求。

如何在后端中处理请求,而无需指定任何服务器?

解决方案

如何将请求传播到服务器,同时向浏览器发送响应并保持连接活动?要做到这一点,唯一的方法是在HAProxy 1.5.14是手动触发503错误(没有服务器可用来处理请求),并设置错误页面到具有自定义CORS头文件。

后端cors_headers 错误文件503 /path/to/custom/file.http

file.http 应包含所需的标题和最后两个空行

HTTP / 1.1 200 OK Access-Control-Allow-Origin:www.example Access- Max-Age:31536000 Content-Length:0 Cache-Control:private < REMOVE THIS LINE COMPLETELY>

此方法有几个限制:

  • 在发送CORS标头之前没有办法检查原点,因此您必须拥有允许起源的静态列表,否则您必须允许所有起点
  • 缺少动态标题:您不能执行

    http-response set-header Date%[date(),http_date]

或设置 Expires b $ b

注意:如果您随时间动态更新HTTP文件,要将更改应用于HAProxy,您必须重新启动它。它可以是正常重新启动或硬重新启动,在这两种情况下,新文件将被加载,缓存和立即提供。

The server is receiving thousands of OPTIONS requests due to CORS (Cross-Origin Resource Sharing). Right now, every options request is being sent to one of the servers, which is a bit wasteful, knowing that HAProxy can add the CORS headers itself without the help of a web server.

frontend https-in ... use_backend cors_headers if METH_OPTIONS ... backend cors_headers rspadd Access-Control-Allow-Origin:\ www.example rspadd Access-Control-Max-Age:\ 31536000

However for this to work I need to specify at least one live server in cors_headers backend and that server will still receive the requests.

How can I handle the request in the backend without specifying any servers? How can I stop the propagation of the request to servers, while sending the response to the browser and keeping the connection alive?

解决方案

The only way to do this is in the HAProxy 1.5.14 is by manually triggering the 503 error (no servers available to handle the request) and setting the error page to the file with custom CORS headers.

backend cors_headers errorfile 503 /path/to/custom/file.http

The file.http should contain the desired headers and 2 empty lines at the end

HTTP/1.1 200 OK Access-Control-Allow-Origin: www.example Access-Control-Max-Age: 31536000 Content-Length: 0 Cache-Control: private <REMOVE THIS LINE COMPLETELY>

This "method" has a couple of limitations:

  • there is no way to check the origin before sending the CORS headers, so you will either have to have a static list of allowed origins or you will have to allow all origins
  • lack of dynamic headers: you can't do

    http-response set-header Date %[date(),http_date]

or set Expires header.

Note: if you are updating the HTTP file dynamically over time, to apply the changes to the HAProxy you will have to restart it. It can be a graceful restart or a hard restart, in either case the new file will be loaded, cached and served immediately.

更多推荐

如何在不将请求传递到Web服务器的情况下使用HAProxy发送响应

本文发布于:2023-10-26 01:11:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1528635.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不将   情况下   服务器   如何在   Web

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!