uWSGI的意义是什么?

问题:uWSGI的意义是什么?

我正在研究WSGI规范,并试图弄清楚像uWSGI这样的服务器是如何适应图片的。我了解WSGI规范的重点是将Web服务器(如nginx)与Web应用程序(如您使用Flask编写的东西)分开。我不明白uWSGI是做什么的。为什么Nginx无法直接调用我的Flask应用程序?Flask不能直接对它说WSGI吗?uWSGI为什么需要介入它们之间?

WSGI规范有两个方面:服务器和Web应用程序。uWSGI在哪一边?

I’m looking at the WSGI specification and I’m trying to figure out how servers like uWSGI fit into the picture. I understand the point of the WSGI spec is to separate web servers like nginx from web applications like something you’d write using Flask. What I don’t understand is what uWSGI is for. Why can’t nginx directly call my Flask application? Can’t flask speak WSGI directly to it? Why does uWSGI need to get in between them?

There are two sides in the WSGI spec: the server and the web app. Which side is uWSGI on?


回答 0

好吧,我想我现在明白了。

为什么Nginx无法直接调用我的Flask应用程序?

因为nginx不支持WSGI规范。从技术上讲,nginx可以根据需要实施该WSGI规范,而实际上并没有。

在这种情况下,我们需要一个能够实现规范的Web服务器,这正是该uWSGI服务器的作用。

请注意,这uWSGI是一个完整的http服务器,可以单独运行并且运行良好。我已经以这种身份多次使用它,并且效果很好。如果您需要超高的静态内容吞吐量,则可以选择停留nginxuWSGI服务器前。完成后,他们将通过称为的低级协议进行通信uwsgi

“什么事?!又叫uwsgi ?!” 你问。是的,令人困惑。当您引用时,uWSGI您正在谈论的是http服务器。当您谈论uwsgi(全部小写)时,您所谈论的是是该uWSGI 服务器用来与其他服务器进行通讯二进制协议nginx。他们在这个名字上取了一个坏名字。

对于任何有兴趣的人,我都写了一篇有关它的博客文章,其中包含更多细节,一些历史和一些示例。

Okay, I think I get this now.

Why can’t nginx directly call my Flask application?

Because nginx doesn’t support the WSGI spec. Technically nginx could implement the WSGI spec if they wanted, they just haven’t.

That being the case, we need a web server that does implement the spec, which is what the uWSGI server is for.

Note that uWSGI is a full fledged http server that can and does work well on its own. I’ve used it in this capacity several times and it works great. If you need super high throughput for static content, then you have the option of sticking nginx in front of your uWSGI server. When you do, they will communicate over a low level protocol known as uwsgi.

“What the what?! Another thing called uwsgi?!” you ask. Yeah, it’s confusing. When you reference uWSGI you are talking about an http server. When you talk about uwsgi (all lowercase) you are talking about a binary protocol that the uWSGI server uses to talk to other servers like nginx. They picked a bad name on this one.

For anyone who is interested, I wrote a blog article about it with more specifics, a bit of history, and some examples.


回答 1

在这种情况下,NGINX仅用作反向代理和渲染 静态文件而不动态文件,它接收请求并将它们代理到应用服务器(即UWSGI)。

UWSGI服务器负责使用WSGI接口加载Flask应用程序。实际上,您可以使UWSGI直接侦听来自Internet的请求,并根据需要删除NGINX,尽管它通常在反向代理之后使用。

文档

uWSGI支持几种与Web服务器集成的方法。它也能够自己处理HTTP请求。

简单来说,WSGI只是一个接口规范,它告诉您应该实现哪些方法以在服务器和应用程序之间传递请求和响应。当使用Flask或Django之类的框架时,由框架本身来处理。

换句话说,WSGI本质上是python应用程序(Flask,Django等)和Web服务器(UWSGI,Gunicorn等)之间的契约。好处是您可以轻松更改Web服务器,因为您知道它们符合WSGI规范,而这实际上是目标之一,如PEP-333中所述

Python目前拥有各种各样的Web应用程序框架,如Zope的,堂吉诃德,Webware的,SkunkWeb,PSO和扭曲网站-仅举几个1。对于新的Python用户而言,如此众多的选择可能是一个问题,因为通常来说,他们对Web框架的选择将限制他们对可用Web服务器的选择,反之亦然。

NGINX in this case only works as a reverse proxy and render static files not the dynamic files, it receives the requests and proxies them to the application server, that would be UWSGI.

The UWSGI server is responsible for loading your Flask application using the WSGI interface. You can actually make UWSGI listen directly to requests from the internet and remove NGINX if you like, although it’s mostly used behind a reverse proxy.

From the docs:

uWSGI supports several methods of integrating with web servers. It is also capable of serving HTTP requests by itself.

WSGI is just an interface specification, in simple terms, it tells you what methods should be implemented for passing requests and responses between the server and the application. When using frameworks such as Flask or Django, this is handled by the framework itself.

In other words, WSGI is basically a contract between python applications (Flask, Django, etc) and web servers (UWSGI, Gunicorn, etc). The benefit is that you can change web servers with little effort because you know they comply with the WSGI specification, which is actually one of the goals, as stated in PEP-333.

Python currently boasts a wide variety of web application frameworks, such as Zope, Quixote, Webware, SkunkWeb, PSO, and Twisted Web — to name just a few 1. This wide variety of choices can be a problem for new Python users, because generally speaking, their choice of web framework will limit their choice of usable web servers, and vice versa.


回答 2

传统的Web服务器无法理解或无法运行Python应用程序。这就是WSGI服务器出现的原因。另一方面,Nginx支持反向代理来处理请求并传递Python WSGI服务器的响应。

该链接可能会对您有所帮助:https : //www.fullstackpython.com/wsgi-servers.html

A traditional web server does not understand or have any way to run Python applications. That’s why WSGI server come in. On the other hand Nginx supports reverse proxy to handle requests and pass back responses for Python WSGI servers.

This link might help you: https://www.fullstackpython.com/wsgi-servers.html


回答 3

简而言之,可以想像一下您在Nginx Web服务器上运行CGI或PHP应用程序的情况。您将使用像php-fpm这样的相应处理程序来运行这些文件,因为Web服务器以其本机格式不会呈现这些格式。

In simple terms, just think of an analogy where you are running a CGI or PHP application with Nginx web server. You will use the respective handlers like php-fpm to run these files since the webserver, in its native form doesn’t render these formats.