uwsgi nginx與django之間的關系以及各自的作用


首先要明確幾個概念及其作用(注意大小寫的區別):

  • WSGI
  • uWSGI
  • uwsgi
  • Nginx

WSGI 是一種協議,不是任何包不是任何服務器,就和 TCP 協議一樣。它定義了 Web 服務器和 Web 應用程序之前如何通信的規范。

至於為什么和 Python 扯在一起?因為這個協議是由 Python 在 2003 年提出的。(參考:PEP-333 和 PEP-3333 )

WSGI is the Web Server Gateway Interface. It is a specification that describes how a web server communicates with web applications, and how web applications can be chained together to process one request.

uWSGI 是一個全功能的 HTTP 服務器,他要做的就是把 HTTP 協議轉化成語言支持的網絡協議。比如把 HTTP 協議轉化成 WSGI 協議,讓 Python 可以直接使用。

The uWSGI project aims at developing a full stack for building hosting services.

Application servers (for various programming languages and protocols), proxies, process managers and monitors are all implemented using a common api and a common configuration style.

uwsgi 是一種 uWSGI 的內部協議,使用二進制方式和其他應用程序進行通信。

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.

Nginx 是一個 Web 服務器其中的 HTTP 服務器功能和 uWSGI 功能很類似,但是 Nginx 還可以用作更多用途,比如最常用的反向代理功能。

 

 

 

接下是為什么不能用 Django 的 Web 服務器直接部署

Django 是一個 Web 框架,框架的作用在於處理 request 和 response,其他的不是框架所關心的內容。所以怎么部署 Django 不是 Django 所需要關心的。

Django 所提供的是一個開發服務器,這個開發服務器,沒有經過安全測試,而且使用的是 Python 自帶的 simple HTTPServer 創建的,在安全性和效率上都是不行的。

DO NOT USE THIS SERVER IN A PRODUCTION SETTING. It has not gone through security audits or performance tests.

 

在 Django 源碼中可以很清楚的看出來,runserver 起來的 HTTPServer 就是 Python 自帶的 simple_server

 

其中 inner_run 函數中的 run 方法和 run 方法中 server_cls 參數分別取自 django.core.servers.basehttp:run 和 django.core.servers.basehttp:WSGIServer

而 WSGIServer 又的父類就是 wsgiref.simple_server。既然是 simple 了很多東西都是不太可以的。

既然 uWSGI 可以完成 Nginx 功能,那為什么又要用 Nginx

因為 Nginx 牛逼啊,能直接在 Nginx 層面就完成很多事情,比如靜態文件、反向代理、轉發等需求。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM