我正在使用HAProxy进行负载均衡,仅想支持https访问我的站点。因此,我希望将所有端口80的请求重定向到端口443。
如何操作?
编辑:我们希望重定向到相同的https URL,保留查询参数。因此,http://foo.com/bar 将 重定向到 https://foo.com/bar。
如何操作?
编辑:我们希望重定向到相同的https URL,保留查询参数。因此,http://foo.com/bar 将 重定向到 https://foo.com/bar。
我没有足够的声望来评论之前的回答,所以我发表了一个新的答案来补充Jay Taylor的答案。基本上他的答案将会重定向,虽然是一个隐性的重定向,意味着它将发出302(临时重定向),但既然问题表明整个网站将作为https提供,则适当的重定向应该是301(永久重定向)。
redirect scheme https code 301 if !{ ssl_fc }
这似乎是一个微小的变化,但它对网站的影响可能是巨大的。通过永久重定向,我们告诉浏览器从一开始就不再寻找http版本(避免未来的重定向),这对于https网站来说是一个节省时间的好方法。它还有助于SEO,但不会分散你链接的权重。
重定向所有流量:
如果没有{ ssl_fc },则重定向方案为https
重定向单个URL(在多个前端/后端的情况下)
如果{ hdr(Host) -i www.mydomain.com } !{ ssl_fc },则重定向方案为https
将所有http重定向到https的最佳保证方法是:
frontend http-in
bind *:80
mode http
redirect scheme https code 301
这是使用“301代码”的稍微高级一些的方法,但最好让客户端知道它是永久的。“mode http”部分在默认配置中不是必需的,但也无妨。如果你在默认部分(就像我一样)有mode tcp
,那么它是必要的。
http-request redirect scheme https code 301
,因为前缀 http-request
允许使用 log-format
来影响记录日志的方式:https://docs.haproxy.org/2.4/configuration.html#4.2-http-request%20redirect - Mikko Rantalainen#---------------------------------------------------------------------
# Redirect to secured
#---------------------------------------------------------------------
frontend unsecured *:80
redirect location https://foo.bar.com
#---------------------------------------------------------------------
# frontend secured
#---------------------------------------------------------------------
frontend secured *:443
mode tcp
default_backend app
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend app
mode tcp
balance roundrobin
server app1 127.0.0.1:5001 check
server app2 127.0.0.1:5002 check
server app3 127.0.0.1:5003 check
server app4 127.0.0.1:5004 check
基于user2966600的解决方案做了轻微的改动...
为了重定向除一个单独的URL之外的所有URL(如果有多个前端/后端):
redirect scheme https if !{ hdr(Host) -i www.mydomain.com } !{ ssl_fc }
redirect scheme
的配置指令,可以实现你需要的功能。redirect scheme
功能移植到了1.4版本中。你可以在这里获取补丁:http://marc.info/?l=haproxy&m=138456233430692&w=2。重定向语句已经过时
请使用http-request redirect
acl http ssl_fc,not
http-request redirect scheme https if http
frontend unsecured *:80
mode http
redirect location https://foo.bar.com
frontend http-in
bind *:80
mode http
redirect scheme https code 301
redirect scheme https code 301 if { hdr(Host) -i www.mydomain.com } !{ ssl_fc }
。 - Quentin Skousenhttp-request redirect scheme https code 301 if !{ ssl_fc }
。ALOHA HAProxy 7.0中的http重定向文档甚至提到“两个指令的语法是相同的,也就是说,重定向现在被视为旧版,配置应该转移到http-request redirect形式”。我推断(不完全确定),这个解释同样适用于新版本的开源版本HAProxy。 - rodolfojcj