gunicorn+nginx部署配置文件
bind = '0.0.0.0:8000' #指定后端ip+端口,默认会使用当前服务器ip地址 reload = True #本地改了代码会自动重启 pidfile = '/usr/src/app/logs/gunicorn.pid'#gunic启动的时候会使用pid文件取保存pid号,是一个进程id accesslog = '/usr/src/app/logs/gunicorn_acess.log'#保存访问日志 errorlog = '/usr/src/app/logs/gunicorn_error.log'#保存错误日志
启动服务命令 /usr/local/bin/gunicorn -c /usr/src/app/configs/gunicorn_config.py ${PROJECT_NAME}.wsgi
# 可以实现多线程,多进程机制
nginx.conf 配置日志,超时信息keepalive_timeout
user nginx; worker_processes 1; error_log off; pid /run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; autoindex_localtime on; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; index index.html index.htm; include /etc/nginx/conf.d/*.conf; }
default.conf 重点在于这个配置文件
# 反向代理 upstream app_server { server django_app:8000;# 指定gunior中ip+端口 # 如果多个gunicorn,负载均衡配置,多个nginx lvs keepalive负载均衡 } #监听后端服务,一般会分开,现在我们是把静态文件服务和反向代理服务,所以没有放开,放在不同serve当中,现在这样做主要是想使用接口文档平台 # 后端反向代理服务,可以访问后端服务,也可以访问接口平台 server { listen 8000;#端口 server_name [SERVER_ADDR]; #ip access_log /var/log/nginx/access.log main; error_log off; autoindex_localtime on; location /static { alias /var/www/html/static; # 找静态资源 } location / { try_files $uri @proxy_to_app;# 反向代理 } location @proxy_to_app { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header Host $http_host; proxy_redirect off; proxy_pass http://app_server; # 这里要与upstream * 中保持一致 ,不然报错, } } # 监听前端服务 server { listen 80; server_name [SERVER_ADDR]; root /var/www/html;# 静态文件位置 access_log /var/log/nginx/access.log main;#访问日志,访问级别 error_log off; # 路由条目,默认去找index的入口文件 location / { try_files $uri $uri/ /index.html; } error_page 404 /404.html; location = /404.html { root /usr/share/nginx/html; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } location ~ /\.ht { deny all; } }
#并发需要redies+celery