diff --git a/gunicorn.conf.py b/gunicorn.conf.py index eeea91f..e9550b9 100644 --- a/gunicorn.conf.py +++ b/gunicorn.conf.py @@ -1,34 +1,52 @@ # /opt/1panel/apps/openresty/nginx/www/sites/43.142.166.152.443/django/gunicorn.conf.py -import multiprocessing -import os +# 绑定的IP和端口(根据你的Nginx配置,后端在8001端口) bind = "127.0.0.1:8001" -# 并发 = workers × threads(gthread 比纯 sync 更抗小程序一进页多接口并发) -cpu_count = multiprocessing.cpu_count() -workers = int(os.environ.get("GUNICORN_WORKERS", max(cpu_count * 2 + 1, 4))) -threads = int(os.environ.get("GUNICORN_THREADS", 2)) -worker_class = "gthread" +# 工作进程数(根据CPU核心数调整,建议:CPU核心数*2+1) +# 查看CPU核心数:cat /proc/cpuinfo | grep "processor" | wc -l +workers = 4 -max_requests = 2000 -max_requests_jitter = 100 +# 工作模式(使用gevent支持异步,更高效) +#worker_class = "gevent" +worker_class = "sync" # 改成这个 -timeout = 60 -graceful_timeout = 30 -keepalive = 5 +# 每个worker的最大请求数(防止内存泄漏) +max_requests = 1000 +max_requests_jitter = 50 # 随机抖动,避免所有worker同时重启 -# Django + MySQL 建议关闭 preload,避免 worker 共享陈旧 DB 连接 -preload_app = False +# 超时时间(秒) +timeout = 30 +keepalive = 2 +# 预加载应用(减少内存占用,加快请求处理) +preload_app = True + +# 日志配置 +# 访问日志:输出到标准输出,便于OnePanel查看 accesslog = "-" +# 错误日志:输出到标准错误 errorlog = "-" +# 日志级别 loglevel = "info" +# 进程名称(便于识别) proc_name = "a_long_dianjing_gunicorn" + +# 工作进程的用户/组(安全考虑,用非root用户运行) +# user = "www-data" +# group = "www-data" + +# 工作进程临时目录(避免权限问题) worker_tmp_dir = "/dev/shm" +# 限制请求数据大小(防止大文件攻击) limit_request_line = 4094 limit_request_fields = 100 limit_request_field_size = 8190 +# 优雅退出时间 +graceful_timeout = 30 + +# 进程守护(如果用systemd管理,这里设为False) daemon = False \ No newline at end of file