gunicorn|daphne程序

gunicorn配置文件
  1. # gunicorn.py
  2. import logging
  3. import logging.handlers
  4. from logging.handlers import WatchedFileHandler
  5. import os
  6. import multiprocessing
  7. #进程名
  8. proc_name = 'gunicorn_项目名_project'
  9. bind = "0.0.0.0:12222"
  10. max_requests = 5000
  11. # 设置gunicorn使用的python虚拟环境
  12. pythonpath='/home/chenxinming/项目名//venv/bin/python3'
  13. timeout = 300
  14. user = 'root'
  15. group = 'root'
  16. # 默认为False。此设置用于开发,每当应用程序发生更改时,都会导致工作重新启动。
  17. reload = true
  18. # 设置环境变量
  19. raw_env=['APE_API_ENV=DEV']
  20. pidfile = '/tmp/zwy-gunicorn.pid'
  21. backlog = 512 #监听队列
  22. chdir = '/home/test/server/bin' #gunicorn要切换到的目的工作目录
  23. worker_class = 'gevent' #使用gevent模式,还可以使用sync 模式,默认的是sync模式
  24. workers = multiprocessing.cpu_count() * 2 + 1 #进程数
  25. threads = 2 #指定每个进程开启的线程数
  26. loglevel = 'info' #日志级别,这个日志级别指的是错误日志的级别,而访问日志的级别无法设置
  27. access_log_format = '%(t)s %(p)s %(h)s "%(r)s" %(s)s %(L)s %(b)s %(f)s" "%(a)s"' #设置gunicorn访问日志格式,错误日志无法设置
  28. """
  29. 其每个选项的含义如下:
  30. h remote address
  31. l '-'
  32. u currently '-', may be user name in future releases
  33. t date of the request
  34. r status line (e.g. ``GET / HTTP/1.1``)
  35. s status
  36. b response length or '-'
  37. f referer
  38. a user agent
  39. T request time in seconds
  40. D request time in microseconds
  41. L request time in decimal seconds
  42. p process ID
  43. """
  44. accesslog = "/home/test/server/log/gunicorn_access.log" #访问日志文件
  45. errorlog = "/home/test/server/log/gunicorn_error.log" #错误日志文件
  1. upstream django {
  2. ip_hash;
  3. server 127.0.0.1:8000;
  4. }
  5. server {
  6. listen 80 default;
  7. server_name xxxxx.com;
  8. keepalive_timeout 70;
  9. location / {
  10. proxy_set_header Host $host;
  11. proxy_set_header X-Real-IP $remote_addr;
  12. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  13. proxy_connect_timeout 60;
  14. client_max_body_size 500m;
  15. client_body_buffer_size 128k;
  16. proxy_pass django;
  17. }
  18. }

uwsgi程序

uwsgi配置文件
  1. [uwsgi]
  2. # 设置python程序名称
  3. py-program-name='django—project'
  4. #使用nginx连接时使用,如果不连接nginx,注释该选项
  5. socket = 127.0.0.1:50000
  6. chmod-socket = 664
  7. #作为web服务器使用,也就是通过http访问,如果使用nginx,注释该选项,使用socket链接
  8. #http = :8000
  9. #项目的文件目录
  10. chdir = /python_test/Fresh_every_day
  11. #项目中wsgi文件所在地址,相对于项目的文件目录地址
  12. wsgi-file = Fresh_every_day/wsgi.py
  13. #启动的进程数量
  14. processes = 4
  15. #每个进程启动的线程数量,由于GIL锁,此选项鸡肋
  16. threads = 2
  17. #允许主进程存在
  18. master = true
  19. #在指定地址上开启服务状态
  20. #使用nc 127.0.0.1 1717
  21. stats = 127.0.0.1:9090
  22. # 允许程序后台允许,并且日志文件保存在哪里,使用了supervisor就不要再让uwsgi后台了,否则会出现uwsgi杀不死的bug
  23. # daemonize = /home/log/uwsgi.log
  24. #程序运行时主进程的uwsgi.pid保存在此文件内
  25. pidfile = /tmp/uwsgi.pid
  26. #当服务器退出的时候自动清理环境,删除unix socket文件和pid文件
  27. vacuum = true
  28. # 将会每2秒检查一次python模块的改动,自动重启。
  29. py-autoreload = 2
  30. #日志最大
  31. log-maxsize = 50000000
  32. #只保存错误请求数据
  33. # disable-logging = true
  34. #env虚拟环境所在路径(两种都可以)
  35. home = /path/to/virtualenv #这是官网写的
  36. virtualenv = /root/.virtualenvs/Fresh_every_day #另一种方式
  37. #启用线程
  38. enable-threads = true
  39. #一个请求花费的时间超过了这个harakiri超时时间,那么这个请求都会被丢弃,
  40. #并且当前处理这个请求的工作进程会被回收再利用(即重启)
  41. harakiri = 60
  42. #设置默认缓冲区大小,接受一个拥有很多请求头的大请求,最多可到64k
  43. #buffer-size = 65536
  44. #设置静态文件
  45. #static-map = /static=//www/wwwroot/mysite/static
  46. #gevent协程支持,最大100个, 不推荐使用协程,有连接不到redis的BUG
  47. #gevent=100
  48. #gevent协程补丁,会抛出下方的警告,补丁慎打,这个项目打了这个补丁无法登录了....
  49. #MonkeyPatchWarning: Monkey-patching ssl after ssl has already been imported may lead to
  50. #errors, including RecursionError on Python 3.6. It may also silently lead to incorrect behaviour on
  51. #Python 3.7. Please monkey-patch earlier. See https://github.com/gevent/gevent/issues/1016.
  52. #Modules that had direct imports (NOT patched):
  53. #['urllib3.util (/root/.virtualenvs/Fresh_every_day/lib/python3.6/site-packages/urllib3/util/__init__.py)',
  54. #gevent-monkey-patch=true
  55. #gevent-early-monkey-patch=true
  56. #添加雷锁,防止惊群现象
  57. thunder-lock=true
  1. upstream django {
  2. server 0.0.0.0:50000;
  3. }
  4. server {
  5. listen 80;
  6. server_name pythonav.cn;
  7. #访问nginx的根路径时,转发请求给uwsgi的8000端口,这里要和uwsgi.ini写的一致
  8. location / {
  9. proxy_set_header Host $host;
  10. proxy_set_header X-Real-IP $remote_addr;
  11. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  12. proxy_connect_timeout 60;
  13. client_max_body_size 500m;
  14. client_body_buffer_size 128k;
  15. include uwsgi_params;
  16. uwsgi_pass django;
  17. }
  18. }