AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 1136690
Accepted
Guiomar Raissa
Guiomar Raissa
Asked: 2023-07-11 20:12:16 +0800 CST2023-07-11 20:12:16 +0800 CST 2023-07-11 20:12:16 +0800 CST

在 haproxy 的错误响应上添加自定义标头

  • 772

我有以下 haproxy 配置,它access-control-allow-origin使用以下配置在成功的 200 个请求上添加标头。我的问题是,当我遇到超时或 haproxy 本身(不是我的上游服务器)因其他原因引发错误时,不会添加此标头。如何在例如 504 响应中添加此标头?

global
        log /dev/log    local0
        log /dev/log    local1 notice
        chroot /var/lib/haproxy
        stats socket /run/haproxy/admin.sock mode 660 level admin
        stats timeout 30s
        user haproxy
        group haproxy
        daemon

        ca-base /etc/ssl/certs
        crt-base /etc/ssl/private

        ssl-default-bind-ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA>
        ssl-default-bind-ciphersuites TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256
        ssl-default-bind-options ssl-min-ver TLSv1.2 no-tls-tickets

defaults
        log     global
        mode    http
        option  httplog
        option  dontlognull
        timeout connect 5s
        timeout client  30s
        timeout server  30s
        errorfile 400 /etc/haproxy/errors/400.http
        errorfile 403 /etc/haproxy/errors/403.http
        errorfile 408 /etc/haproxy/errors/408.http
        errorfile 500 /etc/haproxy/errors/500.http
        errorfile 502 /etc/haproxy/errors/502.http
        errorfile 503 /etc/haproxy/errors/503.http
        errorfile 504 /etc/haproxy/errors/504.http

frontend www-https
    bind *:80
    bind *:443 ssl crt /etc/ssl/cert.io.pem

    http-response add-header access-control-allow-origin "myCoolWebsite.com"

    redirect scheme https code 301 if !{ ssl_fc }
    mode http

    default_backend myBackend


backend myBackend
  server myServer 123.456.789.101:2345



编辑:我主要通过为 504 添加自定义错误文件来解决这个问题,如下所示。唯一的问题是我无法弄清楚如何将 动态设置access-control-allow-origin为仅当前请求的域而不是*. 这是一个问题,因为某些浏览器在接收*. 我有什么想法可以做到这一点吗?

// 504.http
HTTP/1.0 504 Gateway Time-out
Cache-Control: no-cache
Connection: close
Content-Type: text/html
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods:\ GET,\ HEAD,\ OPTIONS,\ POST,\ PUT
Access-Control-Allow-Credentials:\ true
Access-Control-Allow-Headers:\ Origin,\ Accept,\ X-Requested-With,\ Content-Type,\ Access-Control-Request-Method,\ Access-Control-Request-Headers,\ Authorization

{
  "html": {
    "body": {
      "h1": "504 Gateway Time-out",
      "#text": "The server didn't respond in time."
    }
  }
}
haproxy
  • 1 1 个回答
  • 102 Views

1 个回答

  • Voted
  1. Best Answer
    Rapidmod
    2023-07-29T22:37:25+08:002023-07-29T22:37:25+08:00

    在错误文档上动态设置标头是 HAProxy 的一个限制,目前没有开箱即用的功能可直接实现此目的。由于错误在到达后端服务器之前就已被处理,因此动态设置 Access-Control-Allow-Origin 标头很困难。

    对于此用例,通常建议的解决方法是通过专用后端发送错误响应。在此后端中,您可以执行脚本或使用可以检查请求并动态添加标头的应用程序。

    HAProxy 配置步骤如下:

    frontend www-https
        bind *:80
        bind *:443 ssl crt /etc/ssl/cert.io.pem
        redirect scheme https code 301 if !{ ssl_fc }
        mode http
        use_backend errorBackend if { status eq 504 }
        default_backend myBackend
    
    backend errorBackend
        mode http
        errorfile 504 /etc/haproxy/errors/504.http
        http-response set-header Access-Control-Allow-Origin %[hdr(origin)]
        server errSrv localhost:2346
    
    backend myBackend
        server myServer 123.456.789.101:2345
    

    当 haproxy 本身返回错误(例如由于超时而导致 504)时,会在到达后端之前引发错误标志,并且它将直接返回预先配置的错误页面,而不将请求传递给后端。因此,在这种情况下,haproxy 将无法处理涉及动态属性的标头(例如,根据请求域而变化)。

    将错误发送到专用后端的解决方法使 haproxy 的行为如下:

    1. 客户端请求到达前端。
    2. Haproxy 尝试将请求转发到后端。
    3. 如果后端超时(通常会直接从 haproxy 触发 504),我们会将请求定向到专用的“错误后端”。
    4. “错误后端”创建 504 消息,包括基于原始请求的标头(因为“错误后端”的工作方式与普通后端类似)。
    5. 然后将此定制的错误消息发送回客户端。

    诀窍在于我们实际上并不允许 haproxy 触发直接错误响应。超时现在将导致切换到另一个可以处理基于请求的动态标头的后端。因此,它确实在配置中创建了另一个抽象级别,可以将其视为一个新层。

    • 2

相关问题

  • 是否可以使用负载均衡器加密内部网络服务器之间的流量[关闭]

  • HAProxy 和后端服务器之间丢失一半的数据

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve