首页 文章

使用Traws在Aws Loadbalancer后面进行HTTPS重定向

提问于
浏览
2

我正在尝试将所有传入的Traefik从http重定向到https,用于从具有自定义端口的docker容器中提供服务的Web应用程序 .

如果我构建这个docker compose文件,并缩放应用程序,一切都按预期工作 . 我能够请求应用程序的http和https,但我尝试完成只有https获取服务并且http被重定向到https .

由于我使用的是Docker-Compose文件,因此我没有Traefik.toml,并尝试在没有它的情况下完成此操作 .

Docker Compose:

traefik:
  image: traefik:latest
  command:
   - "--api"
   - "--docker"
   - "--docker.domain=example.com"
   - "--logLevel=DEBUG"
   - "--docker.watch"
  labels:
    - "traefik.enable=true"
  ports:
    - "80:80"
    - "8080:8080"
    - "443:443"
  volumes:
    - /var/run/docker.sock:/var/run/docker.sock
    - /dev/null:/traefik.toml

application:
  image: application
  command: web
  tty: false
  stdin_open: true
  restart: always
  expose:
    - "8081"
  labels:
    - "traefik.backend=application"
    - "traefik.frontend.rule=HostRegexp:{subdomain:[a-z]+}.example.com"
    - "traefik.frontend.priority=1"
    - "traefik.enable=true"
  volumes:
    - /var/run/docker.sock:/var/run/docker.sock

我尝试了应用程序容器的不同变体,例如:

- "traefik.frontend.entryPoints=http,https"
- "traefik.frontend.redirect.entryPoint=https"
- "traefik.frontend.headers.SSLRedirect=true"

但我可以完成的最大化是使用SSLRedirect标签的许多重定向响应,并且没有我从traefik获得以下内容,并且http或https请求都没有正确转发 .

level=error msg="Recovered from panic in http handler: runtime error: invalid memory address or nil pointer dereference"

任何人都可以把我推向正确的方向吗?

提前致谢 ;)

I run under the following Settings

user:~$ docker --version
 Docker version 1.13.1, build 092cba3

 user:~$ docker-compose --version
 docker-compose version 1.8.0

Docker PS Response

IMAGE           COMMAND                 ... PORTS                                                              NAMES
application     "dotnet Web..."         ... 8081/tcp                                                           components_application_1
traefik:latest  "/traefik --api --..."  ... 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp, 0.0.0.0:8080->8080/tcp   components_traefik_1

Infrasturcture Setup

aws-elb => vpc => ec2...ecn 
                   traefik per instance, 
                   n applications per instance

1 回答

  • 4

    经过深入研究,我自己找到了解决方案 .

    我添加后,问题是应用程序容器上缺少标签

    - "traefik.frontend.headers.SSLProxyHeaders=X-Forwarded-Proto: https"
    - "traefik.frontend.headers.SSLRedirect=true"
    

    在我的应用程序容器上,它像一个带有清晰的301重定向的魅力 .

    为什么需要标头,默认情况下,aws-elb接受https请求并使用HTTP(80)将其转发到连接的实例,在此过程中,elb会将 X-Forwarded-Proto: https 标头添加到请求中 .

    由于traefik不知道它在elb后面运行,它会一遍又一遍地重定向 . 但是Header阻止了这种行为 .

相关问题