nginx基于uwsgi发布django应用
安装nginx
安装uwsgi
pip install uwsgi
环境路径:
1 [root@Centos6-10 app]# pwd 2 /app 3 [root@Centos6-10 app]# ll 4 drwxr-xr-x. 6 root root 4096 10月 28 17:32 nginx # nginx 5 drwxr-xr-x. 3 root root 4096 10月 28 17:11 wsongl # Django项目
用uwsgi命令启动Django:
uwsgi --http 192.168.177.129:8080 --file wsongl/wsgi.py --static-map=/static=static
参数说明:
--http 这个就和runserver一样指定IP 端口
--file 这个文件就里有一个反射,如果你在调用他的时候没有指定Web Server就使用默认的
-- static 做一个映射,指定静态文件
此时,访问http://192.168.177.129:8080/,如下所示,表示项目启动成功
输出内容如下:
It worked!
Congratulations on your first Django-powered page.
用uwsgi配置文件来启动Django:
在wsongl项目下,建立script目录,在里面建立uwsgi.ini文件,内容如下
# uwsig使用配置文件启动 [uwsgi] # 项目目录 chdir=/app/wsongl/ # 指定项目的application module=wsongl.wsgi:application # 指定sock的文件路径 socket=/app/wsongl/script/uwsgi.sock # 进程个数 workers=5 pidfile=/app/wsongl/script/uwsgi.pid # 指定IP端口 (!!! 只有这里指定了IP端口,才能不通过nginx,直接访问!!!) http=192.168.177.129:8080 # 指定静态文件 static-map=/static=/app/wsongl/static # 启动uwsgi的用户名和用户组 uid=root gid=root # 启用主进程 master=true # 自动移除unix Socket和pid文件当服务停止的时候 vacuum=true # 序列化接受的内容,如果可能的话 thunder-lock=true # 启用线程 enable-threads=true # 设置自中断时间 harakiri=30 # 设置缓冲 post-buffering=4096 # 设置日志目录 daemonize=/app/wsongl/logs/uwsgi.log
启动命令:
uwsgi --ini /app/wsongl/script/uwsgi.ini # 启动
uwsgi --reload /app/wsongl/script/uwsgi.pid # 重启
uwsgi --stop /app/wsongl/script/uwsgi.pid # 停止
用nginx部署uwsgi:
需要两个配置文件
uwsgi.ini
nginx的conf文件,这里命名为django.conf
diango.conf文件
server { listen 80; server_name 192.168.177.129; access_log /app/nginx/logs/access.log; charset utf-8; gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php application/json text/json image/jpeg image/gif image/png application/octet-stream; error_page 404 /404.html; error_page 500 502 503 504 /50x.html; # 指定项目路径uwsgi location / { include uwsgi_params; # 导入一个Nginx模块他是用来和uWSGI进行通讯的 uwsgi_connect_timeout 30; # 设置连接uWSGI超时时间 uwsgi_pass unix:/app/wsongl/script/uwsgi.sock; # 指定uwsgi的sock文件所有动态请求就会直接丢给他
} # 指定静态文件路径 location /static/ { alias /app/wsongl/static/; } }
启动uwsgi uwsgi --ini /app/wsongl/script/uwsgi.ini
启动nginx /app/nginx/sbin/nginx
部署完毕!
nginx负载uwsgi集群的写法:
upstream wsl{ server 127.0.0.1:8080; } server { listen 80; server_name 192.168.154.131 www.wsl.com; access_log /app/nginx/logs/access.log; charset utf-8; gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php ap plication/json text/json image/jpeg image/gif image/png application/octet-stream; error_page 404 /404.html; error_page 500 502 503 504 /50x.html; # 指定项目路径uwsgi
location / {
proxy_pass http://wsl;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_max_temp_file_size 0;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
# 指定静态文件路径 location /static/ { alias /app/wsongl/static/; index index.html index.htm; } }
参考资料: https://www.cnblogs.com/chenice/p/6921727.html
uwsgi配置参数:https://blog.csdn.net/Miss_Audrey/article/details/81874038