Skip to content

Commit b3ef414

Browse files
committed
improving WSGI frameworks section of FSP
1 parent dd9e0b2 commit b3ef414

File tree

3 files changed

+131
-11
lines changed

3 files changed

+131
-11
lines changed

feeds/all.atom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<feed xmlns="http://www.w3.org/2005/Atom"><title>Matt Makai</title><link href="http://www.fullstackpython.com/" rel="alternate"></link><link href="http://www.fullstackpython.com/feeds/all.atom.xml" rel="self"></link><id>http://www.fullstackpython.com/</id><updated>2014-02-15T13:03:51Z</updated></feed>
2+
<feed xmlns="http://www.w3.org/2005/Atom"><title>Matt Makai</title><link href="http://www.fullstackpython.com/" rel="alternate"></link><link href="http://www.fullstackpython.com/feeds/all.atom.xml" rel="self"></link><id>http://www.fullstackpython.com/</id><updated>2014-02-16T09:22:08Z</updated></feed>

source/content/pages/wsgi-servers.rst

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,69 @@ v1.0.1 WSGI standard.
2020
:width: 100%
2121
:class: technical-diagram
2222

23-
Requests from the browser that are not for static assets (this is specified
24-
in the web server's configuration which requests are for static assets and
25-
which are not) are passed to the WSGI server. Once the request is
26-
processed and generated by the WSGI server, the response is passed
27-
back through the web server and onto the browser.
23+
A web server's configuration specifies what requests should be passed to
24+
the WSGI server to process. Once a request is processed and generated by the
25+
WSGI server, the response is passed back through the web server and onto
26+
the browser. For example, this Nginx web server's configuration specifics
27+
Nginx should handle static assets (such as images, JavaScript, and CSS
28+
files) under the /static directory and pass all other requests to the WSGI
29+
server running on port 8000::
30+
31+
# this specifies that there is a WSGI server running on port 8000
32+
upstream app_server_djangoapp {
33+
server localhost:8000 fail_timeout=0;
34+
}
35+
36+
# Nginx is set up to run on the standard HTTP port and listen for requests
37+
server {
38+
listen 80;
39+
40+
# nginx should serve up static files and never send to the WSGI server
41+
location /static {
42+
autoindex on;
43+
alias /srv/www/assets;
44+
}
45+
46+
# requests that do not fall under /static are passed on to the WSGI
47+
# server that was specified above running on port 8000
48+
location / {
49+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
50+
proxy_set_header Host $http_host;
51+
proxy_redirect off;
52+
53+
if (!-f $request_filename) {
54+
proxy_pass http://app_server_djangoapp;
55+
break;
56+
}
57+
}
58+
}
59+
60+
Note that the above code is a simplified version of a production-ready Nginx
61+
configuration. For real SSL and non-SSL templates, take a look at the
62+
`Underwear web server templates <https://github.com/makaimc/underwear/tree/master/underwear/roles/web/templates>`_ on GitHub.
63+
64+
WSGI is by design a simple standard interface for running Python code. As
65+
a web developer you won't need to know much more than
66+
67+
* what WSGI stands for (Web Server Gateway Inteface)
68+
69+
* that a WSGI container is a separate running process that runs on a
70+
different port than your web server
71+
72+
* your web server is configured to pass requests to the WSGI container which
73+
runs your web application, then pass the response (in the form of HTML)
74+
back to the requester
75+
76+
If you're using a standard web framework such as Django, Flask, or
77+
Bottle, or almost any other current Python framework, you don't need to worry
78+
about how frameworks implement the application side of the WSGI standard.
79+
Likewise, if you're using a standard WSGI container such as Green Unicorn,
80+
uWSGI, mod_wsgi, or gevent, you can get them running without worrying about
81+
how they implement the WSGI standard.
82+
83+
However, knowing the WSGI standard and how these frameworks and containers
84+
implement WSGI should be on your learning checklist though as you become
85+
a more experienced Python web developer.
2886

2987

3088
WSGI Resources
@@ -44,3 +102,8 @@ contains a simple example of how a WSGI-compatible application works.
44102

45103
`Complete single server Django stack tutorial <http://www.apreche.net/complete-single-server-django-stack-tutorial/>`_ is thorough and informative for
46104
non-paas hosting choices.
105+
106+
This detailed post entitled
107+
`The Beautiful Simplicity of an nginx and uWSGI Deployments <http://bartek.im/blog/2012/07/08/simplicity-nginx-uwsgi-deployment.html>`_
108+
is great reading for understanding Nginx and uWSGI configurations.
109+

