【问题标题】:Migrating Nginx reverse proxy to traefik => Services do not get served correctly将 Nginx 反向代理迁移到 traefik => 服务无法正确提供
【发布时间】:2020-02-29 14:08:07
【问题描述】:

我想从 Nginx 作为反向代理切换到 traefik,因为 traefik 提供粘性会话,这是我在 Docker Swarm 环境中需要的。这是我的 Nginx 设置的一部分,运行良好:

   location / {
   proxy_pass          http://127.0.0.1:5000;

   proxy_http_version 1.1;
   proxy_set_header Upgrade $http_upgrade;
   proxy_set_header Connection "upgrade";
   proxy_read_timeout 600s;

   proxy_redirect    off;
   proxy_set_header  Host             $http_host;
   proxy_set_header  X-Real-IP        $remote_addr;
   proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
   proxy_set_header  X-Forwarded-Proto $scheme;

 }


   location /auth/ {
   proxy_pass          https://127.0.0.1:8443;

   proxy_http_version 1.1;
   proxy_set_header Upgrade $http_upgrade;
   proxy_set_header Connection "upgrade";
   proxy_read_timeout 600s;

   proxy_redirect    off;
   proxy_set_header  Host             $http_host;
   proxy_set_header  X-Real-IP        $remote_addr;
   proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
   proxy_set_header  X-Forwarded-Proto $scheme;

 }

这是我的 traefik.toml:

debug = false

logLevel = "ERROR"
defaultEntryPoints = ["https","http"]

[entryPoints]
  [entryPoints.http]
  address = ":80"
    [entryPoints.http.redirect]
    entryPoint = "https"
  [entryPoints.https]
  address = ":443"
  [entryPoints.https.tls]
    cipherSuites = [
    "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
    "TLS_RSA_WITH_AES_256_GCM_SHA384"
    ]
    [entryPoints.keycloak]
    address = ":8443"  
    [entryPoints.shinyproxy]
    address = ":5000"  


[retry]

[docker]
exposedByDefault = false

[acme]
email = "langmarkus@hotmail.com"
storage = "acme/certs.json"
entryPoint = "https"
onHostRule = true
[acme.httpChallenge]
entryPoint = "http"

这是我的撰写文件:

version: "3.7"
services:
  shinyproxy:
    build: /home/shinyproxy
    deploy:
      #replicas: 3
    user: root:root
    hostname: shinyproxy
    image: shinyproxy-example
    labels:
      - "traefik.enable=true" # Enable reverse-proxy for this service
      - "traefik.frontend.rule=Host:analytics.data-mastery.com" # Domain name for the app
      - "traefik.port=443"
    ports:
      - 5000:5000
  keycloak:
    image: jboss/keycloak
    labels:
      - "traefik.enable=true" # Enable reverse-proxy for this service
      - "traefik.frontend.rule=Host:analytics.data-mastery.com" # Domain name for the app
      - "traefik.port=443"
    networks:
      - sp-example-net
    volumes:
      - type: bind
        source: /home/certs/fullchain.pem
        target: /etc/x509/https/tls.crt
      - type: bind
        source: /home/certs/privkey.pem
        target: /etc/x509/https/tls.key
      - /home/theme/:/opt/jboss/keycloak/themes/custom/
    environment:
      - PROXY_ADDRESS_FORWARDING=true
      - KEYCLOAK_USER=myadmin
      - KEYCLOAK_PASSWORD=mypassword
    ports:
      - 8443:8443
  reverseproxy:
    image: traefik:v1.7.16
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock # So that Traefik can listen to the Docker events
      - ./traefik/traefik.toml:/traefik.toml # Traefik configuration file
      - ./volumes/traefik-acme:/acme # Tell Traefik to save SSL certs here
    command: --api # Enables the web UI
    ports:
      - "80:80" # The HTTP port
      - "443:443" # The HTTPS port
      - "8080:8080" # The web UI

networks:
  sp-example-net:
    driver: overlay
    attachable: true

SSL 正在运行,我的 keycloak 服务正在这里运行:https://analytics.data-mastery.com:8443/auth/。但是,我想归档与 proxy_pass 相同的行为,我不必在 URL 中使用端口。我需要改变什么?

【问题讨论】:

  • 你考虑使用 traefik 2.1 吗?
  • 我会的,我使用了 1.7.16,因为我无法将卷安装到最新的映像。

