What-are-the-differences-between-nginx-and-gunicorn

https://www.quora.com/What-are-the-differences-between-nginx-and-gunicorn

So, it’s fairly easy to understand how these three pieces fit together, but it’s also important to understand why it’s recommended to connect the pieces in this way. At a high level, Flask is a framework that you build upon to encapsulate the logic of how your respond to a certain HTTP request, gunicorn (or uwsgi) provide an execution environment for this logic to operate in (primarily handling concurrency-related issues) and nginx provides a proxy which has the effect of sheltering that execution environment and providing certain other capabilities (such as serving static requests or routing to different backends based on URL or some other characteristic of the request).

As I said, Flask provides a framework upon which you can build the logic of your application. It specifies that for a given URL, execute this chunk of code, and gives you some scaffolding to handle logic errors, render templates, execute middleware, etc… Flask has a development server with certain features that are useful, like a debugger, but it is only intended to be used in development because it cannot robustly handle a large load of simultaneous requests.

Flask is built upon WSGI (web server gateway interface), which is a specification for the interaction between application logic and a web server. Gunicorn (or uwsgi is the other popular choice for this role) accepts HTTP requests and knows how to call your application logic to get a response. Flask itself can do this in a very rudimentary way intended for low load and development purposes, but Gunicorn is capable of managing concurrency using mechanisms such as threading and event loops. The primary responsibility here is to take an HTTP request and call the corresponding logic code in a robust, concurrent manner. uwsgi is perhaps a better demonstration of this layer because it is much more configurable, it handles request logging, concurrency, worker timeouts, and so much more. But the most important function at this level is robust concurrency.

Finally you have your web proxy layer (called a reverse proxy). Common wisdom says that serving static files from your application layer is a bad idea because it is not optimized for this purpose. Nginx is very well optimized to serving static content; so, you can set up a URL route at the web proxy layer to decide whether a request is for static content or application logic, and either serve the static content or route to the application. Another important function is SSL/TLS termination. Nginx is also very good at doing this (the ‘s’ in https).

But there’s way more to the proxy layer - it is a powerful tool for scalability and protection. Nginx can do load balancing - if your application grows beyond what one server can handle, or if you want more than one for redundancy, Nginx is capable of making one domain route to multiple web servers. Nginx also provides a shield against load spikes and DOS attacks in a few ways. Firstly, it can handle bursts of requests without falling down, and it has a variety of limit mechanisms to help handle traffic spikes (innocent or nefarious); you can block IPs, you can limit the number of allowable backend connections, you can limit request execution times, it can block requests on basically any characteristic of the request (such as user agent or referrer). But really the most important characteristic of Nginx at this layer is its robustness under traffic spikes - it can generally cope with very large amounts of traffic gracefully, and apply the rules and logic you’ve established to handle those circumstances reliably.

So the three layers all serve different roles, Flask encapsulates your logic, your wsgi server handles Python concurrency and the translation from HTTP to Python, and the reverse proxy acts as a gatekeeper, cushion and traffic warden for the wsgi server(s).

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末策州,一起剝皮案震驚了整個濱河市旁仿,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌尘奏,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,039評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件探熔,死亡現(xiàn)場離奇詭異,居然都是意外死亡其垄,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,426評論 3 395
  • 文/潘曉璐 我一進(jìn)店門卤橄,熙熙樓的掌柜王于貴愁眉苦臉地迎上來绿满,“玉大人,你說我怎么就攤上這事窟扑±洌” “怎么了漏健?”我有些...
    開封第一講書人閱讀 165,417評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長橘霎。 經(jīng)常有香客問我蔫浆,道長,這世上最難降的妖魔是什么姐叁? 我笑而不...
    開封第一講書人閱讀 58,868評論 1 295
  • 正文 為了忘掉前任瓦盛,我火速辦了婚禮,結(jié)果婚禮上外潜,老公的妹妹穿的比我還像新娘原环。我一直安慰自己,他們只是感情好处窥,可當(dāng)我...
    茶點故事閱讀 67,892評論 6 392
  • 文/花漫 我一把揭開白布嘱吗。 她就那樣靜靜地躺著,像睡著了一般滔驾。 火紅的嫁衣襯著肌膚如雪柜与。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,692評論 1 305
  • 那天嵌灰,我揣著相機與錄音弄匕,去河邊找鬼。 笑死沽瞭,一個胖子當(dāng)著我的面吹牛迁匠,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播驹溃,決...
    沈念sama閱讀 40,416評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼城丧,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了豌鹤?” 一聲冷哼從身側(cè)響起亡哄,我...
    開封第一講書人閱讀 39,326評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎布疙,沒想到半個月后蚊惯,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,782評論 1 316
  • 正文 獨居荒郊野嶺守林人離奇死亡灵临,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,957評論 3 337
  • 正文 我和宋清朗相戀三年截型,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片儒溉。...
    茶點故事閱讀 40,102評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡宦焦,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情波闹,我是刑警寧澤酝豪,帶...
    沈念sama閱讀 35,790評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站精堕,受9級特大地震影響寓调,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜锄码,卻給世界環(huán)境...
    茶點故事閱讀 41,442評論 3 331
  • 文/蒙蒙 一夺英、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧滋捶,春花似錦痛悯、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,996評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至巡扇,卻和暖如春扭仁,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背厅翔。 一陣腳步聲響...
    開封第一講書人閱讀 33,113評論 1 272
  • 我被黑心中介騙來泰國打工乖坠, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人刀闷。 一個月前我還...
    沈念sama閱讀 48,332評論 3 373
  • 正文 我出身青樓熊泵,卻偏偏與公主長得像,于是被迫代替她去往敵國和親甸昏。 傳聞我的和親對象是個殘疾皇子顽分,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,044評論 2 355

推薦閱讀更多精彩內(nèi)容