首页 文章

无法在AWS负载均衡器后面从http重定向到https

提问于
浏览
2

我正在使用rancher后端在AWS实例上运行traefik . 我在AWS负载均衡器处终止SSL,并在端口80上与实例进行通信,该实例将:80流量转发到traefik容器 .

所以负载均衡器目前有:https:443 ==> http:80 http:80 ==> http:80

这意味着,如果键入https://example.com,则会获得SSL,如果键入http://example.com,则只能获得普通的http连接 .

希望通过http 302进行自动重定向 - 它会将http://example.com重定向到https://example.com .

到目前为止,我未成功尝试的是以下内容:

** AWS负载均衡器**

https:443 => http:80
http:80 => http:81

traefik.toml
------------
[entryPoints]
  [entryPoints.http]
  address = ":81"
     [entryPoints.http.redirect]
     regex = "^http://example.com/(.*)"
     replacement = "https://example.com/$1"
  address = ":80"

docker-compose.yml
------------------
API-Proxy:
  container_name: api-proxy
  image: traefik
  volumes:
  - "/var/run/docker.sock:/var/run/docker.sock"
  - "$PWD/traefik.toml:/etc/traefik/traefik.toml"
  command: "--web --rancher --docker.domain=rancher.localhost --logLevel=DEBUG"
  cpu_shares: 128
  restart: always
  ports:
  - 80:80/tcp
  - 81:81/tcp
  - 8100:8080/tcp

当我尝试通过端口80访问时,会出现超时 . Traefik日志似乎没有帮助 .

这是一个愚蠢的方法吗?或者使用Let的加密在traefic容器上终止SSL是否更好?

2 回答

  • 1

    在你的Traefik配置中试试这样的东西 . 然后将LB上的端口443和80转发到Traefik上的端口80 .

    [entryPoints]
      [entryPoints.http]
         address = ":80"
         [entryPoints.http.redirect]
         regex = "^http://(.*)"
         replacement = "https://$1"
    
  • 1

    我目前在AWS上的Kubernetes中这样做 . 要做得恰到好处,有点蠢,但这完全有可能 .

    首先,您需要确保您的ELB正在端口80上侦听HTTP(而不是HTTPS),并在端口443上侦听HTTPS . 如果您在端口80上侦听了IBM的HTTPS,您将会得到客户非常奇怪的行为 . 先检查一下 . Note: 如果您使用Helm部署了Traefik,则这是默认行为 .

    使用 aws elb describe-load-balancers 打印出所有ELB . 你'll have to find the ELB in there (I don'知道如何告诉你它是哪一个)并查看 LoadBalancerDescriptions[].ListenerDescriptions[].Listener.ProtocolInstanceProtocol 以确保它们分别是 HTTPSHTTP .

    其次,这就是你在 config.toml 中所需要的:

    [entryPoints]
      [entryPoints.http]
      address = ":80"
        [entryPoints.http.redirect]
          regex = "^http://(.*)"
          replacement = "https://$1"
      [entryPoints.httpn]
      address = ":8880"
      compress = true
    

    说明:

    • 收听端口80

    • 为端口80上的任何流量设置永久重定向到端口8880

    • 使用HTTP侦听端口8880并启用gzip压缩

    ELB应该将端口80映射到端口80,将端口443映射到端口8880.现在所有HTTP流量将自动重定向(使用 curl -v -L http://example.com 进行测试)到HTTPS并在ELB处终止并作为HTTP转发到Traefik .

    我仍在寻找一种在部署时为ELB侦听器指定协议的好方法,但除了在部署Traefik之后通过AWS控制台手动更改它们之外,我还没有找到一个好的解决方案 .

相关问题