如何更改nginx-proxy设置 [英] How to change nginx-proxy settings

查看:244
本文介绍了如何更改nginx-proxy设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用这个lib:



https://github.com/jwilder/nginx-proxy



这是我的docker-compose文件:

  version:'2'
services:
nginx-proxy:
image:jwilder / nginx-proxy
container_name: nginx-proxy
ports:
- 80:80
卷:
- /var/run/docker.sock:/tmp/docker.sock:ro

whoami:
image:jwilder / whoami
环境:
- VIRTUAL_HOST = whoami.local
service1:
image:mynode:1.4
构建:。
卷:
- 。:/ app
重新启动:总是
环境:
- VIRTUAL_HOST = service1.local
service2:
image: mynodeother:1.3
build:。
卷:
- 。:/ app
重新启动:总是
环境:
- VIRTUAL_HOST = service2.local
/ pre>

我添加了2个新的节点服务...



我可以这样做: curl -H主机:service2.locallocalhost 并从service2 ...获得响应。



问题是什么好处我有这个吗如何在80端口运行service1?



这里是 Dockerfile from service1:

  FROM node:6.9.4 

#创建应用程序目录
RUN mkdir -p / usr / src / app
WORKDIR / usr / src / app

运行npm安装nodemon -g

#安装应用程序依赖项
COPY package.json / usr / src / app /
运行npm安装

#捆绑应用程序源
COPY。 / usr / src / app

EXPOSE 8080
CMD [npm,start]


解决方案

很简单,只需要在80端口运行所有的服务,然后在Dockerfile中EXPORT那个端口就可以了。
$ b

只要你不把这个端口发布给你的主机(就像你用nginx一样),没有问题。



优点是您可以通过使用主机名来访问另一个服务,这意味着容器的名称,这很酷,因为您不需要知道分配给每个容器的当前IP地址。 p>

所以如果你使用 bash sh 您应该可以使用主机名ping其他服务:



内部服务1:
ping service2



关于 nginx-proxy 的好东西是,它会检测到你是否缩放了服务和它将自动更新nginx配置:



docker-compose scale service1 = 3



我将启动2个其他服务实例1,无论您是否拥有100个服务,其余的服务可以使用主机名:service1。



所以您可以平衡负载,而不用担心同一服务的每个实例的IP地址。


I am using this lib:

https://github.com/jwilder/nginx-proxy

Here is my docker-compose file:

version: '2'
services:
  nginx-proxy:
    image: jwilder/nginx-proxy
    container_name: nginx-proxy
    ports:
      - "80:80"
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro

  whoami:
    image: jwilder/whoami
    environment:
      - VIRTUAL_HOST=whoami.local
  service1:
    image: mynode:1.4
    build: .
    volumes:
        - .:/app
    restart: always
    environment:
      - VIRTUAL_HOST=service1.local
  service2:
    image: mynodeother:1.3
    build: .
    volumes:
        - .:/app
    restart: always
    environment:
      - VIRTUAL_HOST=service2.local

I added 2 new node services...

I can do like this: curl -H "Host: service2.local" localhost and get response from service2....

Questions are what benefits I have from this? And how can I run service1 on 80 port?

here is Dockerfile from service1:

FROM node:6.9.4

# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

RUN npm install nodemon -g

# Install app dependencies
COPY package.json /usr/src/app/
RUN npm install

# Bundle app source
COPY . /usr/src/app

EXPOSE 8080
CMD [ "npm", "start" ]

解决方案

It's easy, you just need to run all your services in 80 port and EXPOSE that port in your Dockerfile, that's it.

As long as you don't publish that port to your host (like you're doing it with nginx), there's no problem.

The advantage is that every service you have there can reach the other one by using the hostname, it means, the container's name, this is cool because you don't need to know the current ip address assigned to every container.

So if you go into one of those services with bash or sh you should be able to ping the other services by using the hostname:

Inside service1: ping service2

The good thing about nginx-proxy is that it's going to detect if you scale one of your services and it will update the nginx config automatically:

docker-compose scale service1=3

I will start 2 other instances of your service1 and no matter if you have 100, the rest of the services can reach them by using the hostname: service1.

So you can balance the load without worring about the ip address of every instance of the same service.

这篇关于如何更改nginx-proxy设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