-1

我新的nginx的,我不知道这是正常的行为对...多个搬运工服务听同一主机和端口

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

我会解释这里就是我试图完成......

我有2个额外的服务service1service2这些服务与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生成的配置文件: http://pushsc.com/show/code/58f739790a58d602a0b99d22

而且当我在浏览器中查看本地主机我得到:

欢迎nginx的!

如果您看到此页面,说明nginx web服务器已成功安装 并正在运行。需要进一步的配置。

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

感谢您使用nginx。

回答

0

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

然后,您应该在每个server上下文中使用location正确定义到每项服务的路由。

所以,你应该修改/etc/nginx/conf.d/default.confnginx容器是这样的:

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

我用这LIB:https://github.com/jwilder/nginx-proxy生成nginx的配置文件...操作你知道它为什么创造了工作机会? – Vladimir

+0

从我看到的情况来看,这是用来代理子域的,而不是路径 –

+0

这是什么意思?我怎样才能使它工作? – Vladimir

相关问题