繁体   English   中英

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

[英]Nginx docker compose - volume add nginx.conf (reverse proxy)

我想容器化我的 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 卷的行,它就可以工作。 我做错了什么?

编辑1:

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 {
          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"
      }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM