簡體   English   中英

docker docker-compose multiple sub.domains with SSL/ port 443 & Apache ssl.conf VirtualHost and Dockerfile

[英]docker docker-compose multiple sub.domains with SSL/ port 443 & Apache ssl.conf VirtualHost and Dockerfile

如何使用子域而不需要使用附加到 sub_n.domain.com:444 的端口號

我有:

  • 多個 sub.domains.com 通過 DNS 重定向到我的 VPS(sub1.sub2.sub3.)
  • every.sub_n 應與其自己的 (L)AMP-Stack-Containers 連接(它們在 Ubuntu 上運行。使用相同的 Kernel 加一個 Apache-PHP-Container 每個具有 sub1 / sub2 / sub3 服務加上數據庫和 phpMyAdmin )
  • 每個 sub_n 都有自己的目錄,例如 sub1/ 有自己的 docker-compose.yml,
  • core/ 包含 Dockerfile(用於 apache-PHP & the databases & phpMyAdmin)加上
  • ssl.conf

我確實在端口 443 上運行了以下“實例”之一:

------下面,sub1.domain.com的docker-compose.yml:------------

 sub1:
    hostname: localhost
    container_name: sub1
    build:
      context: ./core
    restart: always
    volumes:
      - ./core/html:/var/www/html
      - ./core/apacheErrorLog:/var/www/apacheErrorLog

    tty: true
    ports:
      - "443:443"
      - "80:80" 

------下面,Dockerfile(用於 PHP-Apache): -------------

ADD ssl.conf /etc/apache2/sites-available/ssl.conf

RUN rm -rf /etc/apache2/sites-enabled/000-default.conf

COPY ./html/ /var/www/html/sub1/
 
RUN a2enmod ssl
RUN a2ensite ssl
RUN a2enmod vhost_alias
RUN a2enmod rewrite

CMD echo "ServerName localhost" >> /etc/apache2/apache2.conf

並且,ssl.conf

--------下面是ssl.conf -----------

<VirtualHost *:80>
    ServerName sub1.domain.com
    RewriteEngine On
    RewriteCond %{HTTPS} !=on
    RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

    ErrorLog /var/log/apache2/error.log
    CustomLog /var/log/apache2/access.log common
</VirtualHost>

<VirtualHost *:443>
    ServerName sub1.domain.com
    ServerAdmin webmaster@domain.com
    DocumentRoot /var/www/html/sub1
    
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    
    SSLEngine on
    
    SSLCertificateFile /etc/apache2/ssl_keys/cert.crt
    SSLCertificateKeyFile /etc/apache2/ssl_keys/key.key
    SSLCertificateChainFile /etc/apache2/ssl_keys/bundle.ca-bundle

    <FilesMatch "\.(cgi|shtml|phtml|php)$">
        SSLOptions +StdEnvVars
    </FilesMatch>

    <Directory /usr/lib/cgi-bin>
        SSLOptions +StdEnvVars
    </Directory>



    <Directory /var/www/html/sub1>
        Options Indexes FollowSymLinks
        AllowOverride all
        Require all granted
    </Directory>


    DirectoryIndex index.php
</VirtualHost>

==================================================== =============

問題是:如果我現在添加一個 sub2.domain.com 以下/調整這些步驟,我需要將 docker-container 暴露到另一個端口,例如 444 - 現在,正確的“重定向”ro DocumentRoot 只會起作用,當端口顯式輸入 url - 否則,將使用“443 的默認值(此處:var/www/html/”,無論哪個 sub_n. - 域輸入到瀏覽器的 url 地址。

所以: https://sub2.domain.com:444/ -> 好的,進入 var/www/html/sub2/ (根據服務定義)

但是https://sub2.domain.com/ -> false,進入 var/www/html/ (= 為在 sub1-docker-container 中運行的 sub1 定義的那個,因此,完全錯誤的 docker 服務......)

好的,我找到了“答案”:

--> 只有一個端口 443 - 如果您需要多個 docker-containerized-LAMP-stacks 運行 SSL 連接/可通過一個特定端口訪問,您需要一個特殊的解決方案來完成這項工作,例如traefik 反向代理https://doc.traefik.io/traefik/providers/docker/

也許,還有其他/更簡單的解決方案,但我已經對 traefik 和 docker-compose 及其路由范式有一些經驗......

隨意添加其他可能更簡單的解決方案!

暫無
暫無

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

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