【问题标题】:Nginx Request redirection one container to anotherNginx 请求将一个容器重定向到另一个
【发布时间】:2018-01-16 20:08:42
【问题描述】:

我正在使用以下 compose 文件运行两个 centos docker 容器-

version: "2"
services:
  nginx:
    build:
      context: ./docker-build
      dockerfile: Dockerfile.nginx
    restart: always
    ports:
      - "8080:8080"
    command: "/usr/sbin/nginx"
    volumes:
      - ~/my-dir:/my-dir

  data:
    build:
      context: ./docker-build
      dockerfile: Dockerfile.data
    restart: always
    ports:
      - "8081:8081"
    command: "/usr/sbin/nginx"
    volumes:
     - ~/my-dir-1:/my-dir-1

我已经在两个容器中使用 Dockerfile 安装了 nginx 以访问特定目录。 尝试使用 nginx 将请求 http://host-IP:8080/my-data/ 重定向到 data 容器。 下面是我对nginx 容器的 Nginx 配置

/etc/nginx/conf.d/default.conf

server {
    listen 8080;

  location / {
       root   /my-dir/;
       index  index.html index.htm;
       }

}

我可以使用http://host-IP:8080 URL 和my-dir-1 使用http://host-IP:8081 URL 访问my-dir 目录,如何配置Nginx 以使用data 容器重定向请求http://host-IP:8080/my-data URL

【问题讨论】:

    标签: linux docker docker-compose devops docker-swarm


    【解决方案1】:

    我真的不了解您的应用程序的用例以及您为什么要这样做。

    但是您可以使用代理执行此操作,未经测试的代码查找文档,但类似这样。

    http {
      upstream data_container {
        server data:8081;
      }
      server {
        listen 8080;
    
    
        location / {
            root   /my-dir/;
            index  index.html index.htm;
        }
        location /my-data {
          proxy-pass http://data_container$request_uri;
        }
      }
    }
    

    【讨论】:

      【解决方案2】:

      nginx.conf文件

      http {
      
       server {
      
         listen 80;
      
         location /api {
      
           proxy_pass http://<SERVICE_NAME>:8080/my-api/;
      
         }
      
         location /web {
      
           proxy_pass http://<SERVICE_NAME>:80/my-web-app/;
      
         }
      
       }
      
      }
      
      events { worker_connections 1024; }
      

      注意:这里 /my-api 和 /my-web-app 是应用程序上下文路径。 SERVICE_NAME 是 docker-compose.yml 文件中为每个服务指定的名称。

      Dockerfile 用于 nginx

      FROM nginx
      RUN rm /etc/nginx/conf.d/default.conf
      COPY nginx.conf /etc/nginx
      EXPOSE 80
      CMD ["nginx", "-g", "daemon off;"]
      

      现在通过

      访问网址

      【讨论】:

        【解决方案3】:

        如果您在这里寻找 WebSocket conf,它是:

          server {
                server_name  _;
            
                location /ws {
                    proxy_pass http://localhost:8888;
                    # this is the key to WebSocket
                    proxy_http_version  1.1;
                    proxy_set_header    Upgrade $http_upgrade;
                    proxy_set_header    Connection "upgrade";
                    proxy_set_header    Host $http_host;
                    proxy_set_header    X-Real-IP $remote_addr;
                }
            
                location / {
                    proxy_pass http://localhost:8889;
                }
            }
        

        编码愉快:)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-06-24
          • 1970-01-01
          • 2020-10-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多