NGINX + UWSGI + FLASK + SOCKETIO 实现websocket服务器

胥承
2023-12-01

内容如题

软件环境如下:

    系统版本:  Ubuntu 16.04 Server版

    python版本: 3.5.2 (Ubuntu自带)

    nginx 下载  apt install nginx

    uwsgi 下载  pip install uwsgi

   程序根目录  /home/uma/peach_v3.1.1/peach2Produce/

配置方法:

uwsgi配置与nginx 配置参考 https://flask-socketio.readthedocs.io/en/latest/  中 Using nginx as a WebSocket Reverse Proxy

的部分。

nginx配置如下:

server {
    listen  80;
    server_name localhost;


    location ^~ /static/ {
        root /home/uma/peach_v3.1.1/peach2Produce;
    }

    location / {
        include proxy_params;
        proxy_pass http://127.0.0.1:6000;
    }
    location /socket.io {
        include uwsgi_params;

        proxy_http_version 1.1;
        proxy_buffering off;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_pass http://127.0.0.1:6000/socket.io;
     }

    location ~* .(woff|eot|ttf|svg|mp4|webm|jpg|jpeg|png|gif|ico|css|js)$ {
        expires 30d;
    }
}

UWSGI 启动命令

uwsgi --http :6000 --gevent 1000 --http-websockets --master --wsgi-file wsgi.py --callable application

 wsgi.py 内容

# -*- coding: utf-8 -*-

#连接服务器与flask的中间层 由服务器启动程序

import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,'/home/uma/peach_v3.1.1/peach2Produce/')

import Start
from app import application

启动完成,大功告成

Oh nooooooop

我的版本显示

File "/usr/local/lib/python3.5/dist-packages/engineio/async_eventlet.py", line 15, in __call__
    raise RuntimeError('You need to use the eventlet server.

网上搜了一下,详见 https://cloud.tencent.com/developer/article/1348020

说是,最新的flask_socketio 使用的是python-socketio,如果在包中安装了gevent或evenlet,在服务器上使用时,就会报错

我还在解决

 

+++++++++++++++++++12月10号更新++++++++++++++

使用uwsgi 启动时,gevent和eventlet 兼容的问题,我电脑如果将 evenlet 和 gevent 卸载

pip uninstall eventlet 

pip unintall  gevent

然后再通过  uwsgi 启动,启动成功,但是页面会经常卡死

 

发现可能是python版本问题,将python升级为3.6.7

再次安装eventlet 后继续

uwsgi --http :6000 --gevent 1000 --http-websockets --master --wsgi-file wsgi.py --callable application

页面正常,但开启一段时间后会出现超时

查看nginx errorlog 

2018/12/10 14:22:39 [error] 46593#46593: *134 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 192.168.1.60, server: localhost, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:6000/", host: "192.168.1.177"

原因大概是 websocket 长链接,而nginx 60秒超时

搜到一篇参考资料:https://blog.csdn.net/wangkai_123456/article/details/71715852

还未尝试

 

其它的解决方法

直接运行程序,程序入口

socketIO.run(application, host=127.0.0.1, port=6000, use_reloader=False,debug=True)

执行显示,wsgi starting up

root@uma:~/peach2Produce# python3 Start.py 
(51883) wsgi starting up on http://127.0.0.1:6000

使用文章开头的nginx配置,done!

 

一些坑

nginx 启动提示错误

systemctl status nginx.service

提示错误 Failed to read PID from file /run/nginx.pid: Invalid argument

https://blog.csdn.net/sinat_21302587/article/details/72722096?locationNum=9&fps=1的方法解决

mkdir -p /etc/systemd/system/nginx.service.d 
printf "[Service]\nExecStartPost=/bin/sleep 0.1\n" > /etc/systemd/system/nginx.service.d/override.conf
systemctl daemon-reload 
systemctl restart nginx.service

 

python 升级到了 3.6.7 

pip install uwsgi 报错

    In file included from plugins/python/tracebacker.c:1:0:
    plugins/python/uwsgi_python.h:2:20: fatal error: Python.h: No such file or directory
    compilation terminated.

解决办法: apt-get install python3.6-dev
 

 类似资料: