Linux 处理来自 gunicorn 的请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12858674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Serving a request from gunicorn
提问by Chris Dutrow
Trying to setup a server on Rackspace.com.
尝试在 Rackspace.com 上设置服务器。
Have done the following things:
做了以下几件事:
- Installed Centos 6.3
- Installed Python 2.7
- Installed gunicorn using the "Quick Start" on their home page: gunicorn.org/
- 安装 Centos 6.3
- 已安装 Python 2.7
- 使用主页上的“快速入门”安装了 gunicorn:gunicorn.org/
In the quick start, a "hello world" application seems to be initialized:
在快速入门中,似乎初始化了一个“hello world”应用程序:
Create file "myapp.py":
创建文件“ myapp.py”:
(tutorial) $ vi myapp.py (tutorial) $ cat myapp.py
Contents of "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])
Since I know very little about servers, I do not know what to do next. I tried typing the server's IP address into the browser, but that seemed to result in a timeout.
由于我对服务器知之甚少,所以我不知道下一步该怎么做。我尝试在浏览器中输入服务器的 IP 地址,但这似乎导致超时。
I'm not sure if there is:
我不确定是否有:
- something else that needs to be installed. Nginx is mentioned under "deploy" on the gunicorn website. Looks like Nginx is a proxy server which is confusing to me because I thought gunicorn was a server. Not sure why I need two servers?
- something that needs to be configured in gunicorn
- something that needs to be configured on the server itself
- something that else entirely that needs to be done in order to actually serve a request
- 其他需要安装的东西。Gunicorn 网站上的“部署”下提到了 Nginx 。看起来 Nginx 是一个代理服务器,这让我感到困惑,因为我认为 gunicorn 是一个服务器。不知道为什么我需要两台服务器?
- 需要在gunicorn中配置的东西
- 需要在服务器本身上配置的东西
- 为了实际服务请求而完全需要做的其他事情
What are the next steps?
什么是下一步?
Thanks so much!
非常感谢!
采纳答案by SingleNegationElimination
looking at the quickstart guide, you probably should have run
查看快速入门指南,您可能应该运行
(tutorial) $ ../bin/gunicorn -w 4 myapp:app
which should have produced a line that looks a bit like:
这应该产生一条看起来有点像的线:
Listening at: http://127.0.0.1:8000
Among others. see if you can access your site at that address.
其中。看看您是否可以通过该地址访问您的网站。
Also Note that 127.0.0.1
is the loopback address; accessible only from that host itself. To get gunicorn to bind to a different option, pass it --bind 0.0.0.0:80
, as Jan-Philip suggests.
还要注意的127.0.0.1
是环回地址;只能从该主机本身访问。要让 gunicorn 绑定到不同的选项,请传递它--bind 0.0.0.0:80
,正如 Jan-Philip 建议的那样。
Since you mention rackspace, its possible that you may need to adjust the firewall settings to allow incoming connections to the desired ports.
由于您提到了机架空间,因此您可能需要调整防火墙设置以允许传入连接到所需端口。
回答by Dr. Jan-Philip Gehrcke
Looks like you do not have a web application developed so far. So, I assume that your goal for now is to set up a development environment. For the time being, develop your web application using the development web server included in most frameworks, e.g. Flask.
到目前为止,您似乎还没有开发 Web 应用程序。所以,我假设您现在的目标是建立一个开发环境。目前,使用大多数框架(例如 Flask)中包含的开发 Web 服务器来开发您的 Web 应用程序。
Whatever framework you are using, make the development web server listen on 0.0.0.0 so that the service is listening on all configured network interfaces and make sure that the port is open to the outside (check the Rackspace settings).
无论您使用什么框架,都要让开发 Web 服务器侦听 0.0.0.0,以便服务侦听所有配置的网络接口,并确保端口对外开放(检查 Rackspace 设置)。
When you are done developing your application or are looking into an existing one, you have to deploy it in a solid way. Then, gunicorn behind nginx is an option.
当您完成应用程序的开发或正在研究现有的应用程序时,您必须以可靠的方式部署它。那么,nginx 背后的 gunicorn 是一个选择。
I will roughly go through your questions. It looks you have to read a bit more :-)
我会粗略地回答你的问题。看起来你必须多读一点:-)
- Nginx is mentioned under "deploy" on the gunicorn website. Looks like Nginx is a proxy server which is confusing to me because I thought gunicorn was a server. Not sure why I need two servers?
- Gunicorn 网站上的“部署”下提到了 Nginx。看起来 Nginx 是一个代理服务器,这让我感到困惑,因为我认为 gunicorn 是一个服务器。不知道为什么我需要两台服务器?
Nginx is a full-featured web server. It is appreciated for its performance and stability. People use it to serve static files (to not burden a dynamic web application with this task), to forward requests to web applications whenever necessary, for SSL-termination, and for load-balancing. Note that this is an incomplete picture.
Nginx 是一个功能齐全的 Web 服务器。它因其性能和稳定性而受到赞赏。人们使用它来提供静态文件(不让动态 Web 应用程序承担此任务的负担),在必要时将请求转发到 Web 应用程序,用于 SSL 终止和负载平衡。请注意,这是一张不完整的图片。
gunicorn is a server for serving WSGI apps. Mainly, it manages worker processes that actually execute the web application.
gunicorn 是一个服务 WSGI 应用程序的服务器。主要是,它管理实际执行 Web 应用程序的工作进程。
- something that needs to be configured in gunicorn. something that needs to be configured on the server itself. something that else entirely that needs to be done in order to actually serve a request.
- 需要在gunicorn中配置的东西。需要在服务器本身上配置的东西。为了实际服务请求而完全需要做的其他事情。
Actually, you can optimize your linux box in endless ways (for performance, e.g. increase the file descriptor limit and for security). Within gunicorn, you can configure the number of worker processes and a lot more. If you have nginx as frontend or even another load balancer, this one has its own configuration. You see, your setup might become very complex for actual deployment in a real-world scenario. This is not trivial.
实际上,您可以以无穷无尽的方式优化您的 linux 机器(为了性能,例如增加文件描述符限制和为了安全)。在 gunicorn 中,您可以配置工作进程的数量等等。如果你有 nginx 作为前端,甚至是另一个负载均衡器,这个有它自己的配置。您会看到,对于实际场景中的实际部署,您的设置可能会变得非常复杂。这不是小事。
However, for playing around with a WSGI application, just set up your development framework properly, which is very simple in most cases, and make sure that there are no firewall issues. That's all.
但是,要玩 WSGI 应用程序,只需正确设置您的开发框架,这在大多数情况下非常简单,并确保没有防火墙问题。就这样。
回答by andrefsp
since gunicorn is a Web server on your case Nginx will act as a back proxy passing the an HTTP request from Nginx to gunicorn.
因为 gunicorn 是您案例中的 Web 服务器,Nginx 将充当后端代理,将来自 Nginx 的 HTTP 请求传递给 gunicorn。
So, I will put here the steps to take for a simple Nginx and Gunicorn configuration running on the same machine.
因此,我将在此处列出在同一台机器上运行的简单 Nginx 和 Gunicorn 配置所采取的步骤。
- Starting with nginx configuration
- 从nginx配置开始
Go to your /etc/nginx/nginx.confand under the http{} make sure you have: include /etc/nginx/site-enabled/*;
转到您的/etc/nginx/nginx.conf并在 http{} 下确保您有:include /etc/nginx/site-enabled/*;
http{
# other configurations (...)
include /etc/nginx/sites-enabled/*;
}
now, include a file on /etc/nginx/sites-enabled/mysite.conf where you will proxy your requests to your gunicorn app.
现在,在 /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
}
}
Ok, at this point all you have is an Nginx acting as a proxy where all the requests going to 127.0.0.1:80 will be passed to 127.0.0.1:8000.
好的,此时您所拥有的只是一个充当代理的 Nginx,所有发往 127.0.0.1:80 的请求都将传递到 127.0.0.1:8000。
- Now is time to configure your Gunicorn webserver:
- 现在是配置您的Gunicorn 网络服务器的时候了:
Usually the way I do I use a configuration file, Gunicorn config file can be an ordinary python file. So now, create a file at any location you like, I will assume this file will be /etc/gunicorn/mysite.py
通常我使用配置文件的方式,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
Ok, all set in configuration. Now all you have to do its to start the servers.
好的,所有设置都在配置中。现在你要做的就是启动服务器。
Starting the gunicorn and telling it which app to use and which config file. from the command line and the folder where your myapp.pyfile is located run:
启动 gunicorn 并告诉它使用哪个应用程序和哪个配置文件。从命令行和myapp.py文件所在的文件夹运行:
gunicorn -c /etc/gunicorn/mysite.py mysite:app
Now, only start nginx.
现在,只启动 nginx。
/etc/init.d/nginx start
or
或者
service nginx start
Hope this helps.
希望这可以帮助。