最新nginx部署Django+uWSGI,提高并发

项目流程

  • 客户端访问请求发送到nginx
  • nginx自己处理static请求,通过web socket转发动态请求到uWSGI
  • uWSGI将接收到的包进行处理,转发给wsgi
  • wsgi根据请求调用django项目中的函数,结果返回给wsgi
  • wsgi将结果打包并转发给uWSGI
  • uWSGI将包再转发给nginx
  • nginx再将结果返回给客户端

Django

安装

diango安装包

  • django
  • django-cors-headers

本项目必须的安装包

  • librosa
  • soundfile
  • sklearn
  • ...

如果遇到soundfile引用错误:
OSError: sndfile library not found
解决方案=> apt-get install libsndfile1

测试

python manage.py runserver 0.0.0.0:8000

访问127.0.0.1:8000,看能否正常访问Django网站。
能正常访问,则Django项目成功搭建

注:本文中项目路径为/root/ai_security_platform

Virtualenv

root@iZ8vb7ux6dyyb6a2jkpo2kZ:~# apt-get update
root@iZ8vb7ux6dyyb6a2jkpo2kZ:~# apt-get install python3-pip
root@iZ8vb7ux6dyyb6a2jkpo2kZ:~# pip3 install virtualenv
root@iZ8vb7ux6dyyb6a2jkpo2kZ:~# mkdir vpservers
root@iZ8vb7ux6dyyb6a2jkpo2kZ:~# cd vpserver
root@iZ8vb7ux6dyyb6a2jkpo2kZ:~/vpserver# virtualenv -no-site-packages venv

即创建了新的venv环境,且不包含原系统的Python环境及包

source venv/bin/activate

激活新环境,开始新征程

uWSGI

安装

pip3 install uwsgi

测试

<span id="test_py">

新建test .py

def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello World"]

python3时,return必须使用b"xxx",否则测试时会出现空白页面,没有显示"hello world"

运行

uwsgi --http 0.0.0.0:8000 --wsgi-file test.py

使用uwsgi运行test.py文件, 采用http模式, 端口8000

访问127.0.0.1:8000,页面出现"hello world"则运行成功

Django + uWSGI

uwsgi --http 0.0.0.0:8000 --chdir /root/ai_security_platform --wsgi speaker_recognition.wsgi

--chdir指定工作路径
--wsgi指定wsgi文件,其中speaker_recognition.wsgi表示在speaker_recognition文件夹下的wsgi.py文件

访问127.0.0.1:8000,成功访问则表示django+uWSGI联调成功

Nginx

安装

apt-get install nginx

配置

网上的许多教程中的nginx.conf在新版本中已经删除

修改/etc/nginx/sites-available/default这个文件中的配置:

vim /etc/nginx/sites-available/default

# default文件如下:
upstream django {
        server 127.0.0.1:8001;  #web的socket端口
}

server {
        listen 8000 default_server;
        listen [::]:8000 default_server;

        # root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name _;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                root /root/ai_security_platform;
                uwsgi_pass django;
                include /root/ai_security_platform/speaker_recognition/uwsgi_params;

        }
}

测试

nginx

开启nginx服务,若端口占用,则释放掉或者换端口再开
开启成功后,浏览器访问ip:port,出现502 Bad Gateway则说明成功启动,因为web的socket端口还没配置,所以出现502错误

nginx + uWSGI

使用test.py进行测试
运行

uwsgi --socket :8001 --wsgi-file test.py

不同的是,这次测试运行的命令中,--http改为--socket,端口改为上面配置nginx的web socket端口8001

访问上节启动nginx的页面,502变为hello world则联调成功

nginx + uWSGI + Django

uwsgi --socket :8001 --chdir /root/ai_security_platform --wsgi speaker_recognition.wsgi

访问ip:port,成功访问django项目则联调成功

配置文件化

项目中添加uwsgi的配置文件uwsgi.ini:

[uwsgi]
socket = 127.0.0.1:8001
chdir = /root/ai_security_platform/
module = speaker_recognition.wsgi
master = true
processes = 2
threads = 2
max-requests = 2000
chmod-socket = 664
vacuum = true
daemonize = /root/ai_security_platform/log/uwsgi.log
  • socket与nginx中配置的socket端口一致
  • chdir 为项目文件路径
  • module 为项目中wsgi的路径,speaker_recognition.wsgi表示speaker_recognition文件夹下得wsgi.py文件
  • daemonize表示后台运行,并且指定log输出文件

运行:

uwsgi --ini uwsgi.ini

访问nginx配置的地址,正常访问到django项目网站则联调成功

常见问题

  • Nginx : worker-connections are not enough.
    Solution:
    vim /etc/nginx/nginx.conf
events {
    worker_connections  2048;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容