简体   繁体   English

在Docker上使用Nginx反向代理

[英]Using Nginx reverse proxy with Docker

I am trying to develop a distributed Angular app deployed on Nginx that should connect to a backend service. 我正在尝试开发部署在Nginx上的分布式Angular应用程序,该应用程序应连接到后端服务。

docker-compose.yml: 泊坞窗,compose.yml:

version: '3'
services: 
  backend_service_1:
    build:
      context: ./app
      dockerfile: Dockerfile
    ports:
      - "3001:5000"
    networks: 
      - my-network

  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile.3      
    ports:
      - "3000:80"
    networks: 
      - my-network
    links:
      - backend_service_1

networks: 
  my-network:

nginx.conf: nginx.conf:

upstream backend {
  server backend_service_1:3001;
}

server {
  listen 80;
  server_name localhost;

  location / {
    root /usr/share/nginx/html/ki-poc;
    index index.html index.htm;
    try_files $uri $uri/ /index.html =404;
  }

  location /backend {
    proxy_pass http://backend/;
    proxy_redirect     off;
    proxy_set_header   Host $host;
    proxy_set_header   X-Real-IP $remote_addr;
    proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header   X-Forwarded-Host $server_name;
  }
}

I can access the app on localhost:3000 . 我可以在localhost:3000上访问该应用程序。 I can also get a response from the backend service on localhost:3001 using the browser. 我还可以使用浏览器从localhost:3001上的后端服务获得响应。 However, when I try to get a response from the backend service using the proxy on localhost:3000/backend I receive the following error message: [error] 5#5: *4 connect() failed (111: Connection refused) while connecting to upstream, client: 172.20.0.1, server: localhost, request: "GET /backend HTTP/1.1", upstream: "http://172.20.0.2:3001/", host: "localhost:3000" 但是,当我尝试使用localhost:3000/backend上的代理从后端服务获得响应时,收到以下错误消息: [error] 5#5: *4 connect() failed (111: Connection refused) while connecting to upstream, client: 172.20.0.1, server: localhost, request: "GET /backend HTTP/1.1", upstream: "http://172.20.0.2:3001/", host: "localhost:3000"

Can you tell my, why the request to the linked backend container is getting refused? 您能告诉我,为什么对链接的后端容器的请求被拒绝了吗?

You shoul use the port of the container in the nignx config, not the one of the host. 您应该在nignx配置中使用容器的端口,而不是主机之一。

upstream backend {
    server backend_service_1:5000;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM