硬盘使用前需要格式化了Gunicorn或者uWSGI,为什么还需要Nginx

使用了Gunicorn或者uWSGI,为什么还需要Nginx? - 知乎171被浏览10844分享邀请回答722 条评论分享收藏感谢收起156 条评论分享收藏感谢收起查看更多回答使用gunicorn,supversiovr,nginx部署django应用
一、理解WSGI的工作方式
WSGI有两方:“Server”一方,“Application”一方。
一个常规的请求处理流程如下:
"Server"创建socket,监听端口,等待客户端连接。
2."Server"接收到用户的HTTP request请求。
3."Server"解析客户端信息放到环境变量environ中,解析http请求,将请求信息例如method,path等也放到environ中,然后再将一些服务器端信息也放到environ中,这样,服务器信息,客户端信息,本次请求信息全部都保存到了环境变量environ中。
4."Server"调用注册的“Application”,传递给“Application”的参数有两个:
a.第3步中包含所有请求信息的environ字典
b.一个start_response回调函数
5.“Application”处理请求,处理完毕后调用start_response回调函数,将HTTP
response headers/status 返回给"Server"。
6.“Application”将响应正文(response
body)作为返回值,返回给"Server"。
7."Server"通过socket将response信息返回给客户端。
在django中,创建项目时会自动创建一个wsgi.py,按照WSGI规范的叫法,这个wsgi.py就是一个"WSGI
application
callable"。在这个wsgi.py里边实现了一个名为"application"的函数,这个函数就是符合WSGI标准的一个HTTP处理函数,它接收两个参数:environ和start_response,即上边第四步中所说的一个包含所有HTTP请求信息的dict对象和一个发送HTTP响应的函数。
这个函数只有几行,真正执行的代码是django.core模块中的wsgi.py里边的get_wsgi_application()函数。
客户请求进来以后,这个application函数,就会被WSGI
Server调用。
在实际使用中,可能还会有"WSGI中间件"这个角色,它同时实现了API的两方,因此可以在WSGI服务和WSGI应用之间起调解作用:从WSGI服务器的角度来说,中间件扮演应用程序,而从应用程序的角度来说,中间件扮演服务器。“中间件”组件可以执行以下功能:
重写环境变量后,根据目标URL,将请求消息路由到不同的应用对象。
允许在一个进程中同时运行多个应用程序或应用框架。
负载均衡和远程处理,通过在网络上转发请求和响应消息。
进行内容后处理,例如应用XSLT样式表。
二、安装virtualenv,创建django项目
安装pip,virtualenv
install python-pip
install --upgrade pip
install virtualenv
创建virtualenv环境
假设我们的django应用都放在/home/webapps/目录下
/home/webapps
/home/webapps
virtualenv venv
source bin/activate
这里要特别提一句,virtualenv其实用在开发环境中的好处更明显,对于生产环境则是可选项。
如果有兴趣可以去看看activate脚本,其作用就是将你的venv/bin放到$PATH最前边。
所以,如果我们的机器是专用来运行django的,且系统默认的python版本合用,其实完全可以不需要virtualenv。
创建django项目,本例子中的名字叫做mysite
[root@demo webapps]# pip install django gunicorn
[root@demo webapps]# django-admin.py startproject mysite
修改django配置文件,主要修改以下几处,指定主机头,数据库连接,静态文件目录,这些根据自己需要修改即可。
[root@demo webapps]# vi mysite/settings.py
ALLOWED_HOSTS = ['your_server_domain_or_IP',
'second_domain_or_IP', . . .]
DATABASES = {
& & 'default':
& & 'ENGINE':
'django.db.backends.postgresql_psycopg2',
& & 'NAME':
'myproject',
& & 'USER':
'myprojectuser',
& & 'PASSWORD':
'password',
& & 'HOST':
'localhost',
& & 'PORT': '',
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
&# 将静态文件放到project的目录下,名字为static
[root@demo webapps]# cd /home/webapps
[root@demo webapps]# ./manage.py makemigrations
[root@demo webapps]# ./manage.py migrate
创建管理员用户
[root@demo webapps]# ./manage.py createsuperuser
将静态文件收集到指定的目录
[root@demo webapps]# ./manage.py collectstatic
使用django自身的Web
Server测试一下是否可用,如果可以访问,CRTL+C终止
[root@demo webapps]# ./manage.py runserver
0.0.0.0:8000
使用gunicorn测试项目是否可用,如果可以访问,CRTL+C终止
[root@demo webapps]# cd ~/mysite
[root@demo webapps]# gunicorn --bind 0.0.0.0:8000
mysite.wsgi:application
如果此时访问/admin会发现所有CSS样式消失了,这是因为我们还没有配置nginx来处理static文件,别紧张。
三、gunicorn的相关说明
django自带的webserver性能不足以用在生产环境,所以一般会使用其他WSGI
Server,比如uwsgi和gunicorn。
我们在此不讨论这些server间的优劣,只说明一下gunicorn如何使用。
根据第一部分的说明,WSGI中Server是通过wsgi.py中的application函数与django交互的。
所以gunicorn最简单的运行方式就是: gunicorn
mysite.wsgi:application
默认作为一个监听 127.0.0.1:8000 的web server,可以在本机通过http://127.0.0.1:8000
如果要通过网络访问,则需要绑定不同的地址(也可以同时设置监听端口):gunicorn -b
10.2.20.66:8080 mysite.wsgi:application
在多核服务器上,使用更多的gunicorn进程充分利用资源,下边命令启动8个进程同时处理HTTP请求:
-w 8 mysite.wsgi:application
默认使用同步阻塞的网络模型(-k sync),对于大并发的访问可能表现不够好,
它还支持其它更好的模式,比如:gevent或meinheld。
-k gevent mysite.wsgi:application
gunicorn支持使用配置文件,配置文件需要是合法的python源文件,但并不需要写成可以import的module。
只需要文件系统中可读取就行,也没有什么语法方面的特使要求,需要变量,定义就是了。
gunicorn_config.py
multiprocessing
path_of_current_file =
os.path.abspath(__file__)
path_of_current_dir =
os.path.split(path_of_current_file)[0]
_file_name =
os.path.basename(__file__)
sys.path.insert(0,
path_of_current_dir)
worker_class = 'gevent'
= multiprocessing.cpu_count() * 2 + 1
path_of_current_dir
worker_connections = 1000
max_requests = 2000
graceful_timeout = 30
"%s:%s" % ("0.0.0.0", 8000)
= '%s/run/%s.pid' % (path_of_current_dir, _file_name)
= '%s/logs/%s_error.log' % (path_of_current_dir,
_file_name)
accesslog = '%s/logs/%s_access.log' %
(path_of_current_dir, _file_name)
使用以上配置文件,会在gunicorn_config.py所在目录生成logs,run目录,gunicorn要对这些目录拥有写权限,所以这个配置文件放哪里,自己考虑。
四、使用supervisor运行gunicorn
因为gunicorn在运行中有可能会自己崩溃,我们需要一个机制来自动重启,之前写过一篇关于supervisor和tomcat的文档,所以在此不解释supervisor是什么,能干什么,怎么安装这些问题。只简单说明gunicorn的配置部分。
配置文件名字与项目保持一致,这样方便之后用ansible的模版管理。
/etc/supervisord.d/nameofyourprogram.ini
[program:nameofyourprogram]
directory = /path/of/your/project/
= /path/to/venv/bin/gunicorn nameofyourproject.wsgi:application -c
/path/of/your/project/gunicorn.conf.py
autostart=true
autorestart=true
redirect_stderr=true
实例如下:
[program:mysite]
directory = /home/webapps/venv
command = /home/webapps/venv/bin/gunicorn
mysite.wsgi:application -c gunicorn.conf.py
autostart=true
autorestart=true
redirect_stderr=true
如果你的venv中的python版本不同,你还需要在command那里加入python的绝对路径。
五、使用nginx作为反向代理,并直接serve静态文件
我们在第二步的时候,已经将所有静态文件收集到/home/webapps/static目录中。
在nginx配置如下:
& & listen
& & charset
& & access_log
/home/webapps/mysite/access.
& & error_log
/home/webapps/mysite/error.
& & location
/static/ {
/home/webapps/static/;
& & access_
& & location /
& & proxy_pass
http://127.0.0.1:8000;
& & proxy_set_header Host
& & proxy_set_header X-Real-IP
& & proxy_set_header
X-Forwarded-For $proxy_add_x_forwarded_
这样所有静态文件请求就被nginx处理了,动态请求会转到后端的django项目中。
关于静态文件处理,还有django-pipeline,可以做js,css等的合并和压缩,有需要的去看官方文档。
参考文档:
/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04
/community/tutorials/how-to-deploy-python-wsgi-apps-using-gunicorn-http-server-behind-nginx
/Atem18/4696071
https://blog.teststation.org/centos/python//installing-python-virtualenv-centos-7/
/questions/6532486/supervising-virtualenv-django-app-via-supervisor
http://www.rkblog.rk.edu.pl/w/p/continuous-integration-django-projects-jenkins/
http://matthewdaly.co.uk/blog//django-blog-tutorial-the-next-generation-part-5/
/site/kmmbvnr/home/django-jenkins-tutorial
https://media.readthedocs.org/pdf/django-jenkins/latest/django-jenkins.pdf
http://fishi.devtail.io/weblog//setting-django-ci-jenkins-and-git/
/post-228.html
http://michal.karzynski.pl/blog//django-nginx-gunicorn-virtualenv-supervisor/
http://honza.ca/2011/05/deploying-django-with-nginx-and-gunicorn
/wswang/p/5519101.html
/en/dev/howto/static-files
/blog/2013/08/minimal-nginx-and-gunicorn-configuration-for-djang/
http://django-pipeline.readthedocs.io/en/latest/configuration.html
http://rfyiamcool./6364?utm_source=tuicool&utm_medium=referral
http://wsgi.tutorial.codepoint.net/application-interface
http://www.nowamagic.net/academy/detail/1330310
/wiki/095c955c1e6d8bbfac00/740b04430a98f614b6da89da2157ea3efe2000
/mayan-edms/mayan-edms/blob/972aacdd4cc4d7119a39c8b871fe7d5/contrib/gunicorn/gunicorn.conf.py
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。在 SegmentFault,解决技术问题
每个月,我们帮助 1000 万的开发者解决各种各样的技术问题。并助力他们在技术能力、职业生涯、影响力上获得提升。
一线的工程师、著名开源项目的作者们,都在这里:
获取验证码
已有账号?
问题对人有帮助,内容完整,我也想知道答案
问题没有实际价值,缺少关键内容,没有改进余地
线上环境部署Django,nginx+uwsgi 和nginx+gunicorn,这两种方案,应该如何选择?
大家是采用的何种部署方式?
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
用的nginx+gunicorn方式,uwsgi没用过所以没法对比,就gunicorn的感受也来讲已经很快了,nginx处理掉了几乎全部的静态文件请求,实际上需要gunicorn再来处理的请求已经很少了。gunicorn可以用Python文件直接配置,试用起来比较舒服。
分享到微博?
关闭理由:
删除理由:
忽略理由:
推广(招聘、广告、SEO 等)方面的内容
与已有问题重复(请编辑该提问指向已有相同问题)
答非所问,不符合答题要求
宜作评论而非答案
带有人身攻击、辱骂、仇恨等违反条款的内容
无法获得确切结果的问题
非开发直接相关的问题
非技术提问的讨论型问题
其他原因(请补充说明)
我要该,理由是:&&7208 阅读
在这篇文章里,我们将搭建一个简单的 Web 应用,在虚拟环境中基于 Flask 框架,用 Gunicorn 做 wsgi 容器,用 Supervisor 管理进程,然后使用 OneAPM
探针来监测应用性能,形成一个「闭环」 !希望能对大家有所帮助,首先简单来介绍一下环境:
系统环境:ubuntu 14.04
Python 2.7.6
安装组件库
第一步安装所需要的存储库,因为打算用到虚拟环境,用到 pip 安装和管理 Python 组件,所以先更新本地包,然后安装组件:
sudo apt-get update
sudo apt-get install python-pip python-dev nginx
创建虚拟环境 virtualenv
在一个系统中创建不同的 Python 隔离环境,相互之间还不会影响,为了使系统保持干净,遂决定用 virtualenv 跑应用程序,创建一个容易识别的目录,开始安装,再创建项目目录 super,然后激活环境:
sudo pip install virtualenv
mkdir ~/supervisor && cd ~/supervisor
virtualenv super
source super/bin/activate
安装 Flask 框架
好了,现在在虚拟环境里面,开始安装 Flask 框架,flask 依赖两个库 werkzeug 和 jinjia2, 采用 pip 方式安装即可, pip 是一个重要的工具,Python 用它来管理包:
pip install flask
先用 Flask 写一个简单的 Web 服务 myweb.py ,因为后面要做一些测试,所以设置了几个请求:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return 'hello world
supervisor gunicorn '
@app.route('/1')
def index1():
return 'hello world
supervisor gunicorn
ffffff'
@app.route('/qw/1')
def indexqw():
return 'hello world
supervisor gunicorn fdfdfbdfbfb '
if __name__ == '__main__':
app.debug = True
启动 Flask 看看!
python myweb.py
在浏览器中访问 http://127.0.0.1:5000 就可以看到了「几个路径都试一试」
用 Gunicorn 部署 Python Web
现在我们使用 Flask 自带的服务器,完成了 Web 服务的启动。生产环境下,Flask 自带的服务器,无法满足性能要求。所以我们这里采用 Gunicorn 做 wsgi 容器,用来部署 Python,首先还是安装 Gunicorn:
pip install gunicorn
当我们安装好 Gunicorn 之后,需要用 Gunicorn 启动 Flask,Flask 用自带的服务器启动时,Flask 里面的 name 里面的代码启动了 app.run()。而这里我们使用 Gunicorn,myweb.py 就等同于一个库文件,被 Gunicorn 调用,这样启动:
gunicorn -w 4 -b 0.0.0.0:8000 myweb:app
其中 myweb 就是指 myweb.py,app 就是那个 wsgifunc 的名字,这样运行监听 8000 端口,原先的 5000 端口并没有启用,-w 表示开启多少个 worker,-b 表示 Gunicorn 开发的访问地址。
想要结束 Gunicorn 只需执行 pkill Gunicorn,但有时候还要 ps 找到 pid 进程号才能 kill。可是这对于一个开发来说,太过于繁琐,因此出现了另外一个神器 ---supervisor,一个专门用来管理进程的工具,还可以管理系统的工具进程。
安装 Supervisor
pip install supervisor
echo_supervisord_conf & supervisor.conf
# 生成 supervisor 默认配置文件
supervisor.conf
# 修改 supervisor 配置文件,添加 gunicorn 进程管理
在 supervisor.conf 底部下添加 myweb.py 的配置 `/home/wang/supervisor/super` 是我的项目目录」
[program:myweb]
command=/home/wang/supervisor/super/bin/gunicorn -w 4 -b 0.0.0.0:8000 myweb:app
directory=/home/wang/supervisor
startsecs=0
stopwaitsecs=0
autostart=false
autorestart=false
stdout_logfile=/home/wang/supervisor/log/gunicorn.log
stderr_logfile=/home/wang/supervisor/log/gunicorn.err
supervisor 的基本使用命令:
supervisord -c supervisor.conf
supervisorctl -c supervisor.conf status
查看supervisor的状态
supervisorctl -c supervisor.conf reload
重新载入 配置文件
supervisorctl -c supervisor.conf start [all]|[appname]
启动指定/所有 supervisor 管理的程序进程
supervisorctl -c supervisor.conf stop [all]|[appname]
关闭指定/所有 supervisor 管理的程序进程
supervisor 还有一个 web 的管理界面,可以激活。更改下配置:
[inet_http_server] inet (TCP) server disabled by default
port=127.0.0.1:9001 (ip_address:port specifier, *:port for alliface)
username= (default is no username (open server)
password=123 (default is no password (open server))
[supervisorctl]
serverurl=unix:///tmp/supervisor. use a unix:// URL
for a unix socket
serverurl=http://127.0.0.1:9001 use an http:// url to specify an inet socket
username=wang
should be same as http_username if set
password=123
should be same as http_password if set
;prompt=mysupervis cmd line prompt (default &supervisor&)
;history_file=~/.sc_ use readline history if available
现在可以使用 supervsior 启动 gunicorn 啦。运行命令:
supervisord -c supervisor.conf
浏览器访问 http://127.0.0.1:9001 可以得到 supervisor 的 web 管理界面,访问 http://127.0.0.1:8000 可以看见 gunicorn 启动的返回的页面。
配置 Nginx
前面我们已经在系统环境下安装了 Nginx, 安装好的 Nginx 二进制文件放在 /usr/sbin/ 文件夹下,接下来使用 Supervisor 来管理 Nginx。这里需要注意一个问题,权限问题。Nginx 是 sudo 的方式安装,启动的适合也是 root 用户,那么我们现在也需要用 root 用户启动 supervisor。在 supervisor.conf 下添加配置文件:
[program:nginx]
command=/usr/sbin/nginx
startsecs=0
stopwaitsecs=0
autostart=false
autorestart=false
stdout_logfile=/home/wang/supervisor/log/nginx.log
stderr_logfile=/home/wang/supervisor/log/nginx.err
好了,都配置完之后,启动 supervisor:
supervisord -c supervisor.conf
访问页面,也可以用 ab 进行压力测试:
ab -c 100 -n 100 http://127.0.0.1:8000/qw/1
-c 用于指定压力测试的并发数, -n 用于指定压力测试总共的执行次数。
安装 Python 探针
搭建好了 web,想实时监控应用数据,有什么好的工具,用 OneAPM 的 Python 探针试试,
首先也是安装 Python 探针:
pip install -i /simple --upgrade blueware
根据 License Key 生成配置文件:
blueware-admin generate-config (License Key) = blueware.ini
由于是在虚拟环境下,所以要特别注意路径,修改 supervisor.conf 里面两项:
[program:myapp]
command = /home/wang/supervisor/super/bin/blueware-admin run-program /home/wang/supervisor/super/bin/gunicorn -w 4 -b 0.0.0.0:8000 myapp:app
environment = BLUEWARE_CONFIG_FILE=blueware.ini
supervisorctl
# 进入命令行
supervisor&
# 重新加载
向上面一样访问页面,也可以用 ab 进行压力测试
几分钟后有下图,可以看到页面加载时间,web 事物,页面吞吐量,其中后面是设置自定义事物「Business Transaction」。
参考文章:
工程师编译整理。OneAPM 是领域的新兴领军企业,能帮助企业用户和开发者轻松实现:缓慢的程序代码和 SQL 语句的实时抓取。想阅读更多技术文章,请访问 OneAPM 。

我要回帖

更多关于 使用硝基苯 需要 的文章

 

随机推荐