using NGINX+uWSGI to deploy django project

发布时间:2022-07-04 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了using NGINX+uWSGI to deploy django project脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

ArchITecture

实现前后端分离架构:

网页静态资, 由nginx直接响应。

DJANGO脚本资源的请求, 由uWSGI服务器处理。

 

https://decoVAR.dev/blog/2019/11/03/nginx-uwsgi-python-scripts/#nginx-server-config

You have a static HTML page with a form on it. And you want this form to be PRocessed by a Python script when submitted. And you serve your page with NGINX. To make all that work you will also need an application server such as uWSGI.

using NGINX+uWSGI to deploy django project

 

Django+uWSGI+Nginx

https://www.jianshu.COM/p/1c50b15b143a

using NGINX+uWSGI to deploy django project

 

整体架构
  1. Nginx 作为反向代理服务器:
    • 负责静态资源处理、动态请求转发以及结果的回复;
  2. uWSGI 作为Web服务器:
    • 负责接收 Nginx 请求转发并处理后发给 Django 以及 接收 Django 返回信息转发给 Nginx;
  3. Django 作为应用框架:
    • 收到请求后处理数据并响应结果给 uWSGI 服务器。

uWSGI作用

虽然django有自带的web服务器, 通过python manage.py runserver可以启动;

但是这就是调试环境, 对于多一些的并发请求, 此服务器很快就挂死了。

使用uWSGI服务器, 来运行django脚本, 则性能要好得多。

 

支持水平扩展,利用多核优势

同时此服务器是主从模式, 可以开启多个worker来处理请求,支持水平扩展。

https://www.chenshaowen.com/bLOG/how-to-package-django-development-environments-using-docker-4.html

using NGINX+uWSGI to deploy django project

 

 

 

 

支持不同技后台

https://blog.csdn.net/orangleliu/article/details/47080999

using NGINX+uWSGI to deploy django project

 

 

 

支持host静态文件

https://uwsgi-docs.readthedocs.io/en/latest/StatiCFiles.html#:~:text=Fortunately%20uWSGI%20has%20a%20wide%20series%20of%20options,simply%20forward%20dynamic%20requests%20to%20uWSGI%20backend%20nodes.

You can sPEcify --check-static multiple times to specify multiple possible root paths.

--check-static /customers/foobar/app001/public --check-static /customers/foobar/app001/static
@H_149_126@

 

详细概念

https://zhuanlan.zhihu.com/p/36448645

uWSGI是一个Web服务器,它实现了WSGI协议、uwsgi、http等协议。Nginx中HttpUwsgiModule的作用是与uWSGI服务器进行交换。

要注意 WSGI / uwsgi / uWSGI 这三个概念的区分。

  • WSGI看过前面小节的同学很清楚了,是一种通信协议。
  • uwsgi同WSGI一样是一种通信协议。
  • 而uWSGI是实现了uwsgi和WSGI两种协议的Web服务器。

uwsgi协议是一个uWSGI服务器自有的协议,它用于定义传输信息的类型(type of information),每一个uwsgi packet前4byte为传输信息类型描述,它与WSGI相比是两样东西。

 

WSGI是python的一个服务器和应用解耦的协议。促进了python web生态的繁荣。

WSGI是什么

WSGI,全称 Web Server Gateway Interface,或者 Python Web Server Gateway Interface ,是为 Python 语言定义的 Web 服务器和 Web 应用程序或框架之间的一种简单而通用的接口。自从 WSGI 被开发出来以后,许多其它语言中也出现了类似接口。

WSGI 的官方定义是,the Python Web Server Gateway Interface。从名字就可以看出来,这东西是一个Gateway,也就是网关。网关的作用就是在协议之间进行转换。

WSGI 是作为 Web 服务器与 Web 应用程序或应用框架之间的一种低级别的接口,以提升可移植 Web 应用开发的共同点。WSGI 是基于现存的 CGI 标准而设计的。

很多框架都自带了 WSGI server ,比如 Flask,webpy,Django、CherryPy等等。当然性能都不好,自带的 web server 更多的是测试用途,发布时则使用生产环境的 WSGI server或者是联合 nginx 做 uwsgi 。

 

也就是说,WSGI就像是一座桥梁,一边连着web服务器(如 nginx),另一边连着Python的应用程序Application。但是呢,这个桥的功能很弱,有时候还需要别的桥来帮忙才能进行处理。WSGI 的作用如图所示:

using NGINX+uWSGI to deploy django project

 

 

Nginx作为入口前台

主要还是性能考虑, uWSGI 和 Gunicorn 还是面向脚本等耗时处理, 内部是阻塞式服务,

