nginx主机未在上游后端找到- docker

guicsvcw  于 2023-04-29  发布在  Nginx
关注(0)|答案(1)|浏览(130)

我正在尝试用nginx、前端和后端创建docker容器。前端是react,后端是django。下面是docker-compose文件

version: '3'

services:
  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile.prod
    volumes:
      - frontend-data:/usr/share/nginx/html

  backend:
    build:
      context: .
      dockerfile: Dockerfile.prod
    environment:
      - DEBUG=0
      - ALLOWED_HOSTS=nginx,localhost,127.0.0.1
      - SECRET_KEY=django-Super-secret-key
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    volumes:
      - static_data:/vol/web
    depends_on:
      - db
    networks:
      - app-networks
    

  db:
    image: postgres:latest
    restart: always
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    volumes:
      - db-data:/var/lib/postgresql/data

  nginx:
    build: 
      context: ./nginx
      dockerfile: Dockerfile.prod
    ports:
      - "80:80"
    volumes:
      - frontend-data:/usr/share/nginx/html/frontend
      - static_data:/vol/static
    depends_on:
      - frontend
      - backend
    networks:
      - app-networks

networks:
  app-networks:
    driver: bridge

volumes:
  db-data:
  frontend-data:
  static_data:

在前端Dockerfile中,我已经将构建文件移动到卷并将其挂载在nginx中。我正面临着一个错误

[emerg] 1#1: host not found in upstream "backend:8000" in /etc/nginx/nginx.conf:11
nginx: [emerg] host not found in upstream "backend:8000" in /etc/nginx/nginx.conf:11

我的nginx在哪里。conf文件是

worker_processes 1;

events { worker_connections 1024; }

http {

    sendfile on;
    large_client_header_buffers 4 32k;

    upstream backend_servers {
        server backend:8000;
    }

    server {
        listen 80;
        server_name mysite.com;

        location / {
            return 301 https://$host$request_uri;
        }
    }

    server {
        listen 443 ssl;
        server_name mysite.com;

        ssl_certificate /etc/ssl/certs/my-site.crt;
        ssl_certificate_key /etc/ssl/private/my-site.key;

        # Static files
    location /static/ {
        alias /vol/static/;
    }

    location / {
        root /usr/share/nginx/html/frontend;
        try_files $uri $uri/ /index.html;
    }

    # Reverse proxy requests to the uWSGI server
    location /api {
        rewrite ^/api(.*) $1 break;
        uwsgi_pass backend_servers;
        include /etc/nginx/uwsgi_params;
        proxy_redirect     off;
        proxy_http_version 1.1;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection keep-alive;
        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-Proto $scheme;
        proxy_set_header   X-Forwarded-Host $server_name;
        proxy_buffer_size           128k;
        proxy_buffers               4 256k;
        proxy_busy_buffers_size     256k;
    }

    # Proxy pass requests for /admin to the Django admin server
    location /admin {
        uwsgi_pass backend_servers;
        include /etc/nginx/uwsgi_params;
        proxy_redirect     off;
        proxy_redirect     off;
        proxy_http_version 1.1;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection keep-alive;
        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-Proto $scheme;
        proxy_set_header   X-Forwarded-Host $server_name;
        proxy_buffer_size           128k;
        proxy_buffers               4 256k;
        proxy_busy_buffers_size     256k;
    }

    client_max_body_size 3M;

    }
}

nginx.conf文件位于/etc/nginx/nginx.conf

vq8itlhq

vq8itlhq1#

我怀疑问题是nginx容器启动太快,在后端服务准备好服务之前。
depends_on参数仅创建依赖关系图,但不会等待服务就绪。
你应该看看这个:https://docs.docker.com/compose/startup-order/
举个例子:https://stackoverflow.com/a/64921431/12530613

相关问题