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

内容如题

软件环境如下:

    系统版本:  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
 

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

layyuiop

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值