-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

相關問題