简体   繁体   English

HAProxy-将一个域的流量重定向到https,将其他域的流量重定向到仅http

[英]HAProxy - Redirect traffic of one domain to https and other domains to http only

I am using dedicated servers to host 4 domains and 3 subdomains on Centos & WHM. 我正在使用专用服务器在Centos&WHM上托管4个域和3个子域。 Recently planned to use HAProxy for load balancing between them. 最近计划使用HAProxy在它们之间进行负载平衡。

What I am trying to achieve is to redirect all the traffic of one particular domain to https using HAProxy configuration on frontend because I am terminating the SSL of that one particular domain at HAProxy. 我试图实现的是使用前端的HAProxy配置将一个特定域的所有流量重定向到https,因为我要在HAProxy终止该特定域的SSL。

here's what i have used 这是我用过的

frontend www-https
    bind haproxy_www_public_IP:443 ssl crt /etc/ssl/private/example.com.pem
    reqadd X-Forwarded-Proto:\ https
    default_backend www-backend


backend www-backend
    redirect scheme https if !{ ssl_fc }
    server www-1 www_1_private_IP:80 check
    server www-2 www_2_private_IP:80 check

I have googled for solution but most of the solution available are telling to redirect all the traffic to https or http. 我已经用谷歌搜索解决方案,但是大多数可用的解决方案都在告诉将所有流量重定向到https或http。

If I understand correctly, you want one domain (in the configuration below it is httpsonlydomain.com ) to only be accessible by https and all http requests to that domain get forwarded onto https. 如果我理解正确,则您希望一个域(在下面的配置中为httpsonlydomain.com )只能由https访问,并且对该域的所有http请求都将转发到https上。 For the other domains they can work by either http or https with no forwarding. 对于其他域,它们可以通过http或https进行工作,而无需转发。 Finally I have assume that all four domains (including httpsonlydomain.com ) will use the www-backend backend. 最后,我假设所有四个域(包括httpsonlydomain.com )都将使用www-backend后端。

If that is the case then this should do the trick: 如果是这种情况,那么就可以做到这一点:

frontend www-http
    bind haproxy_www_public_IP:80
    acl https_domain hdr(host) -i httpsonlydomain.com
    redirect scheme https if !{ ssl_fc } https_domain
    default_backend www-backend

frontend www-https
    bind haproxy_www_public_IP:443 ssl crt /etc/ssl/private/example.com.pem
    default_backend www-backend

backend www-backend
    server www-1 www_1_private_IP:80 check
    server www-2 www_2_private_IP:80 check

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM