HAProxy 重定向到子域

HAProxy redirect to subdomain

我正在尝试重定向这些:

这些:

但是在文档和执行此操作的最佳方法上苦苦挣扎。

*更新*

这就是我目前的工作。如果我传入:

然后重定向到:

...和配置部分:

acl blog_page path_beg -i /blog
use_backend blog_site if blog_page
backend blog_site
reqrep        ^([^\ :]*)\ \/?(.*)\/blog\/?(.*)    \ /
redirect prefix http://blog.example.co.uk code 301

前端部分中的以下行将完成此重写和重定向。

为清楚起见,显示为多行,这必须全部出现在配置的一行中:

http-request redirect 
        code 301 
        location https://blog.example.com%[capture.req.uri,regsub(^/blog,)]  
        if { hdr(host) -i www.example.com } { path_beg /blog }

如果主机 header 匹配 www.example.com 并且路径以 blog 开头,则重定向到以文字字符串 https://blog.example.com 开头的位置,然后连接通过采用请求 URI (路径 + 查询字符串)并使用正则表达式替换从开头删除 /blog

正在验证:

$ curl -v 'http://www.example.com/blog/posts?which=this&that=1'
* Hostname was NOT found in DNS cache
*   Trying 127.0.0.1...
* Connected to www.example.com (127.0.0.1) port 80 (#0)
> GET /blog/posts?which=this&that=1 HTTP/1.1
> User-Agent: curl/7.35.0
> Host: www.example.com
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Content-length: 0
< Location: https://blog.example.com/posts?which=this&that=1

重定向位置似乎是正确的。

如果您想分别重定向 http 和 https,则需要两行,每行测试一个附加条件以确定原始请求是通过 http 还是 https。

使用 regsub() 转换器需要 HAProxy 1.6+。