多个码头服务可以在同一主机和端口上进行监听 [英] Multiple docker services to listen on same host and port

查看:273
本文介绍了多个码头服务可以在同一主机和端口上进行监听的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是nginx的新手,我不知道这是正常的行为...



这是我使用的lib: https://github.com/jwilder/nginx-proxy





我有2个附加服务 service1 service2 这些服务是具有API端点的简单node.js映像

  service1具有路由:
- service1 / api / first
- service1 / api / second
`

`
service2有路由:
- service2 / api / third
- service2 / api / 4th
`

所以可以从同一个主机访问这个服务,像这样:
localhost / service1 / api / first
localhost / service2 / api / third


我试过这样:

我的'docker-compose.yml`文件:


版本:'2'
服务:
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.1
volumes:
- 。:/ app
restart:always
环境:
- VIRTUAL_HOST = service1.local
- VIRTUAL_PORT = 8080
service2:
image:mynodeother:1.2
卷:
- / app
重新启动:总是
环境:
- VIRTUAL_HOST = service2.local
- VIRTUAL_PORT = 8081

这里是从命令生成配置文件 docker exec nginx-proxy cat /etc/nginx/conf.d/default.conf
http://pushsc.com/show/code/58f739790a58d602a0b99d22



另外当我在浏览器中访问localhost时,我得到:


欢迎使用nginx!



如果看到此页面,nginx Web服务器已成功安装
并正常工作。需要进一步的配置。



有关在线文档和支持,请参阅nginx.org。
nginx.com提供商业支持。



感谢您使用nginx。



解决方案

尽量不要在 nginx 配置文件中使用IP地址。
此外,您应该为两个服务使用相同的端口号:8080(如果这是nodejs应用程序正在侦听的端口)。



那么你应该正确在每个服务器上下文中使用位置定义每个服务的路由。



因此,您应该在 nginx 容器内修改 /etc/nginx/conf.d/default.conf 像这样:

 #service1.local 
上游service1.local {
##可以连接nginxproxy_default网络
#nginxproxy_service1_1
服务器service1:8080;
}

server {
server_name service1.local;
听80;
access_log /var/log/nginx/access.log vhost;
location / service1 {#note this line
proxy_pass http://service1.local;
}
}

#service2.local
上游service2.local {
##可以连接nginxproxy_default网络
# nginxproxy_service2_1
服务器service2:8080; #same port
}

server {
server_name service2.local;
听80;
access_log /var/log/nginx/access.log vhost;
location / service2 {#note this line
proxy_pass http://service2.local;
}
}


i am new to nginx and I am not sure is this normal behavior...

Here is the lib I am using: https://github.com/jwilder/nginx-proxy

I will explain here what I trying to accomplish...

I have 2 additional services service1 and service2 those services are simple node.js images with API endpoints

service1 have routes:
- service1/api/first
- service1/api/second
`

`
service2 have routes:
- service2/api/third
- service2/api/fourth
`

So is possible to be able to access this services from same host, like this:
localhost/service1/api/first
localhost/service2/api/third
?

I tried like this:

My `docker-compose.yml` 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.1
    volumes:
        - .:/app
    restart: always
    environment:
      - VIRTUAL_HOST=service1.local
      - VIRTUAL_PORT=8080
  service2:
    image: mynodeother:1.2
    volumes:
        - .:/app
    restart: always
    environment:
      - VIRTUAL_HOST=service2.local
      - VIRTUAL_PORT=8081

Here is generated config file from command docker exec nginx-proxy cat /etc/nginx/conf.d/default.conf: http://pushsc.com/show/code/58f739790a58d602a0b99d22

Also when I visit localhost in browser I get:

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org. Commercial support is available at nginx.com.

Thank you for using nginx.

解决方案

Try not to use IP addresses inside nginx config file. Also, you should use the same port number for both services: 8080 (if this is the port that nodejs application is listening).

Then you should properly define your routes to each service using location in each server context.

So, you should modify /etc/nginx/conf.d/default.conf inside nginx container like this:

# service1.local
upstream service1.local {
            ## Can be connect with "nginxproxy_default" network
            # nginxproxy_service1_1
            server service1:8080;
}

server {
    server_name service1.local;
    listen 80 ;
    access_log /var/log/nginx/access.log vhost;
    location /service1 { #note this line
        proxy_pass http://service1.local;
    }
}

# service2.local
upstream service2.local {
            ## Can be connect with "nginxproxy_default" network
            # nginxproxy_service2_1
            server service2:8080; #same port
}

server {
    server_name service2.local;
    listen 80 ;
    access_log /var/log/nginx/access.log vhost;
    location /service2 { #note this line
        proxy_pass http://service2.local;
    }
}

这篇关于多个码头服务可以在同一主机和端口上进行监听的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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