Skip to content

Instantly share code, notes, and snippets.

@mignev
Created September 12, 2013 13:19
Show Gist options
  • Save mignev/6537140 to your computer and use it in GitHub Desktop.
Save mignev/6537140 to your computer and use it in GitHub Desktop.
Django deployment with Nginx and Tornado Web
upstream tornadoFrontends {
server 127.0.0.1:8000;
server 127.0.0.1:8001;
server 127.0.0.1:8002;
server 127.0.0.1:8003;
}
server {
listen 80;
server_name example.com;
# Allow file uploads
client_max_body_size 50M;
location ^~ /static/ {
root /home/example.com/app_dir;
if ($query_string) {
expires max;
}
}
location = /favicon.ico {
rewrite (.*) /static/favicon.ico;
}
location = /robots.txt {
rewrite (.*) /static/robots.txt;
}
location / {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_pass http://tornadoFrontends;
}
}
user www-data;
worker_processes 1;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
# multi_accept on;
use epoll;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
keepalive_timeout 65;
proxy_read_timeout 200;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
gzip on;
gzip_min_length 1000;
gzip_proxied any;
gzip_types text/plain text/html text/css text/xml
application/x-javascript application/xml
application/atom+xml text/javascript;
# Only retry if there was a communication error, not a timeout
# on the Tornado server (to avoid propagating "queries of death"
# to all frontends)
proxy_next_upstream error;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
#/usr/bin/env python
# Run this with
# Original file: https://github.com/bdarnell/django-tornado-demo/blob/master/testsite/tornado_main.py
# PYTHONPATH=. DJANGO_SETTINGS_MODULE=testsite.settings testsite/tornado_main.py
# Serves by default at
# http://localhost:8080/hello-tornado and
# http://localhost:8080/hello-django
from tornado.options import options, define, parse_command_line
import django.core.handlers.wsgi
import tornado.httpserver
import tornado.ioloop
import tornado.web
import tornado.wsgi
import daemon
import os, sys
SITE_ROOT = os.path.dirname(os.getcwd())
PROJECT_NAME = os.path.basename(os.getcwd())
sys.path.append( SITE_ROOT )
os.environ['DJANGO_SETTINGS_MODULE'] = PROJECT_NAME + '.settings'
define('port', type=int, default=8080)
class HelloHandler(tornado.web.RequestHandler):
def get(self):
self.write('Hello from tornado')
def main():
tornado.options.parse_command_line()
wsgi_app = tornado.wsgi.WSGIContainer(
django.core.handlers.wsgi.WSGIHandler())
tornado_app = tornado.web.Application(
[
('/hello-tornado', HelloHandler),
('.*', tornado.web.FallbackHandler, dict(fallback=wsgi_app)),
])
server = tornado.httpserver.HTTPServer(tornado_app)
server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
if __name__ == '__main__':
with daemon.DaemonContext():
main()
#!/bin/bash
serverName=$(ls|grep 'Server.py')
tornadoPorts='8000 8001 8002 8003'
function start {
for port in $tornadoPorts
do
python $serverName --port=$port;
echo "Server: $serverName with port $port was Started!";
done
exit 1
}
function stop {
TORNADOS=$(ps ax |grep $serverName |grep -v grep| awk -F ' ' '{print $1}')
for pid in $TORNADOS
do
echo "Killing pid: $pid"
kill -9 $pid;
done
}
case "$1" in
start)
if ( start ); then
exit 0
else
exit 0;
fi
;;
stop)
stop
;;
restart)
stop
if ( start ); then
exit 0
else
exit 0;
fi
;;
*)
echo $"Usage: $0 {start|stop|restart}"
exit 1
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment