Docker nginx:在上游“后端”中找不到[emerg]主机

时间:2019-11-04 05:38:51

标签: nginx flask docker-compose

我正在尝试使用结合docker-composelinuxserver/letsencrypt容器的uWSGI来建立Flask服务器。我已经将下面的docker-compose.yml文件与另一个nginx服务器一起使用了。问题在于nginx容器找不到我的backend烧瓶容器,因此无法对其执行uwsgi_pass

docker-compose.yml

version: '3.1'
services:

    nginx:
      image: linuxserver/letsencrypt
      ports:
        - "80:80"
        - "443:443"
      volumes:
        - ./data/nginx/nginx.conf:/config/nginx/site-confs/default
        - ./data/nginx/uwsgi_params:/config/nginx/uwsgi_params
      depends_on:
        - backend
      networks:
        - backend
      container_name: nginx
      environment:
        - PUID=1000
        - PGID=1000
        - (+ other settings)


    backend:
        build: ./backend
        container_name: flask
        restart: always
        links:
            - db
        volumes:
            - ./backend:/app
            - ./managed:/managed
        expose: 
            - 8080

    db:
        image: mysql:5.7
        container_name: db
        restart: always
        ports:
            - "3306:3306"


volumes:
  flask-db:

networks:
  backend:

我尝试使用linksdepends_onnetworks连接它们,但似乎无济于事。

nginx.conf

server {
    listen 80;
    server_name api.example.com;
    server_tokens off;

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

server {
    listen 443 ssl;
    server_name api.example.com;
    server_tokens off;

    location / {
        include uwsgi_params;
        uwsgi_pass backend:8080;
        proxy_set_header    Host                $http_host;
        proxy_set_header    X-Real-IP           $remote_addr;
        proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;


        if ($request_method = 'POST') {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
            add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
        }
        if ($request_method = 'GET') {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
            add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
        }
    }
}

server {
    listen 80;
    server_name example.com;
    server_tokens off;

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

server {
    listen 443 ssl;
    server_name example.com;
    server_tokens off;

    location / {
        include uwsgi_params;
        uwsgi_pass backend:8080;
        proxy_set_header    Host                $http_host;
        proxy_set_header    X-Real-IP           $remote_addr;
        proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;
    }
}

nginx容器中的错误是:

nginx: [emerg] host not found in upstream "backend" in /config/nginx/site-confs/default:18

我还应该提到./data/nginx/uwsgi_params卷只是uwsgi命令的列表

如何使backend容器可访问nginx容器?

1 个答案:

答案 0 :(得分:0)

您需要在depends_on文件中设置docker-compose属性,例如:

nginx:
  depends_on:
    - backend