Gunicorn 簡單使用
- 配置文件如下
import multiprocessing
bind = '0.0.0.0:8000'
workers = multiprocessing.cpu_count() * 2 + 1
# 服務連接已滿時,可容納的等待連接數,當超過該值時,服務會直接被拒絕
backlog = 2048
worker_class = "gevent"
# 最大連接數
worker_connections = 1000
threads = 30
# 後臺運行
daemon = True
proc_name = 'gunicorn_demo'
# 日誌輸出
access_log = "./logs/access_guni.log"
error_log = "./logs/error/guni.log"
# pid 用於啓停
pidfile = "./logs/guni.pid"
timeout = 600
- 服務啓停
- 啓動
gunicorn -c guni_config.py manage:app
- manage 就是 python 文件
mange.py
- 服務對象叫做 app
- 關閉
kill -9 ./logs/guni.pid
- 重啓
kill -HUP ./logs/guni.pid
- 配置 syslog(可輸出到 logstash)
syslog = True
syslog_addr = "udp://11.1.1.1:9090"
access_log_format = ''%(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s"''
- 日誌格式解釋
- 官方文檔
https://docs.gunicorn.org/en/stable/settings.html#access-log-format
變量名 |
釋義 |
備註 |
h |
remote address |
|
l |
‘-’ |
|
u |
user name |
|
t |
date of the request |
|
r |
status line (e.g. GET / HTTP/1.1) |
|
m |
request method |
|
U |
URL path without query string |
|
q |
query string |
|
H |
protocol |
|
s |
status |
|
B |
response length |
|
b |
response length or ‘-’ (CLF format) |
|
f |
referer |
|
a |
user agent |
|
T |
request time in seconds |
|
D |
request time in microseconds |
|
L |
request time in decimal seconds |
|
p |
process ID |
|
{header}i |
request header |
|
{header}o |
response header |
|
{variable}e |
environment variable |
|