如何为我的 docker 容器配置反向代理?

How do I configure a reverse proxy for my dockers containers?

我目前正在将我的 raspberry pi 配置为我的个人家庭服务器,我想使用 docker。我有几个不同的容器 :

-Apache/PHP 81:80

-Nextcloud 8080:80

-我的世界 25565:25565

我已经有一个动态域 (http://felixbestwaifu.hopto.org)

当我尝试访问 felixbestwaifu.hopto.org:8080 时,超时 :c

我的目标

Nextcloud

网站

我尝试通过在 /sites-enabled// 中创建 nextcloud.conf 来使用 NGINX sites-available/,但根本不起作用^^'

server {
    listen 80;
    server_name nextcloud;

    location /nextcloud {
        proxy_pass 192.168.2.150:8080/;
    }
}

注意:192.168.2.150 是我网络中的 pi 静态 IP

所以这是我的问题:如何为 docker 容器设置反向代理?

非常感谢您的帮助>//<(抱歉英语不好)

让我们假设您的 Apache-PHP 容器名为 apache_php。你应该有两个 .conf 文件,内容相似(或多或少取决于你在 conf 文件中需要什么)。

/etc/apache2/sites-available/minecraft.conf:

<VirtualHost *:80>
        ServerName minecraft.com

        ServerAdmin webmaster@minecraft
        DocumentRoot /home/minecraft
        ProxyPreserveHost On
        ProxyPass "/" "http://minecraft:25565/"
        ProxyPassReverse "/" "http://minecraft:25565/"
        ProxyRequests Off

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

</VirtualHost>

/etc/apache2/sites-available/nextcloud.conf:

<VirtualHost *:80>
        ServerName nextcloud.com

        ServerAdmin webmaster@nextcloud
        DocumentRoot /home/nextcloud
        ProxyPreserveHost On
        ProxyPass "/" "http://nextcloud:8080/"
        ProxyPassReverse "/" "http://nextcloud:8080/"
        ProxyRequests Off

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

</VirtualHost>

注意 ProxyPassProxyPassReverse 中的尾随 /。如果您不添加它们,最终 URL 将类似于 http://minecraft:25565your_page,而尾随 / 它将是 http://minecraft:25565/your_page.

在这种情况下,如果有人进入 minecraft.com,您的 Apache 会将他代理到 minecraft 站点并且 *minecraft 只能访问通过您的 Apache 容器。

请注意,minecraft.comminecraft 是我在下面讨论的名为 proxy 的网络中的两个不同网站

这是我在我的容器中所做的,但让我根据我的知识解释一下:

假设你的 Apache、你的 Nextcloud 和 Minecraft 在同一个 network 中(在这种情况下你应该创建一个网络,在我的情况下它叫做 proxy 网络)和所有这三个容器应该通过 ping 互相看到。

在这种情况下,您的 docker-compose.yml 文件应该是什么样子:

version: '3.9'

services:
    apache_php:
        build: .
        restart: always
        ports:
           - "81:80"
    minecraft:
        build: .
        restart: always
        ports:
           - "25565:25565"
    nextcloud:
        build: .
        restart: always
        ports:
           - "8080:80"
networks:
default:
external:
  name: proxy
volumes:
db_data: {}

所以在这个文件中,我创建了三个名为 apache_phpminecraftnextcloud 的图像,它们是通过 Dockerfile 通过 build 关键字构建的.我还代理了81到80端口,25565到25565端口和8080到80端口。

然后您可以创建一个 Dockerfile 或修改现有的具有类似内容的:

FROM your_os_base:tag

COPY minecraft.conf /etc/apache2/sites-available/
COPY nextcloud.conf /etc/apache2/sites-available/
COPY my_bash_commands.sh /root/
RUN chmod +x /root/my_bash_commands.sh
RUN /root/my_bash_commands.sh
ENTRYPOINT ["your_thing"]

现在我的 my_bash_commands.sh 是什么?在大多数情况下,默认 shell 是 sh,我创建了一个 bash 文件,以便获得我想要的结果。

my_bash_commands.sh类似这样:

#!/usr/bash

service apache2 start
a2ensite minecraft
a2ensite nextcloud
a2enmod proxy
a2enmod proxy_http
service apache2 restart

如果你在 docker-compose.yml 所在的路径中,现在你可以通过 运行 docker-compose up -d 创建它们,否则你应该使用 docker-compose -f /absolute_patht_to/docker-compose.yml up -d.