提供gunicorn的请求

Chr*_*row 16 python linux wsgi centos gunicorn

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

做了以下事情:

  • 已安装Centos 6.3
  • 安装了Python 2.7
  • 使用主页上的"快速入门"安装了gunicorn:gunicorn.org/

在快速入门中,似乎初始化了一个"hello world"应用程序:

创建文件" myapp.py ":

(tutorial) $ vi myapp.py
(tutorial) $ cat myapp.py

" myapp.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])
Run Code Online (Sandbox Code Playgroud)

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

我不确定是否有:

  • 还需要安装其他东西.在gunicorn网站上的" 部署 " 下提到了Nginx .看起来像Nginx是一个令我困惑的代理服务器,因为我认为gunicorn是一个服务器.不知道为什么我需要两台服务器?
  • 需要在gunicorn中配置的东西
  • 需要在服务器上配置的东西
  • 为了实际提供请求而需要完成的其他事情

什么是下一个步骤?

非常感谢!

and*_*fsp 33

因为gunicorn是你服务器上的一个Web服务器,Nginx将作为一个后代理,将来自Nginx的HTTP请求传递给gunicorn.

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

  • 从nginx配置开始

转到你的/etc/nginx/nginx.conf并在http {}下面确保你有:include/etc/nginx/site-enabled/*;

http{
    # other configurations (...)
    include /etc/nginx/sites-enabled/*;
}
Run Code Online (Sandbox Code Playgroud)

现在,在/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
    }
}
Run Code Online (Sandbox Code Playgroud)

好的,此时您所拥有的只是一个Nginx充当代理,其中所有发送到127.0.0.1:80的请求将被传递到127.0.0.1:8000.

  • 现在是时候配置你的Gunicorn网络服务器了:

通常我使用配置文件的方式,Gunicorn配置文件可以是一个普通的python文件.所以现在,在你喜欢的任何地方创建一个文件,我会假设这个文件是/etc/gunicorn/mysite.py

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
Run Code Online (Sandbox Code Playgroud)

好的,所有设置都在配置中.现在你需要做的就是启动服务器了.

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

gunicorn -c /etc/gunicorn/mysite.py mysite:app
Run Code Online (Sandbox Code Playgroud)

现在,只启动nginx.

/etc/init.d/nginx start 
Run Code Online (Sandbox Code Playgroud)

要么

service nginx start
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.

  • 这值得+5 (6认同)

Sin*_*ion 18

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

(tutorial) $ ../bin/gunicorn -w 4 myapp:app
Run Code Online (Sandbox Code Playgroud)

应该产生一条看起来有点像的线:

Listening at: http://127.0.0.1:8000
Run Code Online (Sandbox Code Playgroud)

其中包括.看看你是否可以通过该地址访问您的网站.

另请注意,这127.0.0.1是环回地址; 只能从该主机本身访问.--bind 0.0.0.0:80正如Jan-Philip建议的那样,要让gunicorn绑定到不同的选项,通过它.

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


Jan*_*cke 6

看起来你到目前为止还没有开发过Web应用程序.所以,我认为你现在的目标是建立一个开发环境.目前,使用大多数框架中包含的开发Web服务器(例如Flask)开发Web应用程序.

无论您使用何种框架,都要使开发Web服务器侦听0.0.0.0,以便服务正在侦听所有已配置的网络接口,并确保该端口对外开放(检查Rackspace设置).

完成应用程序的开发或查看现有应用程序后,必须以可靠的方式部署它.然后,nginx背后的gunicorn是一个选项.

我会粗略地回答你的问题.它看起来你必须阅读更多:-)

  • 在gunicorn网站上的"部署"下提到了Nginx.看起来像Nginx是一个令我困惑的代理服务器,因为我认为gunicorn是一个服务器.不知道为什么我需要两台服务器?

Nginx是一个功能齐全的Web服务器.它的性能和稳定性令人赞赏.人们使用它来提供静态文件(不会给使用此任务的动态Web应用程序带来负担),在必要时将请求转发给Web应用程序,用于SSL终止和负载平衡.请注意,这是一张不完整的图片.

gunicorn是一个服务WSGI应用程序的服务器.主要是,它管理实际执行Web应用程序的工作进程.

  • 需要在gunicorn中配置的东西.需要在服务器上配置的东西.为了实际提供请求而需要完成的其他事情.

实际上,您可以以无限的方式优化您的Linux盒子(为了提高性能,例如增加文件描述符限制和安全性).在gunicorn中,您可以配置工作进程的数量等等.如果你有nginx作为前端或甚至另一个负载均衡器,这个有自己的配置.您会发现,在实际场景中,您的设置可能会变得非常复杂.这不是微不足道的.

但是,要使用WSGI应用程序,只需正确设置开发框架,这在大多数情况下非常简单,并确保没有防火墙问题.就这样.