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

2020-04-08 23:11:46 +08:00
 canadahetian
先贴一下 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 天了,国内外全部找遍了,没有找到解决方法
3428 次点击
所在节点    Linux
10 条回复
momocraft
2020-04-08 23:25:10 +08:00
proxy_pass 是 localhost 是不是不对? 不应该是 cakevin 什么吗?
canadahetian
2020-04-09 04:01:07 +08:00
@momocraft
感谢,我把
proxy_pass http://0.0.0.0:8000;
改为
proxy_pass http://cakevin:8000;
就搞定 le ,感谢,感谢!!!!!!!
ericgui
2020-04-09 07:54:03 +08:00
@canadahetian 为了防止你连不上数据库,你连数据库的时候不要写 localhost:3306 了,应该是 db:3306

到底是一样的

你可以理解为 docker 给这些 service 分配了“域名”,他们就是你写在 services 下的每个 service 的名字
ericgui
2020-04-09 07:54:22 +08:00
“道理是一样”,写错字,typo,sorry
image72
2020-04-09 08:18:34 +08:00
设置这些应用都使用同一个 network 肯定没问题
nieqibest
2020-04-09 09:03:55 +08:00
为啥写 0.0.0.0
canadahetian
2020-04-09 12:03:19 +08:00
@ericgui 太谢谢谢了,真的是感谢,我发现这个平台的人真好。祝一生幸福
canadahetian
2020-04-09 12:04:22 +08:00
@image72 感谢感谢,已经解决
canadahetian
2020-04-09 12:04:52 +08:00
@nieqibest 已经改了,感谢
Sunbridger
2020-04-10 16:48:07 +08:00
1

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/660618

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX