微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何从dockerised nginx到达另一个容器

我在docker容器中有Nginx,在另一个docker容器中有一个nodejs webapp.
可以从端口8080上的主机服务器访问nodejs服务器.

Nginx docker容器正在侦听端口80(稍后将执行证书,首先此基础必须正常工作).

现在我希望将子域转发到此8080 nodejs应用程序.让我们说app1.example.com

从外面我可以通过服务器ip(或主机名)和端口8080到达app,但不能在app1.example.com上.它确实适用于app1.example.com:8080(我在主机服务器上打开了端口8080).

当我接近app1.example.com时,我得到一个糟糕的网关Nginx消息所以我进入第一个Nginx容器,但是如何回到主机服务器代理将其传递到主机服务器的端口8080(而不是端口) 8080的Nginx容器).寻找反向EXPOSE语法.

主要的问题是,当然如果我使用ip和端口127.0.0.1:8080它将尝试Nginx容器….
那么如何让Nginx容器路由回到主机127.0.0.1:8080?

我已经尝试了0.0.0.0并定义了一个上游,实际上一直在谷歌搜索,并尝试了很多配置……但还没有找到一个工作….

编辑
刚刚发现,这个docker命令可能会有所帮助:

sudo docker network inspect bridge

显示了cotainers中使用的IP地址(在我的情况下为172.17..0.2),但不确定每次docker重启时该地址都保持不变…(例如服务器重启)

编辑
我现在跟着碱性答案(但仍然没有工作):

我的docker-compose.yml文件

version: "2"
services:
  Nginx:
    container_name: Nginx
    image: Nginx_img
    build: ../docker-Nginx-1/
    ports:
      - "80:80"
    networks:
      - backbone
  nodejs:
    container_name: nodejs
    image: merites/docker-simple-node-server
    build: ../docker-simple-node-server/
    networks:
    - backbone
    expose: 
    - 8080
  networks:
    backbone:
      driver: bridge

和我的Nginx(为简单起见,跳过conf.d文件夹中的包含):

worker_processes 1;

events {worker_connections 1024; }

http {

  sendfile on;

  upstream upsrv {
      server nodejs:8080;
  }

  server {

    listen 80;
    server_name  app1.example.com;

    location / {
        proxy_pass http://upsrv;
    }

  }
}

编辑31-08-2016

这个migth是问题,这个名字不是主干,而是在文件夹启动之后调用srevice:

sudo docker network ls

输出

NETWORK ID          NAME                       DRIVER              ScopE
1167c2b0ec31        bridge                     bridge              local               
d06ffaf26fe2        dockerservices1_backbone   bridge              local               
5e4ec13d790a        host                       host                local               
7d1f8c32f259        none                       null                local 

编辑01-09-2016

这可能是由于我的Nginx docker容器设置方式造成的?

这是我使用的docker文件

############################################################
# Dockerfile to build Nginx Installed Containers
# Based on Ubuntu
############################################################

# Set the base image to Ubuntu
FROM ubuntu

# File Author / Maintainer
MAINTAINER Maintaner Name

# Install Nginx

# Add application repository URL to the default sources
# RUN echo "deb http://archive.ubuntu.com/ubuntu/ raring main     universe"     >> /etc/apt/sources.list

# Update the repository
RUN apt-get update

# Install necessary tools
RUN apt-get install -y nano wget dialog net-tools

# Download and Install Nginx
RUN apt-get install -y Nginx  

# Remove the default Nginx configuration file
RUN rm -v /etc/Nginx/Nginx.conf

# copy a configuration file from the current directory
ADD Nginx.conf /etc/Nginx/

# Append "daemon off;" to the beginning of the configuration
RUN echo "daemon off;" >> /etc/Nginx/Nginx.conf

# Expose ports
EXPOSE 80

# Set the default command to execute
# when creating a new container
CMD service Nginx start

我的最终解决方案1月1日. 2016

我现在使用这个撰写文件

version: "2"
services:
    Nginx:
      image: Nginx
      container_name: Nginx
      volumes:
        - ./Nginx-configs:/etc/Nginx/conf.d
      ports:
        - "80:80"
      networks:
        - backbone
    nodejs:
      container_name: nodejs
      image: merites/docker-simple-node-server
      build: ../docker-simple-node-server/
      networks:
        - backbone
      expose: 
        - 8080
networks:
  backbone:
    driver: bridge

在项目文件夹中,从你运行docker-compose up -d,我添加一个名为Nginx-configs的文件夹.该文件夹将“覆盖”名为/etc/Nginx/conf.d的Nginx容器中的所有文件

因此,在添加此卷装入之前,我从Nginx容器中复制了default.cfg.使用命令:

docker exec -t -i container_name / bin / bash

而不是cat /etc/Nginx/conf.d/default.conf

并使用Nginx配置在项目文件夹中添加了相同的default.conf.

除了认我添加了这个内容的app1.conf:

upstream upsrv1 {
    server nodejs:8080;
}

server {

    listen 80;
    server_name  app1.example.com;


    location / {
        proxy_pass http://upsrv1;
    }

}

这样,我可以轻松添加第二个应用程序……第三个等等.

所以基础工作现在正在……

最佳答案:

这是一个最佳实践.仅将端口80暴露在主机外部. nodejs app可以位于只能通过Nginx访问的专用网络中.

version: "2"
services:
    Nginx:
      ...
      ports:
        - "80:80"
      networks:
        - backbone
    nodejs:
      ...
      networks:
        - backbone
      expose: 
        - 8080

networks:
  backbone:
    driver: bridge

Nginx.conf文件中,上游服务器可以列为nodejs:8080. docker守护程序会将其解析为正确的内部ip.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