[英]Interfacing Docker containers apps with an existing Apache web server
我對 Docker 相當陌生,我在理解如何使我的“包含”應用程序可從 Internet 訪問時遇到了一些麻煩。
[我當前的配置] 兩個網站(在 /var/www/html 中)+ 標准 Apache + 一些 tweeking 我想應用於我部署的所有新內容(基於頭 CSP 的重寫,使用Certbot續簽 SSL 證書等)。 我對我目前的 conf 相當滿意。
[我的兩個應用程序] 都是獨立的應用程序(只需要一個數據庫)。
-官方 wordpress圖像。 (-> 新域名)
- 一個基於 django 的應用程序,包含基於本教程的 gunicorn 服務器(-> 現有域的子域)
[問題] 如果我將兩個應用程序都綁定到端口 8080 和 8000 上,瀏覽器將無法訪問它,因為 DNS 服務器不處理端口(據我所知,如果我錯了,請糾正我)。 如果我在標准端口上綁定,則會與現有的 Apache 發生沖突。
你會怎么處理? 我可以使用某種mod_proxy重定向到容器的內部 IP 嗎? 有沒有更清潔、更簡單、更安全的方法來做到這一點?
<VirtualHost *:443>
ServerName sub.mydomain.io
Redirect "/" "http://172.17.0.2/"
ErrorLog /var/log/apache2/error.zarebski.io.com.log
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/mydomain.io/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/mydomain.io/privkey.pem
</VirtualHost>
如果多個應用程序運行在不同的端口和不同的本地 IP 地址上,推薦的方法是使用 反向代理
基本上,您將 apache 配置為將請求轉發到這些服務,即:
<VirtualHost *:443>
ServerName sub.mydomain.io
ProxyPass "/wordpress" "http://172.17.0.2:8080/"
ProxyPass "/django" "http://172.17.0.2:8000/"
ErrorLog /var/log/apache2/error.mydomain.io.com.log
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/mydomain.io/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/mydomain.io/privkey.pem
</VirtualHost>
如果您有多個外部域,您可以使用基於名稱的虛擬主機
好吧,我花了一段時間才弄明白,因為有兩個主要的角落案例。 我將堅持一個案例: wordpress 圖像
<VirtualHost *:443>
ServerName new_domain.eu
ProxyPass / http://localhost:8081/
<Location />
AddOutputFilterByType SUBSTITUTE text/html
SetOutputFilter proxy-html
ProxyPassReverse /
Substitute "s|http://localhost:8081/|https://new_domain.eu/|i"
RequestHeader unset Accept-Encoding
</Location>
SSLCertificateFile /etc/letsencrypt/live/new_domain.eu/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/new_domain.eu/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
[首先],我無法從他們的本地 IP訪問容器(即 172.7.0.3:80,不知道為什么),所以我使用了在設置容器時定義的 localhost 端口:
docker run -e WORDPRESS_DB_PASSWORD=thePassWord --name wordpress --link wordpressdb:mysql -p 8081:80 -v "$PWD/html":/var/www/html -d wordpress
[其次] 棘手的部分是正確處理相對 url(例如 some/path/to/css),因為這些是不可訪問的。 顯然,這是一個眾所周知的問題。 這部分是最長的:圍繞 Apache 2.4 發生了很大變化,並且語法沒有很好的文檔記錄。 基本上,
Substitute "s|http://localhost:8081/|https://new_domain.eu/|i"
替換 html 中的所有 url,以便可以正確訪問相關資源(css、js、png 等)。
[可能的改進] 我對從外部世界可見的端口 8081 不太滿意。 這意味着可以從這個端口訪問該應用程序,繞過我在apache.conf 中設置的規則。 我通過添加iptables 規則解決了這個問題
iptables -A INPUT -p tcp -s localhost --dport 8081 -j ACCEPT
iptables -A INPUT -p tcp --dport 8081 -j DROP
不太優雅,所以如果有人有建議,請告訴我。
\\//_
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.