wsgi-servers.html

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,65 @@ <h1>WSGI Servers</h1>
8383
<a class="reference external" href="http://www.python.org/dev/peps/pep-3333/">PEP 3333</a>, which defines the
8484
v1.0.1 WSGI standard.</p>
8585
<img alt="WSGI Server &lt;-&gt; Web server &lt;-&gt; Browser" class="technical-diagram" src="theme/img/web-browser-server-wsgi.png" style="width: 100%;" />
86-
<p>Requests from the browser that are not for static assets (this is specified
87-
in the web server's configuration which requests are for static assets and
88-
which are not) are passed to the WSGI server. Once the request is
89-
processed and generated by the WSGI server, the response is passed
90-
back through the web server and onto the browser.</p>
86+
<p>A web server's configuration specifies what requests should be passed to
87+
the WSGI server to process. Once a request is processed and generated by the
88+
WSGI server, the response is passed back through the web server and onto
89+
the browser. For example, this Nginx web server's configuration specifics
90+
Nginx should handle static assets (such as images, JavaScript, and CSS
91+
files) under the /static directory and pass all other requests to the WSGI
92+
server running on port 8000:</p>
93+
<pre class="literal-block">
94+
# this specifies that there is a WSGI server running on port 8000
95+
upstream app_server_djangoapp {
96+
server localhost:8000 fail_timeout=0;
97+
}
98+
99+
# Nginx is set up to run on the standard HTTP port and listen for requests
100+
server {
101+
listen 80;
102+
103+
# nginx should serve up static files and never send to the WSGI server
104+
location /static {
105+
autoindex on;
106+
alias /srv/www/assets;
107+
}
108+
109+
# requests that do not fall under /static are passed on to the WSGI
110+
# server that was specified above running on port 8000
111+
location / {
112+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
113+
proxy_set_header Host $http_host;
114+
proxy_redirect off;
115+
116+
if (!-f $request_filename) {
117+
proxy_pass http://app_server_djangoapp;
118+
break;
119+
}
120+
}
121+
}
122+
</pre>
123+
<p>Note that the above code is a simplified version of a production-ready Nginx
124+
configuration. For real SSL and non-SSL templates, take a look at the
125+
<a class="reference external" href="https://github.com/makaimc/underwear/tree/master/underwear/roles/web/templates">Underwear web server templates</a> on GitHub.</p>
126+
<p>WSGI is by design a simple standard interface for running Python code. As
127+
a web developer you won't need to know much more than</p>
128+
<ul class="simple">
129+
<li>what WSGI stands for (Web Server Gateway Inteface)</li>
130+
<li>that a WSGI container is a separate running process that runs on a
131+
different port than your web server</li>
132+
<li>your web server is configured to pass requests to the WSGI container which
133+
runs your web application, then pass the response (in the form of HTML)
134+
back to the requester</li>
135+
</ul>
136+
<p>If you're using a standard web framework such as Django, Flask, or
137+
Bottle, or almost any other current Python framework, you don't need to worry
138+
about how frameworks implement the application side of the WSGI standard.
139+
Likewise, if you're using a standard WSGI container such as Green Unicorn,
140+
uWSGI, mod_wsgi, or gevent, you can get them running without worrying about
141+
how they implement the WSGI standard.</p>
142+
<p>However, knowing the WSGI standard and how these frameworks and containers
143+
implement WSGI should be on your learning checklist though as you become
144+
a more experienced Python web developer.</p>
91145
<div class="section" id="wsgi-resources">
92146
<h2>WSGI Resources</h2>
93147
<p><a class="reference external" href="http://www.python.org/dev/peps/pep-0333/">PEP 0333 WSGI v1.0</a>
@@ -102,6 +156,9 @@ <h2>WSGI Resources</h2>
102156
contains a simple example of how a WSGI-compatible application works.</p>
103157
<p><a class="reference external" href="http://www.apreche.net/complete-single-server-django-stack-tutorial/">Complete single server Django stack tutorial</a> is thorough and informative for
104158
non-paas hosting choices.</p>
159+
<p>This detailed post entitled
160+
<a class="reference external" href="http://bartek.im/blog/2012/07/08/simplicity-nginx-uwsgi-deployment.html">The Beautiful Simplicity of an nginx and uWSGI Deployments</a>
161+
is great reading for understanding Nginx and uWSGI configurations.</p>
105162
</div>
106163

107164
<br/>

0 commit comments

Comments
 (0)