【发布时间】:2012-10-25 00:28:02
【问题描述】:
我正在使用 HAProxy 进行负载平衡,并且只希望我的网站支持 https。因此,我想将端口 80 上的所有请求重定向到端口 443。
我该怎么做?
编辑:我们希望重定向到 https 上的相同 url,保留查询参数。因此,http://foo.com/bar 将重定向到 https://foo.com/bar
【问题讨论】:
标签: http redirect ssl https haproxy
我正在使用 HAProxy 进行负载平衡,并且只希望我的网站支持 https。因此,我想将端口 80 上的所有请求重定向到端口 443。
我该怎么做?
编辑:我们希望重定向到 https 上的相同 url,保留查询参数。因此,http://foo.com/bar 将重定向到 https://foo.com/bar
【问题讨论】:
标签: http redirect ssl https haproxy
acl host-example hdr(host) -i www.example.com
# for everything not https
http-request redirect scheme https code 301 unless { ssl_fc }
# for anything matching acl
http-request redirect scheme https code 301 if host-example !{ ssl_fc }
【讨论】:
简单地说:
frontend incoming_requsts
bind *:80
bind *:443 ssl crt *path_to_cert*.**pem**
**http-request redirect scheme https unless { ssl_fc }**
default_backend k8s_nodes
【讨论】:
重定向声明是旧版
改用 http-request 重定向
acl http ssl_fc,not
http-request redirect scheme https if http
【讨论】:
可以这样-
frontend http-in
bind *:80
mode http
redirect scheme https code 301
任何访问 http 的流量都会重定向到 https
【讨论】:
在较新版本的 HAProxy 中,建议使用
http-request redirect scheme https if !{ ssl_fc }
将 http 流量重定向到 https。
【讨论】:
I found this to be the biggest help:
使用 HAProxy 1.5 或更高版本,只需将以下行添加到前端配置:
redirect scheme https code 301 if !{ ssl_fc }
【讨论】:
redirect scheme https code 301 if { hdr(Host) -i www.mydomain.com } !{ ssl_fc }
http-request redirect scheme https code 301 if !{ ssl_fc }。 http redirection in ALOHA HAProxy 7.0 的文档甚至提到“两个指令的语法是相同的,也就是说,重定向现在被认为是遗留的,配置应该移动到 http-request 重定向表单”。我不完全确定地推断,相同的解释适用于 HAProxy 的开源版本的较新版本。
将此添加到 HAProxy 前端配置中:
acl http ssl_fc,not
http-request redirect scheme https if http
【讨论】:
将所有 http 重定向到 https 的最佳保证方法是:
frontend http-in
bind *:80
mode http
redirect scheme https code 301
使用“代码 301”有点花哨,但不妨让客户知道它是永久性的。 “模式 http” 部分对于默认配置不是必需的,但不会受到伤害。如果你在默认部分有mode tcp(就像我一样),那么它是必要的。
【讨论】:
user2966600 的解决方案略有不同...
要重定向所有除了单个网址(如果有多个前端/后端):
redirect scheme https if !{ hdr(Host) -i www.mydomain.com } !{ ssl_fc }
【讨论】:
我没有足够的声誉来评论以前的答案,所以我发布了一个新答案来补充 Jay Taylor 的答案。基本上他的回答会进行重定向,虽然是隐式重定向,这意味着它将发出 302(临时重定向),但由于问题告知整个网站将作为 https 服务,因此适当的重定向应该是 301(永久重定向)。
redirect scheme https code 301 if !{ ssl_fc }
这似乎是一个很小的变化,但影响可能很大,具体取决于网站,通过永久重定向,我们会通知浏览器它不应再从一开始就寻找 http 版本(避免未来的重定向) - 一次https 网站的保护程序。它还有助于 SEO,但不会分割链接的汁液。
【讨论】:
就像 Jay Taylor 所说,HAProxy 1.5-dev 有 redirect scheme 配置指令,它完全可以满足您的需要。
但是,如果您无法使用 1.5,并且您准备从源代码编译 HAProxy,我向后移植了 redirect scheme 功能,因此它可以在 1.4 中使用。你可以在这里获取补丁:http://marc.info/?l=haproxy&m=138456233430692&w=2
【讨论】:
重定向所有流量:
redirect scheme https if !{ ssl_fc }
重定向单个 url (如果有多个前端/后端)
redirect scheme https if { hdr(Host) -i www.mydomain.com } !{ ssl_fc }
【讨论】:
frontend unsecured *:80
mode http
redirect location https://foo.bar.com
【讨论】:
为什么不使用 ACL 来区分流量?在我的头顶上:
acl go_sslwebserver path bar
use_backend sslwebserver if go_sslwebserver
这是 Matthew Brown 回答的内容之上。
查看 ha docs ,搜索 hdr_dom 及以下内容以查找更多 ACL 选项。有很多选择。
【讨论】:
如果你想重写 url,你必须改变你的站点虚拟主机添加以下行:
### Enabling mod_rewrite
Options FollowSymLinks
RewriteEngine on
### Rewrite http:// => https://
RewriteCond %{SERVER_PORT} 80$
RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,NC,L]
但是,如果您想将端口 80 上的所有请求重定向到代理后面的 Web 服务器的端口 443,您可以在 haproxy.cfg 上尝试以下 example 配置:
##########
# Global #
##########
global
maxconn 100
spread-checks 50
daemon
nbproc 4
############
# Defaults #
############
defaults
maxconn 100
log global
mode http
option dontlognull
retries 3
contimeout 60000
clitimeout 60000
srvtimeout 60000
#####################
# Frontend: HTTP-IN #
#####################
frontend http-in
bind *:80
option logasap
option httplog
option httpclose
log global
default_backend sslwebserver
#########################
# Backend: SSLWEBSERVER #
#########################
backend sslwebserver
option httplog
option forwardfor
option abortonclose
log global
balance roundrobin
# Server List
server sslws01 webserver01:443 check
server sslws02 webserver02:443 check
server sslws03 webserver03:443 check
希望对你有帮助
【讨论】:
根据http://parsnips.net/haproxy-http-to-https-redirect/,它应该像配置您的 haproxy.cfg 以包含以下内容一样简单。
#---------------------------------------------------------------------
# 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
【讨论】: