Nginx 连接被拒绝错误 111,网关错误

Nginx 连接被拒绝错误 111,网关错误

这是我的工作目录:

/var/www/flaskapp
  • 我的项目文件
  • 我的项目.py
  • 我的项目.sock
  • pycache
  • 韦恩
  • wsgi.py

我的项目.py

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "<h1 style='color:blue'>Hello There!</h1>"

if __name__ == "__main__":
    app.run(host='0.0.0.0')

wsgi.py

from myproject import app

if __name__ == "__main__":
    app.run()

我的项目文件

[uwsgi]
module = wsgi:app

master = true
processes = 5

socket = myproject.sock
chmod-socket = 660
vacuum = true

die-on-term = true
#location of log files
logto = /var/log/uwsgi/%n.log

我还创建了一个 systemd Unit 文件

/etc/systemd/system/myproject.service

[Unit]
Description=uWSGI instance serve myproject
After=network.target

[Service]
User = john
Group = www-data
WorkingDirectory=/var/www/flaskapp/
Environment="PATH=/var/www/flaskapp/venv/bin"
ExecStart=/var/www/flaskapp/venv/bin/uwsgi --ini myproject.ini

[Install]
WantedBy=multi-user.target

进而

sudo systemctl start myproject
sudo systemctl enable myproject

然后配置 nginx。nginx.conf 我保持不变,但对 /etc/nginx/sites-availible/myproject 进行了更正

server {
    listen 83;
    server_name my_external_ip;

    location / {
        include uwsgi_params;
        uwsgi_pass unix:/var/www/flaskapp/venv;
    }
}

并通过命令创建了到 /etc/nginx/sites-enabled 的链接

sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled

之后:sudo systemctl restart nginx 和 sudo ufw 允许“Nginx Full”。

现在当我进入http://我的 IP 地址:83/我看到的是默认的 nginx 问候语,没有其他内容!不过我认为这应该是我的 python 脚本编写的 Hello There!不知道为什么...

这是我的日志:

/var/log/nginx/error.log
2018/02/08 22:17:51 [error] 2394#2394: *1 connect() to unix:/var/www/flaskapp/venv failed (111: Connection refused) while connecting to upstream, client: 46.146.0.30, server: 92.240.202.184, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:/var/www/flaskapp/venv:", host: "my_external_ip:83"

以及uwsgi的日志:

current working directory: /var/www/flaskapp
detected binary path: /var/www/flaskapp/venv/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
your processes number limit is 7157
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to UNIX address myproject.sock fd 3
Python version: 3.5.2 (default, Nov 23 2017, 16:37:01)  [GCC 5.4.0 20160609]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0xf07550
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 436560 bytes (426 KB) for 5 cores
*** Operational MODE: preforking ***
WSGI app 0 (mountpoint='') ready in 2 seconds on interpreter 0xf07550 pid: 2320 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 2320)
spawned uWSGI worker 1 (pid: 2703, cores: 1)
spawned uWSGI worker 2 (pid: 2704, cores: 1)
spawned uWSGI worker 3 (pid: 2705, cores: 1)
spawned uWSGI worker 4 (pid: 2706, cores: 1)
spawned uWSGI worker 5 (pid: 2707, cores: 1)

有人说我需要重新安装 uwsgi 并安装 pcre,但这样做之后仍然没有成功,我真的尝试并搜索了与此相关的所有解决方案,但尽了所有努力我还是一无所获。

请帮助我检测问题并处理它。

答案1

当使用 UNIX 套接字时,指定的路径uwsgi_pass必须是实际 UWSGI 套接字的路径。

那是:

[uwsgi]
socket = /path/to/uwsgi.sock

在 nginx 中必须匹配:

uwsgi_pass unix:/path/to/uwsgi.sock

相关内容