1
Twisted.web2 Deployment
2
=======================
4
There are a number of possibilities for deploying twisted.web2: as standalone HTTP[S] server, HTTP proxied behind another server, SCGI, FastCGI, or CGI.
6
Deploying as a standalone HTTP/HTTPS server is by far the simplest. Unless you have a reason not to, it is recommended that you choose this option. However, many people already run web servers on their computer and are not willing or able to completely blow it away and replace it with twisted.web2. The next best option is to run twisted.web2 as a server proxied behind your existing webserver, using either HTTP or SCGI.
10
For completeness, here is a simple standalone HTTP server again.
12
.. pythonfile:: examples/deployment/standalone.tac
16
If you use HTTP proxying, you must inform twisted.web2 of the real URL it is being accessed by, or else any URLs it generates will be incorrect. You can do this via the AutoVHostURIRewrite resource when using apache2 as the main server.
18
On the apache side, configure as follows. Apache automatically sends the original host in the X-Forwarded-Host header, and the original remote IP address in the X-Forwarded-For header. You must additionally send along the original path, and the original scheme.
20
For proxying a subdirectory::
23
ProxyPass http://localhost:8538/
24
RequestHeader set X-App-Location /whatever/
25
RequestHeader set X-App-Scheme http
29
Or, for serving an entire HTTPS virtual host::
31
<VirtualHost myip:443>
32
ServerName example.com
33
ProxyPass / http://localhost:8538/
34
RequestHeader set X-App-Location /
35
RequestHeader set X-App-Scheme https
38
Now, on the twisted.web2 side
40
.. pythonfile:: examples/deployment/apache2.tac
45
Apache 1 doesn't provide the X-Forwarded-Host or X-Forwarded-For headers, or the ability to set custom headers in the outgoing proxy request. Therefore, you must provide that information to twisted.web2 directly. This is accomplished by the VHostURIRewrite resource.
47
Setup apache as follows::
50
ServerName example.com
51
ProxyPass /foo/ http://localhost:8538/
56
.. pythonfile:: examples/deployment/apache1.tac
58
Because vhost.VHostURIRewrite can exist anywhere in the resource tree, you can have multiple applications running on a single twisted port by making them siblings of a root resource and referencing their full path in the ProxyPass directive.
60
Setup apache as follows::
62
<VirtualHost foo.myhostname.com>
63
ProxyPass / http://localhost:8538/foo/
64
ServerName example.com
67
<VirtualHost bar.myhostname.com>
68
ProxyPass / http://localhost:8538/bar/
69
ServerName example.com
74
.. pythonfile:: examples/deployment/apache1_twohosts.tac
78
SCGI is an alternative to HTTP proxying. SCGI should work instead of HTTP proxying from servers which support it. Additionally, if all you have access to from the web server is CGI, but are able to run long-running processes, you can use the cgi2scgi_ C program to channel CGI requests to your twisted.web2 SCGI port. This won't be as efficient as mod_scgi or http proxying, but it will be much better than using twisted directly as a CGI.
80
FIXME:Someone who has installed mod_scgi in apache should write a bit on it.
82
Configure Twisted as follows
84
.. pythonfile:: examples/deployment/scgi.tac
88
FastCGI is another popular way to run a web application. Blah blah.
92
CGI is the worst possible deployment environment, yet in some cases it may be all that is possible. It allows only a single request to be served from a process, so any kind of in-memory storage is impossible. Also, the overhead of starting up a new python interpreter for every request can get quite high. You should only consider using it if your hosting provider does not allow you to keep a process running.
94
However, if it's your only choice, you can deploy a twisted.web2 app using it. Unlike the other examples, where we create a .tac file for running with twistd, in this case, a standalone python script is necessary
98
from twisted.web2 import channel, server, static
99
toplevel = static.File("/tmp")
100
site = server.Site(toplevel)
101
channel.startCGI(site)
103
.. _cgi2scgi: http://www.mems-exchange.org/software/scgi/scgi-1.2.tar.gz/scgi-1.2/cgi2scgi.c
b'\\ No newline at end of file'