HAProxy将http重定向到https(ssl)

Jon*_*Chu 89 ssl https redirect http haproxy

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

我该怎么做?

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

Jay*_*lor 129

我发现这是最大的帮助:

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

redirect scheme https code 301 if !{ ssl_fc }
Run Code Online (Sandbox Code Playgroud)

  • 要添加到此,请从User2966600的答案中添加301,使用此选项仅针对特定域重定向到https:`redirect scheme https code 301 if {hdr(Host)-i www.mydomain.com}!{ssl_fc} ` (18认同)
  • 给定答案的等效语法如下:`http-request redirect scheme https code 301 if!{ssl_fc}`.[ALOHA HAProxy 7.0中的http重定向](https://www.haproxy.com/doc/aloha/7.0/haproxy/http_redirection.html)的文档甚至提到"_两个指令的语法是相同的,即,重定向现在被视为遗留,配置应移至http请求重定向form_".我推断,不完全确定,相同的解释适用于HAProxy的开源版本的较新版本. (4认同)

Mar*_*reu 64

我没有足够的声誉评论之前的答案,所以我发布了一个新的答案,以补充杰伊泰勒的答案.基本上他的答案会做重定向,虽然是隐式重定向,这意味着它会发出302(临时重定向),但由于问题通知整个网站将作为https提供,那么相应的重定向应该是301(永久重定向) ).

redirect scheme https code 301 if !{ ssl_fc }
Run Code Online (Sandbox Code Playgroud)

这似乎是一个很小的变化,但影响可能很大,具体取决于网站,通过永久重定向,我们通知浏览器它不应该从一开始就寻找http版本(避免将来重定向) - 为https节省时间站点.它也有助于搜索引擎优化,但不能分割链接的果汁.

  • +1,将你的解决方案添加到Jay Taylor的答案中 (2认同)

小智 38

要重定向所有流量:

redirect scheme https if !{ ssl_fc }

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

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


Mai*_*eya 16

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

frontend http-in
   bind *:80
   mode http
   redirect scheme https code 301
Run Code Online (Sandbox Code Playgroud)

这是一个使用'代码301'的小爱好者,但也许让客户知道它是永久性的.'mode http'部分对于默认配置不是必需的,但不能伤害.如果你有mode tcp默认部分(就像我做的那样),那么这是必要的.


Mat*_*own 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
Run Code Online (Sandbox Code Playgroud)

  • 所以这会将所有内容重定向到https://foo.bar.com.但理想情况下,我们希望http://foo.bar.com/baz重定向到https://foo.bar.com/baz.我们还需要查询参数. (7认同)
  • 而不是使用重定向位置,请尝试重定向前缀https://foo.bar.com.它应该有助于Jon Chu提到的用例. (2认同)

小智 10

用户2966600的解决方案稍有不同......

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

redirect scheme https if !{ hdr(Host) -i www.mydomain.com } !{ ssl_fc }
Run Code Online (Sandbox Code Playgroud)


Fla*_*lat 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]
Run Code Online (Sandbox Code Playgroud)

但是,如果您想将端口 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
Run Code Online (Sandbox Code Playgroud)

我希望这对你有帮助