PylonsHQ.

Layout: Fixed-width

Apache as a reverse proxy for Pylons

Unknown macro: {metadata-list}
Name Apache and mod_proxy for Pylons
Space Pylons Cookbook
Section Deployment
Version 1.0
Status Draft
Reviewed False
Author(s) James Gardner

Introduction

This article describes how to configure an Apache virtual host to proxy to a Pylons application run using the paster serve command. The idea of "reverse proxy" is that you run your Pylons application in a separate HTTP server and setup Apache to forward incoming requests to your app and to rely responses back to the client.

See also: this pylons thread and apache docs.

Setting Up Apache 

First enable the necessary modules (installing them if necessary):

sudo a2enmod proxy
sudo a2enmod proxy_http

Next you need to add the ProxyPass directives.

Load the file which contains the VirtualHost definition for the site you want to proxy from and add the Proxy entries shown in the example below to the end of your virtual hosts section.

You should take care not to add a trailing / after the URLs. You should also replace 5000 with the number of the port at which the server is actually running and replace /forms with the path at which you want the Pylons application to be available. For example if you want the Pylons application to be available at the root of the domain you should replace /forms with /.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
<VirtualHost *>
ServerName some.domain

# ... usual options here, then at the end add the ProxyPass entries...

ProxyPass /forms http://localhost:5000/
ProxyPassReverse /forms http://localhost:5000/
ProxyPreserveHost On
<Proxy *>
    Order deny,allow
    Allow from all
</Proxy>
</VirtualHost>

You'll need to restart Apache for the settings to take effect.

Configuring Pylons

Apache is now setup to proxy all requests from /forms to your Pylons application. There is one complication though. Your Pylons application will receive all requests and think they are from / not from /forms so we need to add a proxy prefix to your Pylons configuration file.

The configuration file you need to edit will be in your Pylons application directory. By default this is the MyProject/development.ini file but you should edit whichever file you choose to use later in this tutorial when setting up the server.

Replace the current [app:main] section with this:

1
2
3
4
5
6
7
8
[app:main]
use = egg:RMSForms
filter-with = proxy-prefix
# Usual options here

[filter:proxy-prefix]
use = egg:PasteDeploy#prefix
prefix = /forms

You will need to replace /forms with the correct prefix. If Pylons isn't correctly working out the port the proxy is running on you can also use the force-port option in the [filter:proxy-prefix] section. The proxy-prefix is documented at http://pythonpaste.org/deploy/class-paste.deploy.config.PrefixMiddleware.html.

If you start the Pylons server with

paster serve development.ini

and visit http://localhost/forms you should see your Pylons application served correctly,

Paster scripts.

Moved here: Scripts for paster serve.

Debugging

If you get 503 errors from Apache check you have specified the correct URL and port in the virtual host settings and that the paster server is running. If everything is correct try restarting Apache:

/etc/init.d/apache restart

Labels

apache apache Delete
proxy proxy Delete
deployment deployment Delete
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
  1. Feb 04, 2008

    Daniele Paolella says:

    If you experience any problem with paster setup-app (and maybe nosetests) compla...

    If you experience any problem with paster setup-app (and maybe nosetests) complaining about The section 'main' is not the application (probably a filter), you may use this temporary workaround instead of the filter-with line paired with the filter:proxy-prefix block in your configuration file:

    add a custom line in your main section

    1
    yourapp.proxy_prefix = /yourprefix
    

    then, in config/middleware.py, right before return app, add this:

    1
    2
    3
    if config.get('yourapp.proxy_prefix'):
        from paste.deploy.config import PrefixMiddleware
        app = PrefixMiddleware(app, prefix=config['yourapp.proxy_prefix'])
    

  2. Dec 25, 2008

    Thomas Johnson says:

    <Proxy *> is bad. Instead use something like <Proxy http://127.0.0.1:50...

    <Proxy *> is bad. Instead use something like <Proxy http://127.0.0.1:5000/>

  3. Jan 02, 2010

    Arnys says:

    You should take care not to add a trailing / after the URLs. You mean here? &...

    You should take care not to add a trailing / after the URLs.

    You mean here?

     79     ProxyPass / http://localhost:5000/
     80     ProxyPassReverse / http://localhost:5000/

    Without the trailing slashes I get errors "proxy: DNS lookup failure"


Powered by Pylons - Contact Administrators