Nginx Docker Compose - 添加卷以添加 Nginx.conf(反向代理)

4

我想将我的Web应用程序容器化。目前,我正在使用Apache来提供几个PHP应用程序。

每个应用程序都应该由自己的容器提供。 Nginx应该可以通过80/443端口访问。根据子路由的不同,它应该代理到其中一个容器。

例如:

www.url.de/hello1 --> hello1:80
www.url.de/hello2 --> hello2:80

docker-compose.yml:

version: '3'
services:
    nginx:
            image: nginx:latest
            container_name: reverse_proxy
            volumes:
                    - ./nginx.conf:/etc/nginx/nginx.conf
            ports:
                    - "80:80"
                    - "443:443"
            networks:
                    - app-network
            depends_on:
                    - hello1
                    - hello2

    hello1:
            build: ./test1
            image: hello1
            container_name: hello1
            expose:
                    - "80"
            networks:
                    - app-network
    hello2:
            build: ./test2
            image: hello2
            container_name: hello2
            expose:
                    - "80"
            networks:
                    - app-network

networks:
    app-network:

nginx.conf:

events {

}

http {
    error_log /etc/nginx/error_log.log warn;
    client_max_body_size 20m;

    proxy_cache_path /etc/nginx/cache keys_zone=one:500m max_size=1000m;


    server {
            server_name wudio.de;

            location / {
                    proxy_pass http://hello1:80;
            }

            location /hello1/ {
                    proxy_pass http://hello1:80;
                    rewrite ^/hello1(.*)$ $1 break;
            }

            location /hello2/ {
                    proxy_pass http://hello2:80;
                    rewrite ^/hello2(.*)$ $1 break;
            }

    }
}

如果我运行 docker-compose up -d,只有使用webapp-test1镜像的容器在线。我也可以通过curl localhost:8081访问它。 但是Nginx没有运行。如果我删除将nginx.conf添加到Nginx卷中的行,则可以正常工作。 我做错了什么吗?
http:// was missing. But proxying still not working on subroutes. Only location / is working. How I get /hell1 running?

你有错误信息、堆栈跟踪或类似的东西吗? - Manuel
不幸的是,进程没有出现任何错误/堆栈跟踪。看起来一切都正常工作了。但是Nginx没有上线。如果我删除卷,它就可以工作,并且至少可以通过端口80访问Nginx。 - Nico Schuck
尝试给出你正在尝试映射的nginx.conf文件的完整路径,而不是相对路径。我有一个运行类似于你要求的nginx容器。 - sxm1972
@ThanhNguyenVan nginx:[emerg] /etc/nginx/nginx.conf 中的 URL 前缀无效:11 - Nico Schuck
在nginx中,第11行是哪一行? - Thanh Nguyen Van
显示剩余6条评论
1个回答

0
请注意 proxy_pass 语句。您必须在该语句中提及协议。还要注意如何在 docker-compose.yml 文件中引用服务的名称(在本例中为 hello1)。
events {

}

http {
    error_log /etc/nginx/error_log.log warn;
    client_max_body_size 20m;

    proxy_cache_path /etc/nginx/cache keys_zone=one:500m max_size=1000m;

    server {
       listen 80;
       location / {
          try_files $uri @proxy ;
       }

       location @proxy {
          proxy_pass http://hello1:80/;
       }
    }
}

编辑:请尝试这个

events {

}

http {
    error_log /etc/nginx/error_log.log warn;
    client_max_body_size 20m;

    proxy_cache_path /etc/nginx/cache keys_zone=one:500m max_size=1000m;

    server {
      listen 80;
      location / {
          try_files $uri @proxy ;
      }

      location @proxy {
          if ($request_uri ~* "^\/hello1(\/.*)$") {
            set $url "http://hello1:80$1";
          }

          if ($request_uri ~* "^\/hello2(\/.*)$") {
            set $url "http://hello2:80$1";
          }

          proxy_pass "$url"
      }
    }
}

好的。进展肯定:)。Nginx现在正在运行。但是看起来代理不起作用。我使用了proxy_pass http://127.0.0.1:8081。但是在curl localhost/test1上,我得到502 Bad Gateway错误。 - Nico Schuck
在nginx容器中,回环地址将指向容器而不是主机。您需要将其代理到运行其他容器的服务(从docker-compose文件中的hello1容器)。 - sxm1972
我修改了我的回答中的示例。看看是否有帮助。 - sxm1972
是的,你说得对。我尽可能地将我的代码分解了。我编辑了变更。只有这种方式 / 才能工作。在 /hell1 上,我遇到了 Nginx 的 404 错误。非常感谢你的帮助! - Nico Schuck
抱歉回复晚了。有时候它能正常工作,有时候不行 :/。当前的错误是“/usr/share/nginx/html/hello1/index.html”未找到。 - Nico Schuck
显示剩余9条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接