Linux下Nginx+Tornado+Django配置

1.Nginx作为反代理,分发到后端的torndo服务器
2.Django作为framwork搭建到tornado服务器上,使用其admin功能

服务器版本信息:Ubuntu 12.04.5 LTS

Django项目运行在Tornado服务器上

在你的Django项目根目录下创建index.py文件
vi /path/to/project/index.py

import os
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
import tornado.wsgi
from django.core.wsgi import get_wsgi_application

define("port", default=8000, help="run on the given port", type=int)

def main():
    tornado.options.parse_command_line()
    os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings' # path to your settings module
    application = get_wsgi_application()
    container = tornado.wsgi.WSGIContainer(application)
    http_server = tornado.httpserver.HTTPServer(container, xheaders=True)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()

if __name__ == "__main__":
    main()

进程管理工具Supervisord

安装supervisor
pip install supervisor
生成 supervisor 默认配置文件,放在 /etc/supervisord.conf ,在端口8001-8004为Tornado开四个进程
vi /etc/supervisord.conf

[group:project]
programs=strategy

[program:project]
command=python /path/to/project/index.py -- port=80%(process_num)02d
directory=/path/to/project
process_name = %(program_name)s%(process_num)d
autorestart=true
redirect_stderr=true
stdout_logfile=/var/log/tornado.log
stdout_logfile_maxbytes=500MB
stdout_logfile_backups=50
stderr_logfile=/var/log/tornado.log
loglevel=info
numprocs = 4
numprocs_start = 1

初始启动Supervisordsupervisord,查看supervisor状态
supervisorctrl status,如果报错的话,查看错误原因
vi /var/log/tornado.log, 解决问题后,重启服务器
supervisorctrl restart project(注意换为你的group)或者
supervisorctrl restart all

Nginx

vi /etc/nginx/nginx.conf
或者在/etc/nginx/sites-enabled/下编辑可用站点

upstream tornadoes {
     server 127.0.0.1:8001;
     server 127.0.0.1:8001;
     server 127.0.0.1:8002;
     server 127.0.0.1:8004;
}

server {
    listen 8080;
    server_name www.project.com *.project.com;


    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://tornadoes;
}

重启Nginx服务器/etc/init.d/nginx restart或者service nginx restart

防火墙

允许本地所有端口访问-A INPUT -i lo -j ACCEPT
屏蔽外网访问8001~8004端口-A INPUT -p tcp --dport 8001:8004 -j REJECT

你可能感兴趣的:(Linux下Nginx+Tornado+Django配置)