但是对于页面来说,有很多的请求是静态文件的请求, 这些请求,如果被慢客户端发出, 则慢连接则会占用 uWSGI 和 Gunicorn 线程, 则会阻塞静态文件的请求。

nginx设计上可以处理高并发请不求, 适用于高IO场景, 计算很少, 则非常有利于处理静态资源请求。

 

https://www.zhihu.com/question/38528616

Nginx主要作用(包括但不限于):

  • 提供子域名;
  • 提供SSL证书,强制HTTP转HTTPS;
  • 安全保护,隔离内网主机;
  • 高可用请求转发,负载均衡;
  • 文件类静态内容加速;
  • 另外提供多种数据透传转发。

1、为什么需要Nginx转发Gunicorn服务?

Nginx功能强大,使用Nginx有诸多好处,但用Nginx转发Gunicorn服务,重点是解决“慢客户端行为”给服务器带来的性能降低问题;另外,在互联网上部署HTTP服务时,还要考虑的“快客户端响应”、SSL处理和高并发等问题,而这些问题在Nginx上一并能搞定,所以在Gunicorn服务之上加一层Nginx反向代理,是个一举多得的部署方案

nginx对接uWSGI的模块

http://nginx.org/en/docs/http/ngx_http_uwsgi_module.html

The ngx_http_uwsgi_module module allows passing requests to a uwsgi server.

Example Configuration

 

location / {
    include    uwsgi_params;
    uwsgi_pass localhost:9000;
}

 

uWSGI与nginx对接的协议

https://uwsgi-docs.readthedocs.io/en/latest/Protocol.html

The uwsgi (lowercase!) protocol is the native protocol used by the uWSGI server.

It is a binary protocol that can carry any type of data. The First 4 bytes of a uwsgi packet describe the type of the data contained by the packet.

Every uwsgi request generates a response in the uwsgi format.

有两种连接协议, socket 要比 http socket高效些。

https://stackoverflow.com/questions/36475380/what-are-the-advantages-of-connecting-uwsgi-to-nginx-using-the-uwsgi-protocol

using NGINX+uWSGI to deploy django project

 

 

According to the research From PageFair you will have a boost in throughput with unix_socket+uwsgi over tcp+http. Its not x2, but still.

Numbers they provide indicate that tcp_socket+http gets 1247 queries/second, while unix_socket+uwsgi gets 1495 q/s.

支持多个APP(多个uWSGI)

https://jawher.me/multiple-python-apps-with-nginx-uwsgi-emperor-upstart/

server {
  listen 80;
  server_name jawher.me;
  index index.html;

  location /app1/ {
    include uwsgi_params;
    uwsgi_param SCRIPT_NAME /app1;
    uwsgi_modifier1 30;
    uwsgi_pass unix://tmp/app1.sock;
  }

  location /app2/ {
    include uwsgi_params;
    uwsgi_param SCRIPT_NAME /app2;
    uwsgi_modifier1 30;
    uwsgi_pass unix://tmp/app2.sock;
  }
}

安装

https://ubuntu.com/tutorials/install-and-configure-nginx#2-installing-nginx

sudo apt update
sudo apt install nginx

 

配置

https://decovar.dev/blog/2019/11/03/nginx-uwsgi-python-scripts/#nginx-server-config

 

静态资源服务

https://docs.nginx.com/nginx/admin-guide/web-server/serving-static-content/

server {
    root /www/data;

    location / {
    }

    location /images/ {
    }

    location ~ .(mP3|mp4) {
        root /www/media;
    }
}

 

 

反向代理

https://docs.nginx.com/nginx/admin-guide/web-server/reverse-Proxy/

https://nginx.org/en/docs/http/ngx_http_uwsgi_module.html#uwsgi_pass

 

负载均衡

http://nginx.org/en/docs/http/load_balancing.html

http {
    upstream myapp1 {
        server srv1.example.com;
        server srv2.example.com;
        server srv3.example.com;
    }

    server {
        listen 80;

        location / {
            proxy_pass http://myapp1;
        }
    }
}

 

 

location指令

https://zhuanlan.zhihu.com/p/137146854

 

参考

https://www.runoob.com/python3/python-uwsgi.html

https://www.runoob.com/django/django-nginx-uwsgi.html

https://stackoverflow.com/questions/36475380/what-are-the-advantages-of-connecting-uwsgi-to-nginx-using-the-uwsgi-protocol

脚本宝典总结

以上是脚本宝典为你收集整理的using NGINX+uWSGI to deploy django project全部内容,希望文章能够帮你解决using NGINX+uWSGI to deploy django project所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。