Setting up Mako with Cherrypy on nginx through FastCGI

Posted by xuniluser on Server Fault See other posts from Server Fault or by xuniluser
Published on 2010-11-10T04:47:20Z Indexed on 2012/06/10 10:42 UTC
Read the original article Hit count: 397

Filed under:
|
|

I'm trying to use TemplateLookup from Mako, but can't seem to get it to work.

Layout of the test site is:

/var/www
    main.py
    templates/
       index.html

Nginx's config is setup as:

location / {
    fastcgi_pass 127.0.0.1:8080;
    fastcgi_param SERVER_NAME $server_name;
    fastcgi_param SERVER_PORT $server_port;
    fastcgi_param SERVER_PROTOCOL $server_protocol;
    fastcgi_param PATH_INFO $fastcgi_script_name;
    fastcgi_param REQUEST_METHOD $request_method;
    fastcgi_param QUERY_STRING $query_string;
    fastcgi_param CONTENT_TYPE $content_type;
    fastcgi_param CONTENT_LENGTH $content_length;
    fastcgi_pass_header Authorization;
    fastcgi_intercept_errors off;
}

Cherrypy's config has:

[global]
server.socket_port = 8080
server.thread_pool = 10
engine.autoreload_on = False
tools.sessions.on = True

A simple cherrypy setup in main.py seems to work fine.

import cherrypy
class Main:
    @cherrypy.expose
    def index(self):
        return 'Hello'

cherrypy.tree.mount(Main(), '/', config='config')

Now, if I modify this to use Mako's template lookup, I get a 500 error. I know it has something to do with serving static files, but I've tried over a dozen different configurations accoring to the cherrypy wiki, but none of them work.

Here's a bare setup I have for the templates:

import cherrypy
from mako.template import Template
from mako.lookup import TemplateLookup

templates = TemplateLookup(directories=['templates'], output_encoding='utf-8')

class Main:
    @cherrypy.expose
    def index(self):
        return templates.get_template('index.html').render(msg='hello')

cherrypy.tree.mount(Main(), '/', config='config')

Does anyone know how I can get this to work ?

© Server Fault or respective owner

Related posts about nginx

Related posts about fastcgi