【问题标题】:Adding header to response for specific URLs with HAproxy使用 HAproxy 将标头添加到特定 URL 的响应中
【发布时间】:2015-04-19 14:36:13
【问题描述】:

我的 HAproxy 配置中有一个简单的条件(我为 frontendbackend 尝试了这个):

acl no_index_url path_end .pdf .doc .xls .docx .xlsx
rspadd X-Robots-Tag:\ noindex if no_index_url

它应该将 no-robots 标头添加到不应被索引的内容中。但是在解析配置时它给了我这个WARNING

acl 'no_index_url' will never match because it only involves keywords
    that are incompatible with 'backend http-response header rule'

acl 'no_index_url' will never match because it only involves keywords
    that are incompatible with 'frontend http-response header rule'

根据documentationrspadd 可以用于前端后端path_end 用在 frontend 中的示例中。为什么我会收到此错误,这是什么意思?

【问题讨论】:

    标签: load-balancing haproxy


    【解决方案1】:

    从 HaProxy 1.6 开始,您将无法忽略错误消息。要使其正常工作,请使用临时变量功能:

    frontend main
       http-request set-var(txn.path) path
    
    backend local
       http-response set-header X-Robots-Tag noindex if { var(txn.path) -m end .pdf .doc }
    

    【讨论】:

    • 你拯救了我的一天 :)
    • 这可能作为一种解决方案很有用,尽管在我通过 OpnSense 使用 HAProxy 的情况下它没有用(我认为)。对Why am I getting this error and what does it mean的实际问题有什么反馈吗?
    • 好的。我在这里找到了答案:discourse.haproxy.org/t/…。不知道为什么会这样,无论如何......
    【解决方案2】:

    显然,即使有警告,在前端使用 acl 也可以正常工作。所有带有 .pdf、.doc 等的资源都已添加正确的 X-Robots-Tag

    换句话说,这个WARNING 具有误导性,而实际上acl 确实匹配。

    【讨论】:

    • 只是给遇到此问题的任何人的注意事项,在 1.6.2 中,尽管有警告,但它确实添加了标题,但也将其添加到任何不符合规则的响应中,所以我相信它实际上被忽略了。无论如何,对我来说就是这种情况,如果这会有所作为,我会使用“除非”。接受的答案对我有用,所以试试吧。
    【解决方案3】:

    如果使用 v1.6 以下的 haproxy,请创建一个新的后端块(可能是默认后端的副本)并在其中添加特殊标头。然后在前端有条件地使用该后端。即

    use_backend alt_backend if { some_condition } 
    

    诚然不是一个理想的解决方案,但它确实可以完成工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-15
      • 1970-01-01
      相关资源
      最近更新 更多