主机在Nginx Docker Compose中未找到上游。

huangapple go评论53阅读模式
英文:

host not found in upstream with nginx docker compose

问题

我知道这个问题已经被问过很多次,但是其中没有一个解决方案适用于我。

我正在尝试使用nginx将我的Angular应用程序和Node.js后端docker化。

我所做的是创建了一个docker-compose文件。
它包括三个服务和nginx。

1:space-frontend

2:space-api

3:mongodb

我在nginx配置文件中通过它们的服务名称调用前端和后端,如http://space-frontend:80和http://space-api:3000,但是日志中出现了错误

[emerg] 1#1: host not found in upstream "space-api" in /etc/nginx/nginx.conf:23

前端正常工作。
我无法理解我错在哪里。

供参考,
我的前端Docker文件

FROM node:16-alpine AS builder
WORKDIR /app
COPY . .
RUN npm i && npm run build --prod
FROM nginx:alpine
RUN mkdir /app
COPY --from=builder /app/dist/Space-Locator/browser /app
COPY nginx.conf /etc/nginx/nginx.conf

我的前端nginx配置

events {
    worker_connections 1024;
}
http {
    include /etc/nginx/mime.types;

    server {
        listen       80;
        server_name  localhost;
        root /app;

        location / {
            index  index.html;
            try_files $uri $uri/ /index.html;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    }
}

后端API Docker文件

FROM node:14-alpine as build-step
RUN mkdir -p /usr/app
WORKDIR /usr/app
COPY package.*json /usr/app/
RUN npm install
COPY . /usr/app/
EXPOSE 3000
CMD [ "npm", "start" ]

我的docker-compose文件

version: "3.8"
services:
  reverse_proxy:
    image: nginx:1.17.10
    container_name: reverse_proxy
    depends_on:
      - space-frontend
      - space-api
      - database
    volumes:
      - ./reverse_proxy/nginx.conf:/etc/nginx/nginx.conf  
    ports:
      - 80:80
  space-frontend:
    container_name: space-frontend
    image: space-frontend
    build: 
      context: ./space-frontend
    ports:
      - 4000:80
  space-api:
    container_name: space-api
    hostname: space-api
    image: space-api
    build: 
      context: ./space-api
    ports:
      - 3000:3000
    links:
      - database  
    environment:
      MONGO_INITDB_DATABASE: spaceLocator 
      MONGODB_URI: mongodb://db:27017
    depends_on:
      - database
    volumes:
      - ./db-data/mongo/:/data/database
    networks:
      - node-network
  database:
    container_name: db
    image: mongo
    restart: on-failure
    ports:
      - 27017:27017
    volumes:
      - ./mongodb:/data/database
    networks:
      - node-network  
volumes:
  dbdata6:
networks:
  node-network:
    external: true
    driver: bridge  

以及我的反向代理nginx.conf文件

events {
    worker_connections 1024;
}
http {
    server {
        listen 80;
        server_name  127.0.0.1;

        root   /usr/share/nginx/html;
        index  index.html index.htm;
        
        location ~* \.(eot|ttf|woff|woff2)$ {
            add_header Access-Control-Allow-Origin *;
        }

        location / {
            proxy_pass http://space-frontend:80;
            proxy_set_header X-Forwarded-For $remote_addr;
        }

        location /api {
            proxy_pass http://space-api:3000;
            proxy_set_header X-Forwarded-For $remote_addr;
        }
    }
}

有人能指出我做错了什么吗?
我尝试过添加主机名,但它们与容器名称相同。

英文:

I know this question has been asked many times however, none of the solutions have worked for me.

I am trying to dockerize my angular app and node js backend using nginx.

What I have done is that I have created a docker-compose file.
It has three services and nginx.

1: space-frontend

2: space-api

3: mongodb

I am calling frontend and backend by their service name in nginx conf file like http://space-frontend:80 and http://space-api:3000 but I am getting a error in logs

> [emerg] 1#1: host not found in upstream "space-api" in /etc/nginx/nginx.conf:23

and frontend is working fine.
I am unable to understand where I am missing something.

For reference,
My frontend docker file

FROM node:16-alpine AS builder
WORKDIR /app
COPY . .
RUN npm i && npm run build --prod
FROM nginx:alpine
RUN mkdir /app
COPY --from=builder /app/dist/Space-Locator/browser /app
COPY nginx.conf /etc/nginx/nginx.conf

My frontend nginx conf

events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;

server {
listen       80;
server_name  localhost;
root /app;

location / {
    index  index.html;
    try_files $uri $uri/ /index.html;
}

error_page   500 502 503 504  /50x.html;
location = /50x.html {
    root   /usr/share/nginx/html;
}
}
}

