如何使用haproxy重定向url

时间:2018-02-02 13:25:15

标签: apache .htaccess https haproxy httpd.conf

我想使用haproxy将https://myserver/myapplication/重定向到https://myserver.domain.com/myapplication/

这是我的haproxy配置

frontend LB_http
   bind 10.123.122.112:80
   reqadd X-Forwarded-Proto:\ http
   default_backend LB


frontend LB_https
   bind 10.123.122.112:443 ssl crt /usr/local/apache2/conf/server.pem
   reqadd X-Forwarded-Proto:\ https
   default_backend LB

backend LB
   redirect scheme https if !{ ssl_fc }
   mode http
   stats enable
   stats hide-version
   stats uri /stats
   stats realm Haproxy\ Statistics
   stats auth haproxy:redhat            # Credentials for HAProxy Statistic report page.
   balance roundrobin                   # Load balancing will work in round-robin process.
   option httpchk
   option httpclose
   option forwardfor

   server myserver.domain.com myserver.domain.com:80          # backend server.

我已经通过添加以下两行来编辑配置文件

acl no_domain hdr(host) -i myserver
http-request redirect code 301 prefix %[hdr(host)].domain.com%[path] if no_domain

但现在,当我尝试

  

MYSERVER /所有MyApplication /

我猜这个网址会重定向多次。它正在将我重定向到这样的非常长的网址

  

https://myserver/myapplication/myserver.domain.com/myapplication/myserver.domain.com/myapplication/myserver.domain.com/myapplication/myserver.domain.com/myapplication/myserver.domain.com/myapplication/myserver.domain.com/myapplication/

我错过了什么?

1 个答案:

答案 0 :(得分:0)

我已经修改了下面的代码,它开始按预期工作

frontend LB_http
   bind 10.123.122.112:80
   reqadd X-Forwarded-Proto:\ http
   default_backend LB


frontend LB_https
   bind 10.123.122.112:443 ssl crt /usr/local/apache2/conf/server.pem
   reqadd X-Forwarded-Proto:\ https
   default_backend LB

backend LB
   acl no_domain hdr(host) -i myserver
   http-request redirect code 301 prefix https:\/\/myserver.domain.com if no_domain
   redirect scheme https if !{ ssl_fc }
   mode http
   stats enable
   stats hide-version
   stats uri /stats
   stats realm Haproxy\ Statistics
   stats auth haproxy:redhat            # Credentials for HAProxy Statistic report page.
   balance roundrobin                   # Load balancing will work in round-robin process.
   option httpchk
   option httpclose
   option forwardfor

   server myserver.domain.com myserver.domain.com:80          # backend server.

现在当我给出

  

MYSERVER / MyApplication的

重定向到

  

https://myserver.domain.com/myapplication