标签: docker nginx traefik


【解决方案1】:

如果你想继续使用旧的 traefik 版本,你可以使用下面的堆栈文件(你也可以摆脱 traefik.toml 并只使用 CLI 命令) 使用下面的堆栈文件,您将能够访问 analytics.data-mastery.com 上的 shinyproxy 和 analytics.data-mastery.com/auth 上的 keycloak 此处的导入内容是定义的 rule https://docs.traefik.io/routing/routers/

你也不需要公开这个服务的端口,traefik 会使用内部的

version: "3.7"
services:

  shinyproxy:
    build: /home/shinyproxy
    deploy:
      replicas: 3
    user: root:root
    hostname: shinyproxy
    image: shinyproxy-example
    labels:
      - traefik.enable=true
      - traefik.backend.loadbalancer.swarm=true
      - traefik.backend=shinyproxy
      - traefik.frontend.rule=Host:analytics.data-mastery.com;
      - traefik.port=5000
      - traefik.docker.network=sp-example-net

  keycloak:
    image: jboss/keycloak
    labels:
      - traefik.enable=true
      - traefik.backend.loadbalancer.swarm=true
      - traefik.backend=keycloak
      - traefik.frontend.rule=Host:analytics.data-mastery.com;Path:/auth
      - traefik.port=8443
      - traefik.docker.network=sp-example-net
    networks:
      - sp-example-net
    volumes:
      - type: bind
        source: /home/certs/fullchain.pem
        target: /etc/x509/https/tls.crt
      - type: bind
        source: /home/certs/privkey.pem
        target: /etc/x509/https/tls.key
      - /home/theme/:/opt/jboss/keycloak/themes/custom/
    environment:
      - PROXY_ADDRESS_FORWARDING=true
      - KEYCLOAK_USER=myadmin
      - KEYCLOAK_PASSWORD=mypassword

  reverseproxy:
    image: traefik:v1.7.16
    networks:
      - sp-example-net
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock # So that Traefik can listen to the Docker events
      - ./traefik/traefik.toml:/traefik.toml # Traefik configuration file
      - ./volumes/traefik-acme:/acme # Tell Traefik to save SSL certs here
    command:
      - '--docker'
      - '--docker.swarmmode'
      - '--docker.domain=analytics.data-mastery.com'
      - '--docker.watch'
      - '--accessLog'
      - '--checkNewVersion=false'
      - '--api'
      - '--ping.entryPoint=http'
      # if you want to get reid of the toml file at all
      # - '--entrypoints=Name:http Address::80 Redirect.EntryPoint:https'
      # - '--entrypoints=Name:https Address::443 TLS'
      # - '--defaultentrypoints=http,https'
      # - '--acme.entrypoint=https'
      # - '--acme.email=langmarkus@hotmail.com'
      # - '--acme.storage=/var/lib/traefik/acme.json'
      # - '--acme.acmelogging=true'
      # - '--acme.httpChallenge.entryPoint=http'
      # - '--acme.domains=*.analytics.data-mastery.com,analytics.data-mastery.com'
    ports:
      - "80:80"
      - "443:443"
      - "8080:8080"

networks:
  sp-example-net:
    driver: overlay
    attachable: true

如果你想直接跳转到 traefik2.1,这里有一个link,其中包含很好的使用示例

【讨论】:

  • 我在您的文件中收到两个服务的"backend not found" "/" 错误
  • 尝试访问 traefik 仪表板(端口:8080)并检查后端是否已创建
  • 你还需要将shinyproxy添加到同一个网络
  • ERR_SSL_PROTOCOL_ERROR 是我尝试访问 8080 时得到的结果。一般情况下可能会出现问题,请看:reverseproxy_1 | 216.244.66.247 - - [29/Feb/2020:15:54:26 +0000] "GET /xxx/step-mother-sex HTTP/1.1" 302 5 "-" "Mozilla/5.0 (compatible; DotBot/1.1; http://www.opensiteexplorer.org/dotbot, help@moz.com)" 24 "entrypoint redirect for http" "/xxx/step-mother-sex" 1ms。这里会发生什么? :-|
  • 你正在尝试用https访问8080,你需要用http来做
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-25
  • 2016-10-31
  • 1970-01-01
  • 2023-03-13
  • 1970-01-01
相关资源
最近更新 更多