文章目录
  1. 1. 前言
  2. 2. 安装
  3. 3. 卸载
  4. 4. 配置
  5. 5. 参考资料
  6. 6. 文档信息

前言


Gunicorn ‘Green Unicorn’ is a Python WSGI HTTP Server for UNIX. It’s a pre-fork worker model. The Gunicorn server is broadly compatible with various web frameworks, simply implemented, light on server resources, and fairly speedy.

安装


1
$ pip3 install gunicorn

卸载


1
$ pip3 uninstall gunicorn

配置


gunicorn.conf.py 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import multiprocessing

bind = '127.0.0.1:9046'
workers = multiprocessing.cpu_count() * 2 + 1
threads = 2
backlog = 512
daemon = True #True守护进程启动
chdir = '/Users/guohaitao/Desktop/py/project/circle'
access_log_format = '%(h)s %(l)s %(u)s %(t)s %(s)s %(b)s'
loglevel = 'info'
worker_class = 'sync'
errorlog = chdir + '/logs/error.log'
accesslog = chdir + '/logs/access.log'
pidfile = chdir + '/logs/pidfile.pid'

启动:

1
2
3
./venv/bin/gunicorn -c gunicorn.conf.py myapp:app

gunicorn -c gunicorn.conf.py myapp:app

若没有上述配置文件,我们可以通过参数方式进行配置启动:

1
gunicorn -w 2 -b 127.0.0.1:9045 myapp:app # 指定线程数,指定端口

关闭

1
kill `cat logs/pidfile.pid`

Nginx 配置:

1
2
3
4
5
6
7
8
9
10
11
server {
listen 80;
server_name test.devhitao.com;
access_log /var/log/nginx/example.log;

location / {
proxy_pass http://127.0.0.1:9046;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

参考资料


文档信息


  • 版权声明:自由转载-保持署名-非商用-非衍生 ( CC BY-NC-ND 4.0 )
文章目录
  1. 1. 前言
  2. 2. 安装
  3. 3. 卸载
  4. 4. 配置
  5. 5. 参考资料
  6. 6. 文档信息