uwsgi gunicorn nginx 配置

介绍

uwsgi配置

p.ini

[uwsgi]
#socket=127.0.0.1:5003
# uwsgi的监听端口                                              

socket=/tmp/p.sock
chmod-socket=600

buffer-size=65536                                                                              
#设置用于uwsgi包解析的内部缓存区大小,默认是4k                                                                               
chdir=/opt/p_test 
# 项目根目录                                           
                                                                               
wsgi-file=p.py       
# flask程序的启动文件                                           
                                                                     
callable=app
#flask应用实例的名称,是flask独有的配置项       

# process-related settings
# master
master=true
# maximum number of worker processes
processes=4
# the socket (use the full path to be safe
enable-threads=true
threads=4
thunder-lock=true

pidfile=uwsgi.pid

vacuum = true
#退出后清理环境

#daemonize=/tmp/vancouver.log 
#使进程在后台运行,并将日志打到指定的日志文件

uid=99
gid=99
#指定运行用户

#服务统计
stats=127.0.0.1:5002
#stats-server=true
stats-http=true

static-map=/static=/opt/p_test
#uWSGI 即可将指定请求前缀映射到文件系统上的对应物理目录
uwsgi --ini p.ini

gunicorn

pip3 install gevent

gconf.py

from gevent import monkey
monkey.patch_all()
import multiprocessing
debug = True
loglevel = 'debug'
bind = '0.0.0.0:5000' #绑定与Nginx通信的端口
pidfile = 'log/gunicorn.pid'
logfile = 'log/debug.log'
accesslog='log/debug1.log'
#loglevel='info'
workers = multiprocessing.cpu_count() * 2 + 1
worker_class = 'gevent' #默认为阻塞模式,最好选择gevent模式
worker_connections=10240 #
backlog=5000 #挂起连接的最大数目
max_requests_jitter=3 #抖动参数,防止worker全部同时重启

gunicorn -c gconf.py g:app -D

nginx配置

server {                                                                       
	listen 8000;                   # 服务器监听端口                                                 
	#server_name test.com; # 这里写你的域名或者公网IP                                                    
	charset      utf-8;          # 编码                                                  
	client_max_body_size 75M;                                                       

 location / {                                                                   
	include uwsgi_params;         # 导入uwsgi配置                                            
	#uwsgi_pass 127.0.0.1:5002;    # 转发端口,需要和uwsgi配置当中的监听端口一致                                             
	uwsgi_pass unix:///tmp/p.sock;    # 转发sock,需要和uwsgi配置当中的监听一致                                             
	# uwsgi_param UWSGI_PYTHON /home/自己创建的目录/venv;       # Python解释器所在的路径(这里为虚拟环境)      
	# uwsgi_param UWSGI_CHDIR /home/自己创建的目录;             # 项目根目录     
	# uwsgi_param UWSGI_SCRIPT manage:app; #比如你测试用test.py文件,文件中app = Flask(__name__),那么这里就填  test:app
			# 项目的主程序                           
      }  
 location /static {
  alias /opt/p_test/static;
  #静态文件不走uwsgi直接nginx访问
 }
	  
}