nginx日志文件报错: connect() failed (111: Connection refused) while connecting to upstream

https://github.com/0xbug/Hawkeye
没有直接放在docker里,而是在ubuntu 18.04中运行。
运行起来之后,发现请求总是出现502 Bad Gateway。
在这里插入图片描述
nginx日志文件报错:
connect() failed (111: Connection refused) while connecting to upstream
网上搜到一篇类似的问题:
http://corpus.hubwiz.com/2/node.js/29370360.html
而upstream为:http://127.0.0.1:8888
在这里插入图片描述
在Hawkeye的server目录中发现是在
deploy/supervisor/hawkeye.conf
文件中配置了的。
查看supervisord的日志发现,无法找到/usr/local/bin/gunicorn/usr/local/bin/huey_consumer.py等命令。
在这里插入图片描述
在这里插入图片描述
命令写死了gunicorn是在/usr/local/bin/gunicorn这个目录下,但是我的安装gunicorn是在/home/cqq/.local/bin/gunicorn
在这里插入图片描述
于是将这两个需要的可执行文件复制到其启动命令行对应的绝对路径(或者修改配置文件,替换启动命令行的可执行文件路径)。

sudo cp /home/cqq/.local/bin/gunicorn /usr/local/bin/gunicorn
sudo cp /home/cqq/.local/bin/huey_consumer.py    /usr/local/bin/huey_consumer.py

重新启动

然后梳理了一下整个程序的启动流程:

/usr/bin/python /usr/bin/supervisord -n -c /etc/supervisor/supervisord.conf

/usr/bin/supervisord是一个python进程管理工具,这里是和一个脚本文件。其后跟着的是supervisord的配置文件,看看配置文件里的内容:

; supervisor config file

[unix_http_server]
file=/var/run/supervisor.sock   ; (the path to the socket file)
chmod=0700                       ; sockef file mode (default 0700)

[supervisord]
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
childlogdir=/var/log/supervisor            ; ('AUTO' child log dir, default $TEMP)

; the below section must remain in the config file for RPC
; (supervisorctl/web interface) to work, additional interfaces may be
; added by defining them in separate rpcinterface: sections
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL  for a unix socket

; The [include] section can just contain the "files" setting.  This
; setting can list multiple files (separated by whitespace or
; newlines).  It can also contain wildcards.  The filenames are
; interpreted as relative to this file.  Included files *cannot*
; include files themselves.

[include]
files = /etc/supervisor/conf.d/*.conf

最后包含了所有/etc/supervisor/conf.d/*.conf文件。
进入/etc/supervisor/conf.d/目录看一下:

cqq@ubuntu:~/repos/Hawkeye$ ll /etc/supervisor/conf.d
total 24K
drwxr-xr-x 2 root root 4.0K Jul  4 20:08 ./
-rw-r--r-- 1 root root  164 Jul  4 20:08 hawkeye.conf
-rw-r--r-- 1 root root  154 Jul  4 20:08 huey.conf
-rw-r--r-- 1 root root  153 Jul  4 20:08 openresty.conf
-rw-r--r-- 1 root root  120 Jul  4 20:08 redis.conf
drwxr-xr-x 3 root root 4.0K Jul  4 20:08 ../

四个配置文件。
这里面的内容 就是各个进程启动的参数:

cqq@ubuntu:/etc/supervisor/conf.d$ cat huey.conf
[program:huey]
command=/usr/local/bin/huey_consumer.py task.huey -w 5
directory=/Hawkeye/server
startsecs=5
stopwaitsecs=0
autostart=true
autorestart=truecqq@ubuntu:/etc/supervisor/conf.d$ cat openresty.conf
[program:openresty]
command=/usr/local/openresty/bin/openresty -g "daemon off;"
directory=/tmp
startsecs=5
stopwaitsecs=0
autostart=true
autorestart=truecqq@ubuntu:/etc/supervisor/conf.d$ cat redis.conf
[program:redis]
command=/usr/bin/redis-server
directory=/tmp
startsecs=10
stopwaitsecs=0
autostart=true
autorestart=truecqq@ubuntu:/etc/supervisor/conf.d$ cat hawkeye.conf
[program:hawkeye]
command=/usr/local/bin/gunicorn -w10 -b127.0.0.1:8888 api:app
directory=/Hawkeye/server
startsecs=5
stopwaitsecs=0
autostart=true
autorestart=true

由于进程加载进了内存,需要找到supervisord进程kill掉,然后重启,重新加载我们的新的可执行文件。

cqq@ubuntu:~/repos/Hawkeye$ ps aux|grep supervisord
root        881  0.0  0.5  67148 21848 ?        Ss   15:51   0:03 /usr/bin/python /usr/bin/supervisord -n -c /etc/supervisor/supervisord.conf
cqq        9217  0.0  0.0  16148  2624 pts/0    S+   20:21   0:00 grep --color=auto supervisord

我的机器上默认的python(/usr/bin/python )是python2.
由于项目写的是基于Python3,于是修改一下命令行启动参数。

/usr/bin/python3 /usr/bin/supervisord -n -c /etc/supervisor/supervisord.conf

然而使用python3的时候,发现supervisord并不支持python3:
在这里插入图片描述

使用Python3启动gunicorn,绑定在本地8888端口。其中api对应的是当前目录的api.py文件。
在这里插入图片描述
然后前端发起请求,/api/trend、/api/setting/blacklist
这次没有出现502,而是500。
在这里插入图片描述

启动之后又发现:

pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused

发现并没有安装mongodb,于是安装一下:

sudo apt install -y mongodb

参考:
https://www.digitalocean.com/community/tutorials/how-to-install-mongodb-on-ubuntu-18-04
安装成功之后,确认一下是否安装成功:

sudo systemctl status mongodb

再试了一下,终于没有报错了。
在这里插入图片描述

不能每次都手动用python3启动/usr/local/bin/gunicorn,可以直接在这个脚本文件里修改:
将第一行的 /usr/bin/python改成/usr/bin/python3。
参考:https://www.yduba.com/biancheng-6272006340.html

发现依然不能爬取数据,猜测原因是系统自启动了一个redis,而且是开机自启动的。于Hawkeye的redis冲突了
通过定位系统的redis,禁用掉自启动。
/etc/redis/redis.conf

daemonize no

开机只启动mongodb的端口,

tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN      893/mongod

不开启nginx:80,redis:6379等端口。
这样才是正常的吧,
在这里插入图片描述
之前的启动都是由于开启了其他比如redis,比如nginx服务,导致这些服务没有被启动。
终于能成功爬到数据了:
在这里插入图片描述

之前启动的命令行参数都是:
/usr/local/openresty/nginx/sbin/nginx -g daemon on; master_process on;
现在是
/usr/local/openresty/bin/openresty -g daemon off;
不过需要统一的命令:

sudo /usr/bin/python /usr/bin/supervisord -n -c /etc/supervisor/supervisord.conf

删除掉query之后,发现依然有结果,然后删除掉

db.result.drop()

这个collection之后,终于没有了,原来数据都是来自于mongo,还有部分是redis?这两个如何配合,现在还没搞懂。
在这里插入图片描述
参考:https://www.yiibai.com/mongodb/mongodb_drop_collection.html

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值