101

我正在使用 HAProxy 进行负载平衡,并且只希望我的站点支持 https。因此,我想将端口 80 上的所有请求重定向到端口 443。

我该怎么做?

编辑:我们想重定向到 https 上的相同 url,保留查询参数。因此, http: //foo.com/bar将重定向到https://foo.com/bar

4

16 回答 16

146

我发现这是最大的帮助

使用 HAProxy 1.5 或更高版本,只需将以下行添加到前端配置中:

redirect scheme https code 301 if !{ ssl_fc }
于 2013-05-13T18:06:51.783 回答
69

我没有足够的声誉来评论以前的答案,所以我发布了一个新的答案来补充 Jay Taylor 的答案。基本上他的回答会进行重定向,虽然是隐式重定向,这意味着它将发出 302(临时重定向),但由于问题告知整个网站将作为 https 服务,因此适当的重定向应该是 301(永久重定向)。

redirect scheme https code 301 if !{ ssl_fc }

这似乎是一个很小的变化,但根据网站的不同,影响可能很大,通过永久重定向,我们通知浏览器它不应该从一开始就寻找 http 版本(避免将来的重定向) - 为 https 节省时间网站。它也有助于 SEO,但不会分割链接的汁液。

于 2014-02-18T15:26:54.453 回答
42

重定向所有流量:

redirect scheme https if !{ ssl_fc }

重定向单个 url(如果有多个前端/后端)

redirect scheme https if { hdr(Host) -i www.mydomain.com } !{ ssl_fc }

于 2013-11-07T21:45:44.437 回答
19

将所有 http 重定向到 https 的最佳保证方法是:

frontend http-in
   bind *:80
   mode http
   redirect scheme https code 301

使用“代码 301”有点花哨,但不妨让客户知道它是永久性的。'mode http' 部分对于默认配置不是必需的,但不会受到伤害。如果你有mode tcp默认部分(就像我一样),那么它是必要的。

于 2017-01-12T00:34:38.360 回答
15

根据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
于 2012-11-05T08:32:33.277 回答
12

user2966600的解决方案略有不同......

重定向单个 URL 之外的所有内容(如果有多个前端/后端):

redirect scheme https if !{ hdr(Host) -i www.mydomain.com } !{ ssl_fc }
于 2016-04-22T13:10:56.720 回答
4

就像 Jay Taylor 说的,HAProxy 1.5-dev 有redirect scheme配置指令,它可以完全满足你的需要。

但是,如果您无法使用 1.5,并且您准备从源代码编译 HAProxy,那么我会反向移植该redirect scheme功能,以便它可以在 1.4 中使用。您可以在此处获取补丁:http: //marc.info/ ?l=haproxy&m=138456233430692&w=2

于 2013-11-16T00:52:44.123 回答
2
frontend unsecured *:80
    mode http
    redirect location https://foo.bar.com
于 2013-10-30T08:50:40.597 回答
2

重定向语句是遗留的

改用http- request重定向

acl http      ssl_fc,not
http-request redirect scheme https if http
于 2020-05-07T20:05:13.777 回答
1

在较新版本的 HAProxy 中,建议使用

http-request redirect scheme https if !{ ssl_fc }

将 http 流量重定向到 https。

于 2019-10-07T15:31:09.860 回答
1
  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 }
于 2020-11-06T12:50:13.770 回答
0

如果要重写 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 上尝试以下示例conf:

##########
# 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

我希望这对你有帮助

于 2012-11-05T08:31:07.870 回答
0

为什么不使用 ACL 来区分流量?在我的头顶上:

acl go_sslwebserver path bar
use_backend sslwebserver if go_sslwebserver

这是马修布朗回答的最重要的内容。

请参阅ha 文档,搜索 hdr_dom 和以下内容以查找更多 ACL 选项。有很多选择。

于 2012-11-14T10:23:57.433 回答
0

可以这样做——

  frontend http-in
   bind *:80
   mode http
   redirect scheme https code 301

任何访问 http 的流量都会重定向到 https

于 2020-03-18T16:56:20.020 回答
0

将此添加到 HAProxy 前端配置中:

acl http      ssl_fc,not
http-request redirect scheme https if http

HAProxy - 重定向 HTTP 请求

于 2018-05-16T01:39:44.453 回答
0

简单地:

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
于 2020-08-21T09:50:46.580 回答