我对服务器配置这个主题还比较新,现在我遇到了一个问题,无法使用SSL证书访问容器。
我的设置是什么:
我有一个带有Docker的树莓派。与端口80和443连接的容器是一个反向代理,它将传入的子域名定向到不同的其他容器。 例如: webserver.my-domain.com 导向 IP 192.168.178.69:8080。我通过文件夹中的此配置实现了这一点:
<VirtualHost *:80>
ServerName webserver.my-domain.com
ProxyPreserveHost On
DocumentRoot /var/www/html
ProxyPass /.well-known !
ProxyPass / http://192.168.178.69:8080/
ProxyPassReverse / http://192.168.178.69:8080/
RewriteEngine on
RewriteCond %{SERVER_NAME} =webserver.my-domain.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
我还在反向代理容器内创建了一个Let's Encrypt证书。这将创建一个额外的文件webserver-le-ssl.conf
:
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName webserver.my-domain.com
ProxyPreserveHost On
DocumentRoot /var/www/html
ProxyPass /.well-known !
ProxyPass / http://192.168.178.69:8080/
ProxyPassReverse / http://192.168.178.69:8080/
SSLCertificateFile /etc/letsencrypt/live/my-domain.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/my-domain.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>
目前为止还不错。如果我尝试使用URL https://webserver.my-domain.com
,我的内容就可以访问。
我的问题是什么
我使用Portainer来管理我的docker容器,我希望通过portainer.my-domain.com
来访问Portainer。
因此,这是sites-enabled
中的portainer配置:
<VirtualHost *:80>
ServerName portainer.my-domain.com
ProxyPreserveHost On
DocumentRoot /var/www/html
ProxyPass /.well-known !
ProxyPass / http://192.168.178.69:9443/
ProxyPassReverse / http://192.168.178.69:9443/
RewriteEngine on
RewriteCond %{SERVER_NAME} =portainer.my-domain.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
同时,还需要针对此进行SSL配置:
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName portainer.my-domain.com
ProxyPreserveHost On
DocumentRoot /var/www/html
ProxyPass /.well-known !
ProxyPass / http://192.168.178.69:9443/
ProxyPassReverse / http://192.168.178.69:9443/
SSLCertificateFile /etc/letsencrypt/live/my-domain.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/my-domain.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>
如果我调用
192.168.178.69:9443/
,我可以毫无问题地访问容器。
但如果我尝试访问URLportainer.my-domain.com
,我只会得到以下信息:Client sent an HTTP request to an HTTPS server.
但是URL显示为:https://portainer.my-domain.com/
。
所以我不明白为什么会有HTTP请求,即使我的浏览器显示连接是使用https。
有人可以解释一下这个问题并告诉我如何解决吗?
更新:我的解决方案 经过多次尝试,我找到了一个解决方案: 由于我运行反向代理和Portainer作为Docker容器,因此我将两个容器都放入了一个docker-compose网络中:
version: "3.4"
services:
apache:
build:
context: .
dockerfile: Dockerfile
ports:
- "80:80"
- "443:443"
volumes:
- /home/pi/reverse-proxy/:/etc/apache2
networks:
- homeserver
portainer:
image: portainer/portainer-ce:latest
ports:
- "8000:8000"
- "9000:9000"
- "9443:9443"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- portainer_data:/data
networks:
- homeserver
networks:
homeserver:
volumes:
portainer_data:
Apache2的配置文件目录是/etc/apache2
。在这之后,我将ProxyPass的IP更改为容器的名称:
ProxyPass / http://reverse-proxy_portainer_1:9000/
ProxyPassReverse / http://reverse-proxy_portainer_1:9000/
经过这些更改,它开始工作了。