tornado之supervisor nginx部署

tornado之supervisor nginx部署

  • 本文主要介绍tornado基于supervisor和nginx的部署问题

nginx tornado反向代理配置

  • 部署的模型图如下
    tornado之supervisor nginx部署_第1张图片

supervisor安装

  • supervisor入门地址
http://blog.csdn.net/ricky110/article/details/77430387
  • 配置(新建tornado.conf)
[group:tornadoes]
programs=tornado-7000,tornado-7001,tornado-7002,tornado-7003

[program:tornado-7000]
command=python /home/wuyong/tornado_learn/hello_module.py --port=7000
directory=/var/www
user=www-data
autorestart=true
redirect_stderr=true
stdout_logfile=/var/log/tornado.log
loglevel=info

[program:tornado-7001]
command=python /home/wuyong/tornado_learn/hello_module.py --port=7001
directory=/var/www
user=www-data
autorestart=true
redirect_stderr=true
stdout_logfile=/var/log/tornado.log
loglevel=info

[program:tornado-7002]
command=python /home/wuyong/tornado_learn/hello_module.py --port=7002
directory=/var/www
user=www-data
autorestart=true
redirect_stderr=true
stdout_logfile=/var/log/tornado.log
loglevel=info

[program:tornado-7003]
command=python /home/wuyong/tornado_learn/hello_module.py --port=7003
directory=/var/www
user=www-data
autorestart=true
redirect_stderr=true
stdout_logfile=/var/log/tornado.log
loglevel=info
  • 修改supervisor的inet_http_server
    inet_http_server
  • 被测试的服务脚本
import tornado.web

from tornado.options import define, options


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


class IndexHandler(tornado.web.RequestHandler):
    def get(self):
        greeting = self.get_argument('greeting', 'Hello')
        self.write(greeting + ', friendly user!')


if __name__ == "__main__":

    tornado.options.parse_command_line()
    app = tornado.web.Application(handlers=[(r"/", IndexHandler)])
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()
  • 测试验证supervisor是否正确
    tornado之supervisor nginx部署_第2张图片
  • 页面测试
    tornado之supervisor nginx部署_第3张图片
    以上说明验证成功

nginx搭建

  • nginx安装 apt-get install nginx

  • 修改配置 cd /etc/nginx && vim tornado_nginx.conf加入以下配置,将后端上游服务upstream指定好

proxy_next_upstream error;

upstream tornadoes {
    server 127.0.0.1:7000;
    server 127.0.0.1:7001;
    server 127.0.0.1:7002;
    server 127.0.0.1:7003;
}

server {
    listen 81;

    location /static/ {
        root /var/www/static;
        if ($query_string) {
            expires max;
        }
    }


    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;
    }
}
  • 运行
    /usr/sbin/nginx

  • 验证成功,查看日志及页面访问
    tornado之supervisor nginx部署_第4张图片

https

  • nginx ssl解密配置
server {
    listen 443;
    ssl on;
    ssl_certificate /path/to/cert.pem;
    ssl_certificate_key /path/to/cert.key;

    default_type application/octet-stream;

    location /static/ {
        root /var/www/static;
        if ($query_string) {
            expires max;
        }
    }

    location = /favicon.ico {
        rewrite (.*) /static/favicon.ico;
    }

    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 转发http到https请求
server {
    listen 80;
    server_name example.com;

    rewrite /(.*) https://$http_host/$1 redirect;
}

你可能感兴趣的:(tornado)