本文介绍了使用Nginx反向代理在生产中设置龙卷风Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去几个月来,我一直在龙卷风中开发Web服务,并在测试环境中运行我使用的服务:

I have been developing a web service in tornado for the last few months, in my test environment to run the service I use:

python index.py

index.py是我的龙卷风应用程序处理程序,它侦听端口8001.然后,我使用http://localhost:8001向Web服务请求.我现在将测试环境部署到应该反映生产的过渡环境中.如何在生产中运行龙卷风?我猜我需要为该应用程序创建某种守护程序,但是我不知道从哪里开始!

index.py is my tornado application handler that listens on port 8001. I then request from the web service using http://localhost:8001. I am now deploying my test environment to a staging environment that should mirror production. How do I go about running tornado in production? I'm guessing I need to create some sort of daemon for the application but I have no idea where to start!

推荐答案

您可以使用一些工具.

首先, Supervisord

Supervisord是一个过程控制系统",您可以配置过程并让Supervisor对其进行管理,如果它们失败,它将重新启动它们,使它们的管理更加容易并使它们在后台运行

Supervisord is a "process control system", you configure your processes and let Supervisor manage them, it'll restart them if they fail, make managing them easier and keep them running in the background

这是示例主管配置文件

[program:myprogram] 
process_name=MYPROGRAM%(process_num)s
directory=/var/www/apps/myapp 
command=/var/www/apps/myapp/virtualenv/bin/python index.py --PORT=%(process_num)s
startsecs=2
user=youruser
stdout_logfile=/var/log/myapp/out-%(process_num)s.log
stderr_logfile=/var/log/myapp/err-%(process_num)s.log
numprocs=4
numprocs_start=14000

通过该配置,Supervisor将在端口14001-14004(numprocs_start)上启动4(numprocs)个index.py实例.我们通过--PORT=%(process_num)s来使每个进程在不同的端口上启动.您应该更改numprocsnumprocs_start以适合您的环境/设备.通常,我们运行2xCPU内核进程(因此一个四核处理器将有8个进程),但这会根据您的进程的工作以及代码中的阻塞程度而有很大差异.

With that config, Supervisor will start 4 (numprocs) instances of index.py on ports 14001-14004 (numprocs_start). We pass the --PORT=%(process_num)s to get each process to start on a different port. You should change numprocs and numprocs_start to suit your environment/equipment. Generally we run 2xCPU cores processes (so a quad core processor would have 8 processes) but that can vary hugely based on what your processes do and how much blocking there is in your code.

接下来,将NGINX配置为将请求转发到您的站点

Next, configure NGINX to forward requests to your site

   upstream myappbackend {
            server 127.0.0.1:14001  max_fails=3     fail_timeout=1s;
            server 127.0.0.1:14002  max_fails=3     fail_timeout=1s;
            server 127.0.0.1:14003  max_fails=3     fail_timeout=1s;
            server 127.0.0.1:14004  max_fails=3     fail_timeout=1s;
    }

    server {
            listen                                          4.5.6.7:80;
            server_name                                     example.com;

            access_log      /var/log/nginx/myapp.log  main;


            location / {
                    proxy_set_header                Host            $host;
                    proxy_set_header                X-Real-Ip       $remote_addr;
                    proxy_pass                      http://myappbackend/;
            }
    }      

该配置应根据您的应用程序及其工作方式进行修改,这是一个非常小的配置,几乎可以肯定需要进行扩展,但这足以使您入门

That config should be modified dependent on your application and the way it works, that is a very minimal configuration and will almost certainly need expanding on but that should be enough to get you started

这篇关于使用Nginx反向代理在生产中设置龙卷风Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 06:08