簡體   English   中英

我如何在我的主機上使用 Apache2(Lamp 安裝)並運行 Traefik(反向代理 Docker),因為兩者都使用端口 80 和 443?

[英]How can I use Apache2 on my host machine (Lamp install) and run Traefik (Reverse proxy Docker) since both are using ports 80 & 443?

我有一台運行 LAMP 環境和 Apache2 的 Debian 10 機器,我將指代主機。 主機在虛擬主機上運行的網站很少,例如:

<VirtualHost *:80>
        ServerName VirtualExample.com
        ServerAlias www.VirtualExample.com

        ServerAdmin development@example.cafe
        DocumentRoot /var/www/hosted_sites/VirtualExample

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
        RewriteEngine on
        RewriteCond %{SERVER_NAME} =VirtualExample.com [OR]
        RewriteCond %{SERVER_NAME} =www.VirtualExample.com
        RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
<IfModule mod_ssl.c>
    <VirtualHost *:443>
        ServerName VirtualExample.com
        ServerAlias www.VirtualExample.com

        ServerAdmin development@example.cafe
        DocumentRoot /var/www/hosted_sites/VirtualExample

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        Include /etc/letsencrypt/options-ssl-apache.conf
        SSLCertificateFile /etc/letsencrypt/live/VirtualExample.com/fullchain.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/VirtualExample.com/privkey.pem
    </VirtualHost>
</IfModule>

或者像這樣隱藏在反向代理后面:

<VirtualHost *:80>
    ServerAdmin development@example.cafe
    ServerName api.staging.example.fr
    ProxyPreserveHost On
    ProxyPass / http://localhost:3001/ Keepalive=On
    ProxyPassReverse / http://localhost:3001/
</VirtualHost>

在我們的最后一種情況下,http://localhost:3001/ 可以指直接在主機上運行的應用程序或 docker 應用程序(其中 3001 將是公開的端口)

現在從長遠來看,我計划對所有其他應用程序進行 dockerize,但現在我的目標只是擺脫 Apache Reverse 代理並在 traefik 中設置它們(以便更好地監控未來的 docker 應用程序)。

目前,我沒有設法同時運行 Apache2 和 Traefik,問題是共享端口 80 和 443。

我的 Traefik 配置相當通用:

Docker-compose

version: '3.3'
networks:
  wan:
    external: true

services:
  traefik:
    container_name: traefik
    restart: always
    image: traefik:1.7-alpine
    networks:
      - wan
    ports:
      - 81:80
      - 444:443
    labels:
      - traefik.frontend.rule=Host:traefik.example.com
      - traefik.port=8080
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - ./traefik.toml:/traefik.toml
      - ./acme.json:/acme.json

traefik.toml

defaultEntryPoints = ["http", "https"]

[entryPoints]
  [entryPoints.dashboard]
    address = ":8080"
    [entryPoints.dashboard.auth]
      [entryPoints.dashboard.auth.basic]
        users = ["user:encryptedpassword"]
  [entryPoints.http]
    address = ":80"
      [entryPoints.http.redirect]
        entryPoint = "https"
  [entryPoints.https]
    address = ":443"
      [entryPoints.https.tls]

[api]
entrypoint="dashboard"

[acme]
email = "development@example.com"
storage = "acme.json"
entryPoint = "https"
onHostRule = true
  [acme.httpChallenge]
  entryPoint = "http"

[docker]
domain = "example.com"
watch = true
network = "wan"

到目前為止,我看到的唯一解決方案是將 Traefik 80 和 443 端口映射到其他值,並將一個 Apache 反向代理設置為重定向? 這似乎有點模糊,我覺得這只是冰山一角。

  1. 將所有 HTTP/HTTPS 流量重定向到 Traefik 的解決方案是否正確? 是否可以將無法解析為 Traefik 的流量回退到 Apache ?

  2. 最好的方法是什么? 代理反向時有哪些好的做法?

+> 將來,我將繼續對服務器上的每個應用程序進行 dockerize。

你不能在同一個 IP 地址的同一個端口上綁定 2 個進程:沒有辦法做到這一點。 正如您已經發現的,解決方案是使用不同的端口:80 和 443 用於 apache,例如,20080 和 20443 用於 traefik。

另一種解決方案(非常不鼓勵)可能是將新 ip 關聯到同一個以太網卡,這樣同一個物理接口有 2 個 ip:在第一個上,您可以在端口 80 和 443 上綁定 apache,在第二個上您可以綁定 traefik在端口 80 和 443 上。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM