Gunicorn 和 Nginx
Web Application Deployment Using Nginx
Nginx is a very high performant web server / (reverse)-proxy. It has reached its current popularity due to being light weight, relatively easy to work with, and easy to extend (with add-ons / plug-ins). Thanks to its architecture, it is capable of handling a lot of requests (virtually unlimited), which - depending on your application or website load - could be really hard to tackle using some other, older alternatives.
Remember: "Handling" connections technically means not dropping them and being able to serve them with something. You still need your application and database functioning well in order to have Nginx serve clients *esponses that are not error messages.
Why use Nginx as a reverse-proxy in front of an application server?
Many frameworks and application servers (including Gunicorn) can serve static files (e.g. javascript, css, images etc.) together with responses. However, the better thing to do is to let a (reverse-proxy) server such as Nginx handle the task of serving these files and managing connections (requests). This relieves a lot of the load from the application servers, granting you a much better overall performance.
As your application grows, you will want to optimize it— and when the time comes, distribute it across servers (VPS) to be able to handle more connections simultaneously (and have a generally more robust architecture). Having a reverse-proxy in front of your application server(s) helps you with this from the very beginning.
Nginx's extensibility (e.g. native caching along with failover and other mechanisms) is also a great feat that benefits web applications unlike (simpler) application servers.
https://www.digitalocean.com/community/tutorials/how-to-deploy-python-wsgi-apps-using-gunicorn-http-server-behind-nginx
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Why do I need Nginx and something like Gunicorn?
Overly simplified: You need something that executes Python but Python isn't the best at handling all types of requests.
[disclaimer: I'm a Gunicorn developer]
Less simplified: Regardless of what app server you use (Gunicorn, mod_wsgi, mod_uwsgi, cherrypy) any sort of non-trivial deployment will have something upstream that will handle the requests that your Django app should not be handling. Trivial examples of such requests are serving static assets (images/css/js).
This results in two first tiers of the classic "three tier architecture". Ie, the webserver (Nginx in your case) will handle many requests for images and static resources. Requests that need to be dynamically generated will then be passed on to the application server (Gunicorn in your example). (As an aside, the third of the three tiers is the database)
Historically speaking, each of these tiers would be hosted on separate machines (and there would most likely be multiple machines in the first two tiers, ie: 5 web servers dispatch requests to two app servers which in turn query a single database).
In the modern era we now have applications of all shapes and sizes. Not every weekend project or small business site actually needs the horsepower of multiple machines and will run quite happily on a single box. This has spawned new entries into the array of hosting solutions. Some solutions will marry the app server to the web server (Apache httpd + mod_wsgi, Nginx + mod_uwsgi, etc). And its not at all uncommon to host the database on the same machine as one of these web/app server combinations.
Now in the case of Gunicorn, we made a specific decision (copying from Ruby's Unicorn) to keep things separate from Nginx while relying on Nginx's proxying behavior. Specifically, if we can assume that Gunicorn will never read connections directly from the internet, then we don't have to worry about clients that are slow. This means that the processing model for Gunicorn is embarrassingly simple.
The separation also allows Gunicorn to be written in pure Python which minimizes the cost of development while not significantly impacting performance. It also allows users the ability to use other proxies (assuming they buffer correctly).
As to your second question about what actually handles the HTTP request, the simple answer is Gunicorn. The complete answer is both Nginx and Gunicorn handle the request. Basically, Nginx will receive the request and if it's a dynamic request (generally based on URL patterns) then it will give that request to Gunicorn, which will process it, and then return a response to Nginx which then forwards the response back to the original client.
So in closing, yes. You need both Nginx and Gunicorn (or something similar) for a proper Django deployment. If you're specifically looking to host Django with Nginx, then I would investigate Gunicorn, mod_uwsgi, and maybe CherryPy as candidates for the Django side of things.
Gunicorn 和 Nginx的更多相关文章
- gunicorn+anaconda+nginx部署django项目(ubuntu)
首先进入conda 虚拟环境: source activate test 安装gunicorn: pip install gunicorn 运行gunicorn gunicorn -w 2 -b 12 ...
- 在nginx上部署django项目--------Gunicorn+Django+nginx+mysql
一.安装nginx 以前的博客我有写,这里就不写了 http://www.cnblogs.com/wt11/p/6420442.html 二.安装mysql 我用的mysql5.7 64位的二进制包 ...
- (Nginx + Gunicorn) 时 nginx 400 request line is too large (4360 4094)
查看nginx下面两个参数 值是否满足 client_header_buffer_size 512k;large_client_header_buffers 4 512k; 满足依然出现 如果ngin ...
- gunicorn flask nginx环境配置
防火墙端口设置****修改后要重启生效(有时在控制面板中设置80端口无法生效) /etc/init.d/iptables statusservice iptables stopfirewall-cmd ...
- Python3 Flask+nginx+Gunicorn部署(上)
前言:一般在本地运行flask项目通常是直接python3 文件名.py,然后打开:http://127.0.0.1:5000 查看代码结果 这次主要是记录flask在python3 环境结合ngin ...
- 【新手向】阿里云上ubuntu+flask+gunicorn+nginx服务器部署(二)项目部署
本项目实现的是类似于ins的图片分享网站.继续(一),当nginx的配置已修改好后,要在远程服务器上部署网站,只需要几个步骤: 1 前期准备 2 将运行网站的代码从github上下载过来 3 下载依赖 ...
- 如何使用Nginx和uWSGI或Gunicorn在Ubuntu上部署Flask Web应用
你好!欢迎阅读我的博文,你可以跳转到我的个人博客网站,会有更好的排版效果和功能. 此外,本篇博文为本人Pushy原创,如需转载请注明出处:https://pushy.site/posts/151981 ...
- nginx+uwsgi 和nginx+gunicorn区别、如何部署
[线上环境部署Django,nginx+uwsgi 和nginx+gunicorn,这两种方案,应该如何选择?] 大家是采用的何种部署方式? 第一种,高并发稳定一点 我们公司使用的是nginx+gun ...
- 使用docker-compose部署nginx+gunicorn+mariadb的django应用
目录 1. docker-compose 项目的组织目录 2. 构建 mysql 容器 3. 构建 django-blog 容器 4. 构建 nginx 容器 5. docker-compose.ya ...
随机推荐
- Yocto开发笔记之《Tip-bitbake常用命令》(QQ交流群:519230208)
开了一个交流群,欢迎爱好者和开发者一起交流,转载请注明出处. QQ群:519230208,为避免广告骚扰,申请时请注明 “开发者” 字样 =============================== ...
- Jquery 的事件方法
1.$(selector).bind(event,data,function,map) //给元素添加一个事件 2.当元素失去焦点时发生 blur 事件,获得焦点时触发focus事件: $(" ...
- 9月5日网页基础知识 通用标签、属性(body属性、路径、格式控制) 通用标签(有序列表、无序列表、常用标签)(补)
网页基础知识 一.HTML语言 HTML语言翻译汉语为超文本标记语言. 二.网页的分类 1.静态页面:在静态页面中修改网页内容实际上就是修改网页原代码,不能从后台操作,数据来只能来源于原于代码.静态网 ...
- 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【三】——Web Api入门
系列导航地址http://www.cnblogs.com/fzrain/p/3490137.html 前言 经过前2节的介绍,我们已经把数据访问层搭建好了,从本章开始就是Web Api部分了.在正式开 ...
- 在项目中那个少用if else 语句,精简代码,便于维护的方法(1)
一般我在写一个函数的时候,可能需要一个回调函数,例如: function loadQtipCode(dom, title, content, width, showcb, hidecb) { $(do ...
- Shader
Shader的学习方法总结 http://www.cnblogs.com/Esfog/p/How_To_Learn_Shader.html [Shader 着色器]学习shader之前必须知道的东西之 ...
- maven可选依赖(Optional Dependencies)和依赖排除(Dependency Exclusions)
我们知道,maven的依赖关系是有传递性的.如:A-->B,B-->C.但有时候,项目A可能不是必需依赖C,因此需要在项目A中排除对A的依赖.在maven的依赖管理中,有两种方式可以对依赖 ...
- [Unity] UGUI研究院之游戏摇杆
最近在做Unity基于UGUI的摇杆,发现了一种非常简单并且巧妙的方法,原文在这里, 不过要FQ!!http://godstamps.blogspot.tw/2015/07/unity-ugui-sc ...
- javascript生成二维码
参考: http://www.w3dev.cn/article/20140617/javascript-create-QR-code.aspx
- JS原型链理解
1. 每个对象都有原型属性(__proto__)2. 对象的原型(__proto__)指向其构造函数(Class)的prototype属性3. 构造函数(Class)的prototype属性本身也是一 ...