Backend api docker file

FROM node:14-alpine as build-step
RUN mkdir -p /usr/app
WORKDIR /usr/app
COPY package.*json /usr/app/
RUN npm install
COPY . /usr/app/
EXPOSE 3000
CMD [ "npm", "start" ]

my docker-compose file

    version: "3.8"
services:
  reverse_proxy:
    image: nginx:1.17.10
    container_name: reverse_proxy
    depends_on:
      - space-frontend
      - space-api
      - database
    volumes:
      - ./reverse_proxy/nginx.conf:/etc/nginx/nginx.conf  
    ports:
      - 80:80
  space-frontend:
    container_name: space-frontend
    image: space-frontend
    build: 
      context: ./space-frontend
    ports:
      - 4000:80
  space-api:
    container_name: space-api
    hostname: space-api
    image: space-api
    build: 
      context: ./space-api
    ports:
      - 3000:3000
    links:
      - database  
    environment:
      MONGO_INITDB_DATABASE: spaceLocator 
      MONGODB_URI: mongodb://db:27017
    depends_on:
      - database
    volumes:
      - ./db-data/mongo/:/data/database
    networks:
      - node-network
  database:
    container_name: db
    image: mongo
    restart: on-failure
    ports:
      - 27017:27017
    volumes:
      - ./mongodb:/data/database
    networks:
      - node-network  
volumes:
  dbdata6:
networks:
  node-network:
    external: true
    driver: bridge  

and my nginx.conf file for reverse proxy

   events {
    worker_connections 1024;
}
http {

  server {
        listen 80;
        server_name  127.0.0.1;

        root   /usr/share/nginx/html;
        index  index.html index.htm;
        
        location ~* \.(eot|ttf|woff|woff2)$ {
        add_header Access-Control-Allow-Origin *;
        }

        location / {
          proxy_pass http://space-frontend:80;
          proxy_set_header X-Forwarded-For $remote_addr;
        }

        location /api {
            proxy_pass http://space-api:3000;
            proxy_set_header X-Forwarded-For $remote_addr;
        }
    }
}

Can somebody please point to the direction where I am doing wrong?
I have tried adding hostname but they are as same as container-name.

答案1

得分: 0

你需要告诉nginx使用Docker的DNS解析器,在nginx.conf中使用resolver关键字。类似这样:

load_module modules/ngx_http_js_module.so;

worker_processes 4;

events { worker_connections 1024; }

http {
    js_import index.js;

    resolver 127.0.0.11 ipv6=off;

不要以为你的工作完成了 - 在Kubernetes上,你要将127.0.0.11替换为kube-dns.kube-system.svc.cluster.local

英文:

You need to tell nginx to use dockers DNS resolver, using the resolver keyword in nginx.conf. Something like this:

load_module modules/ngx_http_js_module.so;

worker_processes 4;

events { worker_connections 1024; }

http {
    js_import index.js;

    resolver 127.0.0.11 ipv6=off;

Don't think that your work is done - on Kubernetes you replace 127.0.0.11 with kube-dns.kube-system.svc.cluster.local

答案2

得分: 0

问题是我们必须在相同的网络上运行每个容器。

networks:

  • node-network

我不得不在每个服务中定义这个。然后它就可以正常运行了。
谢谢所有帮助的人 主机在Nginx Docker Compose中未找到上游。

英文:

So guys problem was that we have to run every container on same network.

  networks:

  - node-network  

I had to define this in my every service. Then it ran without any problem.
Thank you for everyone who helped 主机在Nginx Docker Compose中未找到上游。

huangapple
  • 本文由 发表于 2023年6月2日 00:42:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76384047.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定