V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
canadahetian
V2EX  ›  Linux

几度崩溃, docker 下配置的 nginx 无论如何也连不上 gunicorn, 希望有经验的朋友指点以下,刚接触 docker

  •  
  •   canadahetian · 2020-04-08 23:11:46 +08:00 · 3426 次点击
    这是一个创建于 1717 天前的主题,其中的信息可能已经有所发展或是发生改变。
    先贴一下 nginx 错误日志
    nginx error.log 的错误日志
    2020/04/08 15:00:15 [error] 6#6: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 82.223.16.212, server: www.cakevin.com, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8000/", host: "52.60.58.127"
    2020/04/08 15:03:20 [error] 6#6: *3 connect() failed (111: Connection refused) while connecting to upstream, client: 99.254.94.66, server: www.cakevin.com, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8000/", host: "cakevin.com"
    2020/04/08 15:03:21 [error] 6#6: *3 connect() failed (111: Connection refused) while connecting to upstream, client: 99.254.94.66, server: www.cakevin.com, request: "GET /favicon.ico HTTP/1.1", upstream: "http://127.0.0.1:8000/favicon.ico", host: "cakevin.com", referrer: "http://cakevin.com/"


    目前用 IP:8000 端口可以访问,登录,就是没有样式,静态文件没有被找到,
    cakevin.com:80 访问报 502
    用 ip:80 无法访问此网站 拒绝了我们的连接请求。
    Django setting 测试设置的 ALLOWED_HOSTS = ['*'] 应该不是这个问题


    容器信息
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    8ccc02234bc5 cakevin_nginx "nginx -g 'daemon of…" 10 seconds ago Up 7 seconds 0.0.0.0:80->80/tcp cakevin_nginx_1
    22b222635f17 cakevin:1.1 "bash start.sh" 12 seconds ago Up 10 seconds 0.0.0.0:8000->8000/tcp cakevin_cakevin_1
    d95336fabb61 mysql:5 "docker-entrypoint.s…" 13 seconds ago Up 11 seconds 0.0.0.0:3306->3306/tcp, 33060/tcp cakevin_db_1

    我的 docker-compose.yml 配置如下:
    version: "3"

    services:

    db:
    image: mysql:5
    environment:
    - MYSQL_HOST=localhost
    - MYSQL_DATABASE=cakevin
    - MYSQL_ROOT_PASSWORD=111111
    ports:
    - 3306:3306
    restart: always

    cakevin:
    image: cakevin:1.1
    ports:
    - "8000:8000"
    volumes:
    - /home/cakevin:/cakevin
    links:
    - db
    depends_on:
    - db
    command: bash start.sh
    restart: always


    nginx:
    build:
    context: .
    dockerfile: dockerfile_nginx
    ports:
    - "80:80"
    volumes:
    - /home/cakevin/static:/usr/share/nginx/html/static
    - /home/nginx/logs:/var/log/nginx
    - /home/nginx/conf.d:/etc/nginx/conf.d
    links:
    - cakevin
    depends_on:
    - cakevin
    restart: always


    dockerfile_nginx 的配置如下
    FROM nginx
    EXPOSE 80
    RUN apt-get upgrade && apt-get update && apt-get -y install vim
    RUN mkdir -p /usr/share/nginx/html/static


    nginx 的 conf.d 里的配置文件是
    server {
    listen 80;
    server_name cakevin.com;

    location /static {
    alias /usr/share/nginx/html/static;
    }

    location / {
    proxy_set_header Host $host;
    proxy_pass http://0.0.0.0:8000;
    proxy_redirect off;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    }

    请大家帮忙给看一下,刚接触 docker,有点晕,已经搜索了 1 天了,国内外全部找遍了,没有找到解决方法
    10 条回复    2020-04-10 16:48:07 +08:00
    momocraft
        1
    momocraft  
       2020-04-08 23:25:10 +08:00   ❤️ 1
    proxy_pass 是 localhost 是不是不对? 不应该是 cakevin 什么吗?
    canadahetian
        2
    canadahetian  
    OP
       2020-04-09 04:01:07 +08:00
    @momocraft
    感谢,我把
    proxy_pass http://0.0.0.0:8000;
    改为
    proxy_pass http://cakevin:8000;
    就搞定 le ,感谢,感谢!!!!!!!
    ericgui
        3
    ericgui  
       2020-04-09 07:54:03 +08:00
    @canadahetian 为了防止你连不上数据库,你连数据库的时候不要写 localhost:3306 了,应该是 db:3306

    到底是一样的

    你可以理解为 docker 给这些 service 分配了“域名”,他们就是你写在 services 下的每个 service 的名字
    ericgui
        4
    ericgui  
       2020-04-09 07:54:22 +08:00
    “道理是一样”,写错字,typo,sorry
    image72
        5
    image72  
       2020-04-09 08:18:34 +08:00
    设置这些应用都使用同一个 network 肯定没问题
    nieqibest
        6
    nieqibest  
       2020-04-09 09:03:55 +08:00 via Android
    为啥写 0.0.0.0
    canadahetian
        7
    canadahetian  
    OP
       2020-04-09 12:03:19 +08:00 via iPhone
    @ericgui 太谢谢谢了,真的是感谢,我发现这个平台的人真好。祝一生幸福
    canadahetian
        8
    canadahetian  
    OP
       2020-04-09 12:04:22 +08:00 via iPhone
    @image72 感谢感谢,已经解决
    canadahetian
        9
    canadahetian  
    OP
       2020-04-09 12:04:52 +08:00 via iPhone
    @nieqibest 已经改了,感谢
    Sunbridger
        10
    Sunbridger  
       2020-04-10 16:48:07 +08:00
    1
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2719 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 05:42 · PVG 13:42 · LAX 21:42 · JFK 00:42
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.