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

Nic*_*uck 6 nginx docker docker-compose

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

\n

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

\n

例如:

\n
www.url.de/hello1 --> hello1:80\nwww.url.de/hello2 --> hello2:80\n
Run Code Online (Sandbox Code Playgroud)\n

docker-compose.yml:

\n
version: '3'\nservices:\n    nginx:\n            image: nginx:latest\n            container_name: reverse_proxy\n            volumes:\n                    - ./nginx.conf:/etc/nginx/nginx.conf\n            ports:\n                    - "80:80"\n                    - "443:443"\n            networks:\n                    - app-network\n            depends_on:\n                    - hello1\n                    - hello2\n\n    hello1:\n            build: ./test1\n            image: hello1\n            container_name: hello1\n            expose:\n                    - "80"\n            networks:\n                    - app-network\n    hello2:\n            build: ./test2\n            image: hello2\n            container_name: hello2\n            expose:\n                    - "80"\n            networks:\n                    - app-network\n\nnetworks:\n    app-network:\n
Run Code Online (Sandbox Code Playgroud)\n

nginx.conf:

\n
events {\n\n}\n\nhttp {\n    error_log /etc/nginx/error_log.log warn;\n    client_max_body_size 20m;\n\n    proxy_cache_path /etc/nginx/cache keys_zone=one:500m max_size=1000m;\n\n\n    server {\n            server_name wudio.de;\n\n            location / {\n                    proxy_pass http://hello1:80;\n            }\n\n            location /hello1/ {\n                    proxy_pass http://hello1:80;\n                    rewrite ^/hello1(.*)$ $1 break;\n            }\n\n            location /hello2/ {\n                    proxy_pass http://hello2:80;\n                    rewrite ^/hello2(.*)$ $1 break;\n            }\n\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

如果我运行docker-compose up -d ,则只有带有图像webapp-test1 的容器在线。我也可以通过 到达它curl localhost:8081。\nNginx 没有运行。如果我删除将nginx.conf添加到 Nginx 卷的行,它\xc2\xb4s 就会工作。\n我\xc2\xb4m 做错了什么?

\n

编辑1:

\n
http:// was missing. But proxying still not working on subroutes. Only location / is working. How I get /hell1 running?\n
Run Code Online (Sandbox Code Playgroud)\n

sxm*_*972 1

请注意 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/;
       }
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:试试这个

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"
      }
    }
}
Run Code Online (Sandbox Code Playgroud)