HAProxy:具有重定向的hdr_dom(host) [英] HAProxy: hdr_dom(host) with redirects

查看:246
本文介绍了HAProxy:具有重定向的hdr_dom(host)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在大多数情况下,我们有几个运行良好的haproxy配置.在我们的方案中,我们仅基于域名路由请求.这里是一个域名drawmessage.com的示例:

We have a couple of haproxy configurations running fine for the most part. In our scenario, we simply route requests based on domain names. Here a sample for one domain, drawmessage.com:

frontend http
    bind *:80

    redirect prefix http://app.drawmessage.com code 301 if { hdr_dom(host) -i www.app.drawmessage.com }
    redirect prefix http://drawmessage.com code 301 if { hdr_dom(host) -i www.drawmessage.com }

    redirect prefix https://drawmessage.com code 301 if { hdr_dom(host) -i drawmessage.com }

    use_backend http:app.drawmessage.com if { hdr_dom(host) -i app.drawmessage.com }
    use_backend http:app.drawmessage.com if { hdr_dom(host) -i app-drawmessage-com.d250.hu }
    use_backend http:drawmessage.com if { hdr_dom(host) -i drawmessage.com }
    use_backend http:drawmessage.com if { hdr_dom(host) -i drawmessage-com.d250.hu }

还有其他域,仅对此域进行过滤.如您所见,在www重定向之后,我们对drawmessage.com进行了特殊的重定向,但从理论上讲,对app.drawmessage.com则没有进行特殊的重定向.

There are other domains also, this is filtered for this domain only. As you can see, after redirects for www, we apply a special redirect for drawmessage.com, but theoretically not for app.drawmessage.com.

frontend https
     bind *:443 ssl crt /var/haproxy

     redirect prefix https://app.drawmessage.com code 301 if { hdr_dom(host) -i www.app.drawmessage.com }
     redirect prefix https://drawmessage.com code 301 if { hdr_dom(host) -i www.drawmessage.com }

     use_backend https:app.drawmessage.com if { hdr_dom(host) -i app.drawmessage.com }
     use_backend https:app.drawmessage.com if { hdr_dom(host) -i app-drawmessage-com.d250.hu }
     use_backend https:drawmessage.com if { hdr_dom(host) -i drawmessage.com }
     use_backend https:drawmessage.com if { hdr_dom(host) -i drawmessage-com.d250.hu }

问题在于,实际上,我们不希望将子域app.drawmessage.com重定向到https,但是由于我们具有该域的重定向,因此重定向规则适用于两者.以某种方式对规则进行重新排序,以使排序与我们要实现的配置相匹配,并不会产生相同的结果,并且还会收到haproxy警告:

The problem is that actually, we do not want a redirect to https for the subdomain app.drawmessage.com, but since we have a redirect for the domain the redirect rule applies for both. Reordering the rules in a way, so that the sorting matches the configuration we want to achieve does produce the same result, and we get haproxy warnings:

a 'redirect' rule placed after a 'use_backend' rule will still be processed before.

如果配置行的顺序影响处理请求的顺序,那么该顺序本身就是配置参数,为什么在use_backend规则之前处理重定向规则?...

If the order of configuration lines affects the order of processing a request, thus the ordering is a configuration parameter itself, why are redirect rules processed before use_backend rules? ...

任何人都建议如何正确选择重定向来实现基于域的路由?我希望使用一种简洁的方法,..

Anyone has a suggestion how to achieve domain-based routing, with the correct preference of redirects? I would prefer a clean and simple way, ..

推荐答案

请勿使用 hdr_dom().只需使用 hdr().

redirect prefix https://drawmessage.com code 301 if { hdr(host) -i drawmessage.com }

后缀 _dom 表示您想将后面给出的值与在指定标头中找到的任意数量的完整的,连续的类似域名的令牌相匹配,因此您提供的模式必须从以下任一位置开始紧跟在之后的字符串的开头.,并且必须在字符串的结尾处结束,并紧随其后的是.那不是您想要做的,因此 hdr_dom()不是正确的提取方法.

The _dom suffix means you want to match the value given later against any number of complete, consecutive domain-name-like tokens found in the specified header, so the pattern you provide must begin either at the beginning of the string or immediately following a . and must end either at the end of the string or be immediately followed by a .. That isn't what you want to do, so hdr_dom() isn't the correct fetch to use.

这种表示法可能意味着您正在将左值与右值进行比较,但是比较实际上是右值与左值的比较.

The notation may imply that you are comparing left value against right value, but the comparison is actually right value against left value.

放置在"use_backend"规则之后的重定向"规则仍将在处理之前,因为这些指令在请求处理的不同阶段由HAProxy代码的不同部分处理.在每个规则类别中,顺序都会保留,但是重定向将在请求处理的开始附近进行,而后端选择则在请求处理的结束附近进行.

a 'redirect' rule placed after a 'use_backend' rule will still be processed before because these directives are handled by different parts of the HAProxy code, at different stages of request processing. Within each class of rule, the order is preserved, but redirects are handled near the beginning and backend selection is near the end of request processing.

这篇关于HAProxy:具有重定向的hdr_dom(host)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