Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为gunicorn的请求提供服务_Python_Linux_Centos_Wsgi_Gunicorn - Fatal编程技术网

Python 为gunicorn的请求提供服务

Python 为gunicorn的请求提供服务,python,linux,centos,wsgi,gunicorn,Python,Linux,Centos,Wsgi,Gunicorn,正在尝试在Rackspace.com上设置服务器 我们做了以下几件事: 已安装Centos 6.3 已安装Python2.7 使用主页上的“快速启动”安装gunicorn: 在快速入门中,“hello world”应用程序似乎已初始化: 创建文件“myapp.py”: 因为我对服务器知之甚少,所以我不知道下一步该做什么。我试图在浏览器中输入服务器的IP地址,但这似乎导致超时 我不确定是否有: 其他需要安装的东西。Nginx在gunicorn网站的“”下提到。看起来Nginx是一个代理服务器

正在尝试在Rackspace.com上设置服务器

我们做了以下几件事:

  • 已安装Centos 6.3
  • 已安装Python2.7
  • 使用主页上的“快速启动”安装gunicorn:
在快速入门中,“hello world”应用程序似乎已初始化:

创建文件“myapp.py”:

因为我对服务器知之甚少,所以我不知道下一步该做什么。我试图在浏览器中输入服务器的IP地址,但这似乎导致超时

我不确定是否有:

  • 其他需要安装的东西。Nginx在gunicorn网站的“”下提到。看起来Nginx是一个代理服务器,这让我很困惑,因为我认为gunicorn是一个服务器。不知道为什么我需要两台服务器
  • 需要在gunicorn中配置的内容
  • 需要在服务器本身上配置的内容
  • 为了实际满足请求而需要做的其他事情
接下来的步骤是什么


非常感谢

看起来您目前还没有开发web应用程序。所以,我假设您现在的目标是建立一个开发环境。目前,使用大多数框架(如Flask)中包含的开发web服务器开发web应用程序

无论您使用什么框架,都要让开发web服务器侦听0.0.0.0,以便服务侦听所有配置的网络接口,并确保端口对外开放(检查机架空间设置)。

当您开发完应用程序或正在研究现有应用程序时,您必须以可靠的方式部署它。那么,nginx背后的gunicorn就是一个选项

我将大致回顾一下你们的问题。看来你得多读一点:-)

  • Nginx在gunicorn网站的“deploy”下提到。看起来Nginx是一个代理服务器,这让我很困惑,因为我认为gunicorn是一个服务器。不知道为什么我需要两台服务器
Nginx是一个功能齐全的web服务器。它因其性能和稳定性而受到赞赏。人们使用它来服务静态文件(不让动态web应用程序承担此任务)、在必要时将请求转发给web应用程序、SSL终止和负载平衡。请注意,这是一张不完整的图片

gunicorn是为WSGI应用提供服务的服务器。它主要管理实际执行web应用程序的工作进程

  • 需要在gunicorn中配置的内容。 需要在服务器本身上配置的内容。 为了实际满足一个请求而需要做的事情
事实上,您可以通过各种方式优化linux机箱(为了性能,例如增加文件描述符限制和安全性)。在gunicorn中,您可以配置工作进程的数量以及更多。如果您将nginx作为前端或甚至是另一个负载平衡器,那么这个有自己的配置。您知道,在现实场景中,对于实际部署,您的设置可能会变得非常复杂。这不是小事


然而,为了使用WSGI应用程序,只需正确地设置开发框架(在大多数情况下非常简单),并确保没有防火墙问题。仅此而已。

因为gunicorn是您案例中的一个Web服务器,Nginx将充当后台代理,将HTTP请求从Nginx传递给gunicorn

因此,我将在这里介绍在同一台机器上运行的简单Nginx和Gunicorn配置的步骤

  • 从nginx配置开始
转到您的/etc/nginx/nginx.conf并在http{}下确保:include/etc/nginx/site enabled/*

http{
    # other configurations (...)
    include /etc/nginx/sites-enabled/*;
}
现在,在/etc/nginx/sites enabled/mysite.conf上包含一个文件,您将在其中将请求代理到gunicorn应用程序

server {
    listen 80 default; # this means nginx will be 
                       # listening requests on port 80 and 
                       # this will be the default nginx server
    server_name localhost;

    # declare proxy params and values to forward to your gunicorn webserver
    proxy_pass_request_headers on;
    proxy_pass_request_body on;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_read_timeout 120s;

    location / {
        # here is where you declare that every request to / 
        # should be proxy to 127.0.0.1:8000 (which is where
        # your gunicorn will be running on)          
        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_connect_timeout 10;
        proxy_read_timeout 10;

        proxy_pass http://127.0.0.1:8000/; # the actual nginx directive to 
                                           # forward the request
    }
}
好的,在这一点上,您所拥有的只是一个作为代理的Nginx,其中所有到127.0.0.1:80的请求都将传递到127.0.0.1:8000

  • 现在是配置Gunicorn Web服务器的时候了:
通常我使用配置文件的方式是,Gunicorn配置文件可以是普通python文件。现在,在您喜欢的任何位置创建一个文件,我假设这个文件是/etc/gunicorn/mysite.py

def app(environ, start_response):
   data = "Hello, World!\n"
   start_response("200 OK", [
       ("Content-Type", "text/plain"),
       ("Content-Length", str(len(data)))
   ])
   return iter([data])
workers = 3              # number of workers Gunicorn will spawn 

bind = '127.0.0.1:8000'  # this is where you declare on which address your 
                         # gunicorn app is running.
                         # Basically where Nginx will forward the request to

pidfile = '/var/run/gunicorn/mysite.pid' # create a simple pid file for gunicorn. 

user = 'user'          # the user gunicorn will run on

daemon = True          # this is only to tell gunicorn to deamonize the server process

errorlog = '/var/log/gunicorn/error-mysite.log'    # error log

accesslog = '/var/log/gunicorn/access-mysite.log'  # access log

proc_name = 'gunicorn-mysite'            # the gunicorn process name
好的,全部设置在配置中。现在你所要做的就是启动服务器

启动gunicorn并告诉它使用哪个应用程序和哪个配置文件。 从命令行和myapp.py文件所在的文件夹运行:

gunicorn -c /etc/gunicorn/mysite.py mysite:app
现在,只启动nginx

/etc/init.d/nginx start 


希望这能有所帮助。

查看快速入门指南,您可能应该运行

(tutorial) $ ../bin/gunicorn -w 4 myapp:app
这应该会产生一条线,看起来有点像:

Listening at: http://127.0.0.1:8000
除其他外。看看你是否能在那个地址访问你的网站

还要注意,
127.0.0.1
是环回地址;只能从该主机本身访问。要让gunicorn绑定到另一个选项,请按照Jan Philip的建议,传递它
--bind 0.0.0.0:80

由于您提到了机架空间,因此可能需要调整防火墙设置,以允许到所需端口的传入连接

(tutorial) $ ../bin/gunicorn -w 4 myapp:app
Listening at: http://127.0.0.1:8000