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

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

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文件

  1. FROM node:16-alpine AS builder
  2. WORKDIR /app
  3. COPY . .
  4. RUN npm i && npm run build --prod
  5. FROM nginx:alpine
  6. RUN mkdir /app
  7. COPY --from=builder /app/dist/Space-Locator/browser /app
  8. COPY nginx.conf /etc/nginx/nginx.conf

我的前端nginx配置

  1. events {
  2. worker_connections 1024;
  3. }
  4. http {
  5. include /etc/nginx/mime.types;
  6. server {
  7. listen 80;
  8. server_name localhost;
  9. root /app;
  10. location / {
  11. index index.html;
  12. try_files $uri $uri/ /index.html;
  13. }
  14. error_page 500 502 503 504 /50x.html;
  15. location = /50x.html {
  16. root /usr/share/nginx/html;
  17. }
  18. }
  19. }

后端API Docker文件

  1. FROM node:14-alpine as build-step
  2. RUN mkdir -p /usr/app
  3. WORKDIR /usr/app
  4. COPY package.*json /usr/app/
  5. RUN npm install
  6. COPY . /usr/app/
  7. EXPOSE 3000
  8. CMD [ "npm", "start" ]

我的docker-compose文件

  1. version: "3.8"
  2. services:
  3. reverse_proxy:
  4. image: nginx:1.17.10
  5. container_name: reverse_proxy
  6. depends_on:
  7. - space-frontend
  8. - space-api
  9. - database
  10. volumes:
  11. - ./reverse_proxy/nginx.conf:/etc/nginx/nginx.conf
  12. ports:
  13. - 80:80
  14. space-frontend:
  15. container_name: space-frontend
  16. image: space-frontend
  17. build:
  18. context: ./space-frontend
  19. ports:
  20. - 4000:80
  21. space-api:
  22. container_name: space-api
  23. hostname: space-api
  24. image: space-api
  25. build:
  26. context: ./space-api
  27. ports:
  28. - 3000:3000
  29. links:
  30. - database
  31. environment:
  32. MONGO_INITDB_DATABASE: spaceLocator
  33. MONGODB_URI: mongodb://db:27017
  34. depends_on:
  35. - database
  36. volumes:
  37. - ./db-data/mongo/:/data/database
  38. networks:
  39. - node-network
  40. database:
  41. container_name: db
  42. image: mongo
  43. restart: on-failure
  44. ports:
  45. - 27017:27017
  46. volumes:
  47. - ./mongodb:/data/database
  48. networks:
  49. - node-network
  50. volumes:
  51. dbdata6:
  52. networks:
  53. node-network:
  54. external: true
  55. driver: bridge

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

  1. events {
  2. worker_connections 1024;
  3. }
  4. http {
  5. server {
  6. listen 80;
  7. server_name 127.0.0.1;
  8. root /usr/share/nginx/html;
  9. index index.html index.htm;
  10. location ~* \.(eot|ttf|woff|woff2)$ {
  11. add_header Access-Control-Allow-Origin *;
  12. }
  13. location / {
  14. proxy_pass http://space-frontend:80;
  15. proxy_set_header X-Forwarded-For $remote_addr;
  16. }
  17. location /api {
  18. proxy_pass http://space-api:3000;
  19. proxy_set_header X-Forwarded-For $remote_addr;
  20. }
  21. }
  22. }

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

英文:

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

  1. FROM node:16-alpine AS builder
  2. WORKDIR /app
  3. COPY . .
  4. RUN npm i && npm run build --prod
  5. FROM nginx:alpine
  6. RUN mkdir /app
  7. COPY --from=builder /app/dist/Space-Locator/browser /app
  8. COPY nginx.conf /etc/nginx/nginx.conf

My frontend nginx conf

  1. events {
  2. worker_connections 1024;
  3. }
  4. http {
  5. include /etc/nginx/mime.types;
  6. server {
  7. listen 80;
  8. server_name localhost;
  9. root /app;
  10. location / {
  11. index index.html;
  12. try_files $uri $uri/ /index.html;
  13. }
  14. error_page 500 502 503 504 /50x.html;
  15. location = /50x.html {
  16. root /usr/share/nginx/html;
  17. }
  18. }
  19. }

Backend api docker file

  1. FROM node:14-alpine as build-step
  2. RUN mkdir -p /usr/app
  3. WORKDIR /usr/app
  4. COPY package.*json /usr/app/
  5. RUN npm install
  6. COPY . /usr/app/
  7. EXPOSE 3000
  8. CMD [ "npm", "start" ]

my docker-compose file

  1. version: "3.8"
  2. services:
  3. reverse_proxy:
  4. image: nginx:1.17.10
  5. container_name: reverse_proxy
  6. depends_on:
  7. - space-frontend
  8. - space-api
  9. - database
  10. volumes:
  11. - ./reverse_proxy/nginx.conf:/etc/nginx/nginx.conf
  12. ports:
  13. - 80:80
  14. space-frontend:
  15. container_name: space-frontend
  16. image: space-frontend
  17. build:
  18. context: ./space-frontend
  19. ports:
  20. - 4000:80
  21. space-api:
  22. container_name: space-api
  23. hostname: space-api
  24. image: space-api
  25. build:
  26. context: ./space-api
  27. ports:
  28. - 3000:3000
  29. links:
  30. - database
  31. environment:
  32. MONGO_INITDB_DATABASE: spaceLocator
  33. MONGODB_URI: mongodb://db:27017
  34. depends_on:
  35. - database
  36. volumes:
  37. - ./db-data/mongo/:/data/database
  38. networks:
  39. - node-network
  40. database:
  41. container_name: db
  42. image: mongo
  43. restart: on-failure
  44. ports:
  45. - 27017:27017
  46. volumes:
  47. - ./mongodb:/data/database
  48. networks:
  49. - node-network
  50. volumes:
  51. dbdata6:
  52. networks:
  53. node-network:
  54. external: true
  55. driver: bridge

and my nginx.conf file for reverse proxy

  1. events {
  2. worker_connections 1024;
  3. }
  4. http {
  5. server {
  6. listen 80;
  7. server_name 127.0.0.1;
  8. root /usr/share/nginx/html;
  9. index index.html index.htm;
  10. location ~* \.(eot|ttf|woff|woff2)$ {
  11. add_header Access-Control-Allow-Origin *;
  12. }
  13. location / {
  14. proxy_pass http://space-frontend:80;
  15. proxy_set_header X-Forwarded-For $remote_addr;
  16. }
  17. location /api {
  18. proxy_pass http://space-api:3000;
  19. proxy_set_header X-Forwarded-For $remote_addr;
  20. }
  21. }
  22. }

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关键字。类似这样:

  1. load_module modules/ngx_http_js_module.so;
  2. worker_processes 4;
  3. events { worker_connections 1024; }
  4. http {
  5. js_import index.js;
  6. 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:

  1. load_module modules/ngx_http_js_module.so;
  2. worker_processes 4;
  3. events { worker_connections 1024; }
  4. http {
  5. js_import index.js;
  6. 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.

  1. networks:
  2. - 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:

确定