Gunicorn部署部分的翻译
部署Gunicorn
文档建议Gunicorn最好是用在代理服务器后面。(等于前面最好加一个反向代理)
Nginx Configuration
文档建议用Nginx,当然用其他也可以,但是要确保当你用Gunicorn默认的worker时,那个代理能够减缓(安排好)客户端的访问,不然很有可能会导致拒绝服务。文档建议用Hey(由GO编写的一个库)来检验这个代理是否有用。
文档提供了一个关于Nginx的配置例子:
worker_processes 1; # 设置worker进程数量
user nobody nogroup; # 设置了运行用户,和运行用户组
# 这个一定要设置好,不然读取静态文件的时候会报403
# 'user nobody nobody;' for systems with 'nobody' as a group instead
pid /tmp/nginx.pid; # 设置pid文件地址
error_log /tmp/nginx.error.log; # 设置错误日志的地址
events {
worker_connections 1024; # increase if you have lots of clients
accept_mutex off; # set to 'on' if nginx worker_processes > 1
# 'use epoll;' to enable for Linux 2.6+
# 'use kqueue;' to enable for FreeBSD, OSX
}
http {
include mime.types;# mime 多用途因特网邮件扩展类型?
# fallback in case we can't determine a type
default_type application/octet-stream;
access_log /tmp/nginx.access.log combined;
sendfile on;
upstream app_server {
# fail_timeout=0 means we always retry an upstream even if it failed
# to return a good HTTP response
# for UNIX domain socket setups
server unix:/tmp/gunicorn.sock fail_timeout=0;
# for a TCP configuration
# server 192.168.0.7:8000 fail_timeout=0;
}
server {
# if no Host match, close the connection to prevent host spoofing
listen 80 default_server;
return 444;
}
server {
# use 'listen 80 deferred;' for Linux
# use 'listen 80 accept_filter=httpready;' for FreeBSD
listen 80;
client_max_body_size 4G;
# set the correct host(s) for your site
server_name example.com www.example.com;
keepalive_timeout 5;
# path for static files
root /path/to/app/current/public;
location / {
# checks for static file, if not found proxy to app
try_files $uri @proxy_to_app;
}
location @proxy_to_app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# enable this if and only if you use HTTPS
# proxy_set_header X-Forwarded-Proto https;
proxy_set_header Host $http_host;
# we don't want nginx trying to do something clever with
# redirects, we set the Host: header above already.
proxy_redirect off;
proxy_pass http://app_server;
}
error_page 500 502 503 504 /500.html;
location = /500.html {
root /path/to/app/current/public;
}
}
}
如果需要处理stream(流)请求/响应,或者其他特别的,如comet(服务器推),long polling(长轮询),web sockets(网络套接字),则需要关闭代理缓存功能。当然要执行这些的话,是需要用异步worker的。
设置的方法为,可以类似在在上面的配置的66行左右插入
proxy_buffering off;。
当Nginx去处理ssl请求的时候,是需要把相应协议的信息传给Gunicorn。很多web框架是需要用到相关的信息来生成对应的URL,如果没有这些信息,则有可能在一个https的响应里 生成一个http的URL,这很可能会导致不好的事情发送,所以要对Nginx进行设置,让它可以传递适当的头部信息。
设置的方法,类似在上面的66行左右插入
proxy_set_header X-Forwarded-Proto $scheme;
如果Nginx和Gunicorn不在同一台机器上,则需要告诉Gunicorn要相信从Nginx传过来的一些头部信息,因为在默认配置下,为了防止恶意的攻击者伪造请求,Gunicorn只相信那些本地连接传过来的头部信息。
设置信赖的安全地址
gunicorn --forwarded-allow-ips="10.170.3.217, 10.170.3.220" test:app
如果Gunicorn主机的端口完全在防火墙后面,那就可以将上面这个值设为*,就是代表相信所有。但如果这样设置的话,也是会有安全风险的。
Gunicorn v19版本在REMOTE_ADDR的处理方法方面有了重大突破,在此之前,Gunicorn设置X-Forwarded-For的值是靠代理传过来的。然而这并不符合RFC3875的要求,REMOTE_ADDR现在是代理的ip地址,而不是真实用户的ip地址。文档建议配置Nginx通过X-Forwarded-For头部来传递用户真实的ip地址。
设置的方法,类似在上面的66行左右插入
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
要注意的是,如果将Gunicorn绑到UNIX的socket或者不是TCP的端口,那么REMOTE_ADDR就是一个空值。
(由于翻译水平有限,我自己看回去也感觉有点问题。。总结一下,反正就是最好在Nginx的配置文件加上proxy_set_header X-Forwarded-Proto $scheme;,
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;)
Using Virtualenv
介绍了在虚拟环境下安装Gunicorn,pass pass
Monitoring
注意事项
使用以下的监控服务时是不能开启Gunicorn的守护进程模式。这些监控程序******。守护进程会
Gaffer
Using Gafferd and gaffer
可以用Gaffer来监控Gunicorn,简单的配置例子如下:
[process:gunicorn]
cmd = gunicorn -w 3 test:app
cwd =/path/to/project
Using a Procfile
在project中创建一个Procfile
gunicorn = gunicron -w 3 test:app
这样的话就可以同时运行其他应用
通过gaffer start来启动应用
也可以通过gaffer load来直接热载入Procfile
Runit
也可以用runit这个执行监控
#! /bin/sh
GUNICORN = /usr/local/bin/gunicorn
ROOT = /path/to/project
PID = /var/run/gunicorn.pid
app = main:application
if [-f $PID ]; then rm $PID; fi
cd $ROOT
exec $GUNICORN -c $ROOT/gunicorn.conf.py --pid=$PID $APP
把这个配置文件保存为/etc/sv/[app_name]/run,然后chmod u+x /etc/sv/[app_name]/run更改权限,接着创建软接连到ln -s /etc/sv[app_name] /etc/service/[app_name]。如果已经好安装runit,那么Gunicorn就会在创建好连接后自动运行。
如果Gunicorn没有自动启动,那么就要去用troubleshoot来检查了。
Supervisor
Supervisor也是一个非常好的监控,是用python写的,不过不支持python3。
简单的配置文件例子如下:
[program:gunicorn]
command = /path/to/gunicorn main:application -c /path/to/gunicorn.conf.py
directory = /path/to/project
user = nobody
autostart = true
autorestart = true
redirect_stderr = true
Upstart
/etc/init/myapp.conf:
description `myapp`
start on (filesystem)
stop on runlevel [016]
respawn
setuid nobody
setgid nogroup
chdir /path/to/app/directory
exec /path/to/virtualenv/bin/gunicorn myapp:app
这个配置例子里,我们运行了myapp这个在虚拟环境中的应用,然后所有错误日志会输出到var/log/upstart/myapp.log(什么时候指定的。。。)
Gunicorn部署部分的翻译的更多相关文章
- gunicorn部署Flask服务
作为一个Python选手,工作中需要的一些服务接口一般会用Flask来开发. Flask非常容易上手,它自带的app.run(host="0.0.0.0", port=7001)用 ...
- 记基于docker+gunicorn部署sanic项目遇到的很多很多坑
前言: 最近有个项目需要上线,是python中sanic网络异步框架写的,并且要求使用docker+nginx来部署项目实现负载均衡,于是乎百度了sanic项目部署,基本上都是基于docker+gun ...
- Nginx 和 Gunicorn 部署 Django项目
目录 Nginx 和 Gunicorn 部署 Django项目 配置Nginx 安装配置Gunicorn 通过命令行直接启动 Gunicorn 与 uwsgi 的区别,用哪个好呢 Gunicorn u ...
- Gunicorn配置部分的翻译
写在前面,虽然翻译得很烂,但也是我的劳动成果,转载请注明出处,谢谢. Gunicorn版本号19.7.1 Gunicorn配置 概述 三种配置方式 优先级如下,越后的优先级越大 1.框架的设置(现在只 ...
- 【Django】 gunicorn部署纪要
使用Gunicorn 来部署Django应用, 没有一步一步写怎么操作,简单记录下重要的点,方面以后查阅. 主要的方式还是Nginx反向代理到Gunicorn, Gunicorn wsgi来启动Dja ...
- Python3 Flask+nginx+Gunicorn部署(上)
前言:一般在本地运行flask项目通常是直接python3 文件名.py,然后打开:http://127.0.0.1:5000 查看代码结果 这次主要是记录flask在python3 环境结合ngin ...
- 使用 Nginx 和 Gunicorn 部署 Django 博客(转)
原文:http://zmrenwu.com/post/20/ http://www.siar.me/post/9/ 针对很多朋友反映按照教程的做法始终只能看到 Nginx 欢迎页面的问题,Tian ...
- 使用gunicorn部署Flask项目
[*] 本文出处:http://b1u3buf4.xyz/ [*] 本文作者:B1u3Buf4 [*] 本文授权:禁止转载 从自己的博客移动过来. gunicorn是一个python Wsgi的WEB ...
- nginx+gunicorn部署Django项目
实际采用的nginx.conf文件内容: server { charset utf-8; listen 80; server_name ip; access_log /webapps/project/ ...
随机推荐
- SQL语句(九)使用特殊关系运算符查询
使用特殊关系运算符查询 特殊关系运算符 IN.NOT IN IS NULL.IS NOT NULL BETWEEN.NOT BETWEEN LIKE.NOT LIKE IN , NOT IN IN 在 ...
- 织梦 dedecms 首页调用公司简介的内容
首页调用公司简介的代码: {dede:sql sql='Select content,substring(content,1,300) as content from dede_arctype whe ...
- ASP.NET私有构造函数作用
一.私有构造函数的特性 1.一般构造函数不是私有或者保护成员,但构造函数可以使私有成员函数,在一些特殊的场合,会把构造函数定义为私有或者保护成员. 2.私有构造函数是一种特殊的实例构造函数.它通常用在 ...
- J2EE架构
从整体上讲,J2EE是使用Java技术开发企业级应用的一种事实上的工业标准(Sun公司出于其自身利益的考虑,至今没有将Java及其相关技术纳入标准化组织的体系),它是Java技术不断适应和促进企业级应 ...
- JSBinding+Bridge.NET:Inspector拖变量支持
之前的文档说了,JSB的设计是不允许gameObject上挂逻辑脚本的.原因很简单,在Js工程中根本就不存在C#形式的逻辑脚本,如果在Cs工程中挂上了,到了Js工程这边,直接Missing. 实际在使 ...
- 日常训练赛 Problem C – Complete Naebbirac’s sequence
比赛链接https://vjudge.net/contest/256988#status/17111202012/C/0/ 大意:三个操作,使得输入的数中,从1-n,每一个数出现的次数相同. wa代码 ...
- Oracle Logminer 分析重做日志RedoLog和归档日志ArchiveLog
在实际开发过程中,有时我们很有可能需要某个表的操作痕迹,或通过记录的SQL语句进行有目的性的数据恢复(此时POINT-IN-TIME恢复已经满足不了更细的粒度).或仅仅是查看: 据说Oracle8i之 ...
- Quartus II 破解教程—FPGA入门教程【钛白Logic】
这一节主要说明如何破解Quartus II 13.1.首先找到我们提供的破解工具,这里我们的电脑是64位的,所以使用64位破解器.如下图. 第一步:将破解工具拷贝到安装目录下“D:\altera\13 ...
- 解决centos7下tomcat启动正常,无法访问项目的问题
centos7防火墙不再采用iptables命令,改用firewalld 禁用防火墙命令: # systemctl stop firewalld.service # systemctl disable ...
- 移动端调试利器之vconsole
说明 由于移动端项目在手机中调试时不能使用chrome的控制台,而vconsole是对pc端console的改写 使用方法 使用 npm 安装: npm install vconsole 使用webp ...