Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Nginx 在同一主机和端口上侦听多个docker服务_Nginx_Docker_Docker Compose_Microservices_Jwilder Nginx Proxy - Fatal编程技术网

Nginx 在同一主机和端口上侦听多个docker服务

Nginx 在同一主机和端口上侦听多个docker服务,nginx,docker,docker-compose,microservices,jwilder-nginx-proxy,Nginx,Docker,Docker Compose,Microservices,Jwilder Nginx Proxy,我是新来nginx的,我不确定这是不是正常的行为 以下是我正在使用的库: 我将在这里解释我试图实现的目标 我有两个附加服务service1和service2这些服务是带有API端点的简单node.js映像 service1 have routes: - service1/api/first - service1/api/second ` ` service2 have routes: - service2/api/third - service2/api/fourth ` So is pos

我是新来nginx的,我不确定这是不是正常的行为

以下是我正在使用的库:

我将在这里解释我试图实现的目标

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

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
下面是从命令
docker exec nginx proxy cat/etc/nginx/conf.d/default.conf
生成的配置文件:

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

欢迎来到nginx

如果您看到此页面,则nginx web服务器已成功安装 还有工作。需要进一步配置

有关在线文档和支持,请参阅nginx.org。 可在nginx.com上获得商业支持

感谢您使用nginx


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

然后,您应该在每个
服务器
上下文中使用
位置
正确定义到每个服务的路由

因此,您应该在
nginx
容器中修改
/etc/nginx/conf.d/default.conf
,如下所示:

# 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;
    }
}

我正在使用这个库:生成nginx配置文件。。。你知道它为什么创建worng conf吗?据我所见,它是用来通过子域代理的,而不是通过path@Vladimir这意味着您无法使用此工具。您的解决方案也已试用,但无法使用。它将仅从第一个服务返回路由,但无法从第二个服务访问路由…@Vladimir将您的共同语言文件发送给我