V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
yuting0501
V2EX  ›  Python

Flask + Nginx + Docker-compose 遇到 502 Bad Gateway 问题

  •  
  •   yuting0501 · 2019-04-21 22:40:15 +08:00 · 5382 次点击
    这是一个创建于 2035 天前的主题,其中的信息可能已经有所发展或是发生改变。

    请教一下,我用 docker-compose 部署 Flask + Nginx + Docker-compose + gunicorn 应用,本地访问 0.0.0.0:5000 可以访问,访问 0.0.0.0:80 显示 502 bad gate,但 0.0.0.0:80/static/… 静态文件能成功访问,不知道是配置文件哪个地方配错,有没有什么建议?非常感谢

    docker-compose.yml

    version: '3'
    
    services:
      webapp:
        build: .
        # command: bash ./start_server.sh
        # env_file: .env
        volumes:
          - ./webapp:/iodock/
        ports:
          - "5000:5000"  # web 容器内的 5000 端口映射到主机的 5000 端口
        depends_on:
          - nginx
    
      nginx:
        restart: always
        image: nginx:stable
        volumes:
          - ./webconf/nginx/conf.d/:/etc/nginx/conf.d/
          - ./webconf/nginx/log/:/var/log/nginx/
          - ./webconf/nginx/cert/:/opt/cert/
          - ./webapp/app/static/:/opt/static/
        ports:
          - "80:80"
          - "443:443"
    

    Dockerfile

    # --------------------------
    # Docker file
    # --------------------------
    FROM python:3.6
    
    # Set system language
    ENV LC_ALL C.UTF-8
    ENV LANG C.UTF-8
    
    # install python requirement
    COPY ./webapp/requirements.txt /tmp/requirements.txt
    RUN pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple --upgrade pip
    RUN pip3 install --no-cache-dir gunicorn
    RUN pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple -r /tmp/requirements.txt
    
    # Create root folder docker_web_app
    COPY ./webapp /docker_web_app/iodock/
    WORKDIR /iodock/
    
    # ENV PORT 5000
    EXPOSE 5000
    
    CMD ["/usr/local/bin/gunicorn", "-w", "2", "-b", ":5000", "manage:app"]
    

    nginx.conf

    server {
        listen  80;
        server_name     localhost;                           # 公网地址
        access_log      /var/log/nginx/nginx_access.log;
        error_log       /var/log/nginx/nginx_error.log;
    
        location / {
            proxy_pass         http://localhost:5000;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    
        location /static {
            alias /opt/static;
            proxy_set_header Host $host;
            # proxy_cache mycache;
            # expires 30d;
        }
    }
    
    

    nginx_error.log

    2019/04/21 14:36:18 [error] 7#7: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.18.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:5000/", host: "0.0.0.0"
    2019/04/21 14:36:18 [error] 7#7: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.18.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:5000/", host: "0.0.0.0"
    2019/04/21 14:36:19 [error] 7#7: *1 no live upstreams while connecting to upstream, client: 172.18.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", upstream: "http://localhost/favicon.ico", host: "0.0.0.0", referrer: "http://0.0.0.0/"
    
    
    11 条回复    2019-05-07 09:57:23 +08:00
    jerry
        1
    jerry  
       2019-04-21 22:44:53 +08:00   ❤️ 1
    nginx 的 localhost:5000 改成 webapp:5000
    yuting0501
        2
    yuting0501  
    OP
       2019-04-21 22:53:13 +08:00
    @jerry 非常感谢!成功了,原来是要指明容器对象
    Kilerd
        3
    Kilerd  
       2019-04-21 23:11:00 +08:00   ❤️ 1
    顺便把 flask 里面的

    - "5000:5000" # web 容器内的 5000 端口映射到主机的 5000 端口

    这一样删了吧,都通过 nginx 访问了, 就没有必要暴露出原始端口了
    yuting0501
        4
    yuting0501  
    OP
       2019-04-21 23:21:16 +08:00
    @Kilerd 有道理,我试试看,谢谢!
    wangy
        5
    wangy  
       2019-04-21 23:22:04 +08:00
    支持一下!
    honglongmen
        6
    honglongmen  
       2019-04-21 23:57:34 +08:00
    @yuting0501
    @Kilerd 正好顺便请教下,我用 portainer 部署了个 gitlab-ce,通过 cdn 访问总是间隔性有 422 出错提示:

    下面是 nginx 的的配置,是否 proxy_pass http://127.0.0.1:8880; 这个我也要改为 docker 的容器对象名才可?

    location /
    {
    location ~ .*\.(php|jsp|cgi|asp|aspx|flv|swf|xml)?$
    {
    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 REMOTE-HOST $remote_addr;
    proxy_pass http://127.0.0.1:8880;

    }
    proxy_pass http://127.0.0.1:8880;
    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 REMOTE-HOST $remote_addr;


    以下为 docker ps:

    [root@back ~]# docker ps
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    ed82dc089c7e gitlab/gitlab-ce:latest "/assets/wrapper" 3 weeks ago Up 9 days (healthy) 0.0.0.0:8222->22/tcp, 0.0.0.0:8880->80/tcp, 0.0.0.0:8443->443/tcp Gitlab-CE

    807229f4387e portainer/portainer "/portainer" 3 weeks ago Up 9 days 0.0.0.0:9000->9000/tcp
    yuting0501
        7
    yuting0501  
    OP
       2019-04-22 17:59:32 +08:00
    @honglongmen 你可以试试看,但按我的了解,如果是配置错了应该是必定不工作
    honglongmen
        8
    honglongmen  
       2019-04-22 18:25:07 +08:00
    @yuting0501 容器对象我是写 CONTAINER ID:ed82dc089c7e 好还是 NAMES:Gitlab-CE 好呢
    yuting0501
        9
    yuting0501  
    OP
       2019-04-22 22:07:06 +08:00
    @honglongmen 囧,不太懂,其实我是搞嵌入式单片机的。。。
    yuting0501
        10
    yuting0501  
    OP
       2019-05-06 22:59:49 +08:00
    更新一下贴子,修改为`webapp:5000`不是正确的解法,nginx 报错,猜测是因为 nginx 无法识别 webapp 所以变成类似`:5000`? 所以我试了一下`0.0.0.0:5000`,可以成功运行。之前一直纳闷为什么 nginx 可以识别 docker-compose.yml 中的 webapp 参数,现在似乎说得通了。
    yuting0501
        11
    yuting0501  
    OP
       2019-05-07 09:57:23 +08:00
    抱歉,上一条回复是错误的,虽然 nginx 会报警告,:
    nginx: [emerg] host not found in upstream "webapp" in /etc/nginx/conf.d/nginx.conf:10

    对 docker-compose network 配置没有深入了解,这里得指定 docker-compose 网络名,具体细节请参考:
    https://docs.docker.com/compose/networking/
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2692 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 11:45 · PVG 19:45 · LAX 03:45 · JFK 06:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.