there have been many posts on running django on tornado with static media served by nginx. But for dumb people like me, the whole thing needs to be spelt out. So here is how I succeeded in serving django from a virtual host using nginx and tornado. The key thing to note is that 'root' refers to the parent directory of the root and not the full path. Also remember to put in ':' as a line end. Procedure - start the tornado server with the python script on localhost:8888, start nginx. Relax and enjoy your django at the speed of light. Nginx can be got by apt-get or yum, but you need the latest git clone of Tornado - the default tarball does not support django. btw, this install is for FC11 on my laptop - I have done it in production on lenny.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | # nginx.conf:
________________________________________________________
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log;
#error_log /var/log/nginx/error.log notice;
#error_log /var/log/nginx/error.log info;
pid /var/run/nginx.pid;
#----------------------------------------------------------------------
# Events Module
#
# http://wiki.nginx.org/NginxHttpEventsModule
#
#----------------------------------------------------------------------
events {
worker_connections 1024;
use epoll;
}
#----------------------------------------------------------------------
# HTTP Core Module
#
# http://wiki.nginx.org/NginxHttpCoreModule
#
#----------------------------------------------------------------------
http {
upstream tornadoserver {
server 127.0.0.1:8888;
server 127.0.0.1:8889;
}
include /etc/nginx/mime.types;
#include /etc/nginx/conf.d/virtual.conf;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
# Load config files from the /etc/nginx/conf.d directory
include /etc/nginx/conf.d/*.conf;
#
# The default server
#
server {
listen 80;
server_name xlquest.web;
#charset koi8-r;
#access_log logs/host.access.log main;
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
}
# /etc/nginx/conf.d/virtual.conf
__________________________________________________________________________________
server {
listen 80;
server_name ilugc.web;
location / {
proxy_pass http://127.0.0.1:8888;
}
location /sitemedia/ {
root /home/lawgon/ilugc/;
}
location /smedia/ {
root /home/lawgon/;
}
location /media/ {
root /home/lawgon/django-trunk/django/contrib/admin/;
}
}
server {
listen 80;
server_name conference.web;
location / {
proxy_pass http://127.0.0.1:8889/;
}
location /2009/ {
proxy_pass http://127.0.0.1:8889/;
}
location /sitemedia/ {
root /home/lawgon/conference/;
}
location /smedia/ {
root /home/lawgon/;
}
location /media/ {
root /home/lawgon/django-trunk/django/contrib/admin/;
}
}
# python script to run django on tornado
_____________________________________________________________________________
#! /usr/bin/env python
import os
import tornado.httpserver
import tornado.ioloop
import tornado.wsgi
import sys
import django.core.handlers.wsgi
sys.path.append('/home/lawgon/')
def main():
os.environ['DJANGO_SETTINGS_MODULE'] = 'ilugc.settings'
application = django.core.handlers.wsgi.WSGIHandler()
container = tornado.wsgi.WSGIContainer(application)
http_server = tornado.httpserver.HTTPServer(container)
http_server.listen(8888)
tornado.ioloop.IOLoop.instance().start()
if __name__ == "__main__":
main()
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
For what it's worth, Tornado isn't a bad media server at all. In this case, it might not be a terrible idea to just have Tornado serve all of it. Of course, nginx is faster.
#
well, could not figure out how to serve media with tornado, so did it with nginx - facebook people are doing the same.
#
I guess if your Django app is running on Apache (and Apache is serving static content), you are going to have a hell of a time trying to port it to Tornado and nginx. Especially if you are new to those technologies. I have been trying to use Tornado as my comet server, but it is terrible with jQuery integration. The self.write that Tornado does, isn't meant to return objects that jQuery understands.
#
Please login first before commenting.