Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Nginx docker compose-卷添加Nginx.conf(反向代理)_Docker_Nginx_Docker Compose - Fatal编程技术网

Nginx docker compose-卷添加Nginx.conf(反向代理)

Nginx docker compose-卷添加Nginx.conf(反向代理),docker,nginx,docker-compose,Docker,Nginx,Docker Compose,我想将我的web应用程序容器化。目前,我正在使用Apache提供一些PHP应用程序 每个应用程序都应该由自己的容器提供。 Nginx应该可以通过端口80/443访问。根据子路线,它应代理到其中一个容器 例如: www.url.de/hello1 --> hello1:80 www.url.de/hello2 --> hello2:80 version: '3' services: nginx: image: nginx:latest

我想将我的web应用程序容器化。目前,我正在使用Apache提供一些PHP应用程序

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

例如:

www.url.de/hello1 --> hello1:80
www.url.de/hello2 --> hello2:80
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:
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;
            }

    }
}
http:// was missing. But proxying still not working on subroutes. Only location / is working. How I get /hell1 running?
docker compose.yml:

www.url.de/hello1 --> hello1:80
www.url.de/hello2 --> hello2:80
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:
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;
            }

    }
}
http:// was missing. But proxying still not working on subroutes. Only location / is working. How I get /hell1 running?
nginx.conf:

www.url.de/hello1 --> hello1:80
www.url.de/hello2 --> hello2:80
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:
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;
            }

    }
}
http:// was missing. But proxying still not working on subroutes. Only location / is working. How I get /hell1 running?
如果运行docker compose up-d,则只有带有imagewebapp-test1的容器处于联机状态。我还可以通过
curl localhost:8081
访问它。 Nginx没有运行。如果我删除了将nginx.conf添加到nginx卷中的行,它就工作了。 我做错了什么

Edit1:

www.url.de/hello1 --> hello1:80
www.url.de/hello2 --> hello2:80
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:
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;
            }

    }
}
http:// was missing. But proxying still not working on subroutes. Only location / is working. How I get /hell1 running?

请注意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 {
          if ($request_uri ~* "^\/hello1(\/.*)$") {
            set $url "http://hello1:80$1";
          }

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

          proxy_pass "$url"
      }
    }
}

您是否有错误消息、stacktrace或类似信息?不幸的是,没有。进程正在运行,没有任何错误/堆栈跟踪。看起来一切都很顺利。但Nginx并不在线。如果我删除了该卷,它会工作,并且至少可以通过端口80Run
访问Nginx。docker logs Nginx\u container\u id
显示Nginx的错误日志container@RajeshGupta不是我,请尝试给出要映射的nginx.conf文件的完整路径,而不是相对路径。我有一个nginx容器运行类似于您的requirementok。绝对进步:)。Nginx正在运行。但看起来代理不起作用。我用了你的通行证。但是在curl localhost/test1上,我得到错误502 Bad Gateway。在nginx容器中,环回地址将引用容器而不是主机。您需要将其代理给运行其他容器(docker compose文件中的hello1)的服务。我编辑我的问题。你知道怎么回事吗?我在回答中修改了这个例子。看看能不能帮上忙,谢谢。但它不适用于多个应用程序,对吗?或者如何将路线映射到服务?