Nginx反向代理Docker容器LAN

时间:2018-09-10 07:51:23

标签: docker nginx localhost

我无法理解nginx反向代理的工作原理,也许有人可以提供帮助。可以说我的局域网中带有docker的服务器的IP为192.168.0.3我有这样的docker-compose文件:

version: '2'

  angular-on-nginx:
    build: angular_client
    ports:
      - "80:80"
    volumes:
      - ./angular_client/dist:/usr/share/nginx/html
      - ./angular_client/default.conf:/etc/nginx/conf.d/default.conf

  express:
    build: express_server
    ports:
      - "4444:4444"
    volumes:
      - ./express_server/:/usr/src/app 
    links:
      - database
    depends_on:
      - database

  database:
    image: mongo
    ports:
      - "27777:27777"
    volumes:
      - ./mongodb_data:/data/db

在angular-on-nginx容器上,有angular app和nginx具有以下配置:

server {
    listen       80;
    server_name  localhost;

    root   /usr/share/nginx/html;
    index  index.html index.htm;

    location / {
        try_files $uri $uri/ /index.html;
    }
    location /express {
        proxy_pass http://192.168.0.3:4000/;
    }   
}

,但是在键入192.168.0.3时,此功能不起作用。当输入192.168.0.3:4444时,快速服务器给出的响应很好,但是当输入192.168.0.3/express时,存在502错误的网关。如何使它起作用,以便当我输入192.168.0.3/express时,我得到的结果与键入192.168.0.3:4444时的结果相同?

1 个答案:

答案 0 :(得分:0)

不知道为什么,但这行得通。

server {
    listen       80;
    listen       [::]:80;
    server_name  192.168.0.3;

    root   /usr/share/nginx/html;
    index  index.html index.htm;

    location / {
        try_files $uri $uri/ /index.html;
    }
    location /express/ {
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $host;
        proxy_pass http://192.168.0.3:4444;
    }   
}