connect() failed (111: Connection refused) while connecting to upstream

报错

Nginx代理Vue项目,访问基于Flask搭建的后台时,一直报出访问路径错误。查看Nginx错误日志,发现报错是connect() failed (111: Connection refused) while connecting to upstream

$ cat /var/log/nginx/error.log

2022/04/26 17:46:26 [error] 1593845#1593845: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.110.245, server: , request: "POST /api/user/login/ HTTP/1.1", upstream: "http://127.0.0.1:5000//user/login/", host: "192.168.110.228", referrer: "http://192.168.110.228/"

原因

原因是在安装flask的时候,使用时是apt安装的,而不是pip3进行安装的。

$ sudo apt install python3-flask 

$ flask run -h 0.0.0.0 -p 5000 

解决方案

卸载掉python3-flask

$ sudo apt remove python3-flask

使用pip3安装flask

$ pip3 install flask
  
  WARNING: The script flask is installed in '/data2/jing.w/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
  

# 如果安装flask之后, 路径不在Path中, 使用软链接
$ sudo ln -s /data2/jing.w/.local/bin/flask /usr/bin/flask
Logo

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。

更多推荐