核心配置文件
nginx.conf
server {
listen 80;
server_name 111.67.204.165;
charset utf-8;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8002;
}
location /static {
alias /www/wwwroot/library_manage/static;
}
}
uwsgi.ini
[uwsgi]
socket=127.0.0.1:8002
chdir=/www/wwwroot/library_manage
wsgi-file = /www/wwwroot/library_manage/library_manage/wsgi.py
module=library_manage.wsgi
master=true
processes=4
vacuum=true
参数:
http : 协议类型和端口号
processes : 开启的进程数量
workers : 开启的进程数量,等同于processes(官网的说法是spawn the specified number ofworkers / processes)
chdir : 指定运行目录(chdir to specified directory before apps loading)
wsgi-file : 载入wsgi-file(load .wsgi file)
stats : 在指定的地址上,开启状态服务(enable the stats server on the specified address)
threads : 运行线程。由于GIL的存在,我觉得这个真心没啥用。(run each worker in prethreaded mode with the specified number of threads)
master : 允许主进程存在(enable master process)
daemonize : 使进程在后台运行,并将日志打到指定的日志文件或者udp服务器(daemonize uWSGI)。实际上最常用的,还是把运行记录输出到一个本地文件上。
pidfile : 指定pid文件的位置,记录主进程的pid号。
vacuum : 当服务器退出的时候自动清理环境,删除unix socket文件和pid文件(try to remove all of the generated file/sockets)
socket用于和nginx对接,必须内网ip,端口与nginx保持一致。nginx监听80端口,uwsgi_pass 对接uwsgi,80端口外部直接访问ip可以映射到django。
过程
nginx listen 指定的是nginx代理uwsgi对外的端口号(外网)。
server_name 网上大多资料都是设置的一个网址(例,www.example.com),我这里指定的到了本机默认ip。如果配置网址,需要反向代理(目前没试过)。
nginx与uwsgi产生关联。最主要的就是这两行配置。
include uwsgi_params;
uwsgi_pass 127.0.0.1:8000;
include 必须指定为uwsgi_params;而uwsgi_pass指的本机IP(内网)的端口与myweb_uwsgi.ini配置文件中的必须一致。
使用宝塔界面时,python管理器中启动文件必须为项目目录(坑)
uwsgi
本地单独启动时,配置文件应该为http:
[uwsgi]
http=:8001
chdir=/www/wwwroot/library_manage
module=library_manage.wsgi
master=true
processes=4
vacuum=true
完整的uwsgi文件,socket用于和nginx对接:
[uwsgi]
#监听的地址 必须和nginx中的一致
socket = 127.0.0.1:8000
#wsgi文件,在你的项目配置目录下可以找到 注意:最后输入绝对地址
wsgi-file = /home/aeasringnar/my_Project/jwt-test/my_jwt_test/wsgi.py
# 你的项目的根目录 绝对地址
chdir = /home/aeasringnar/my_Project/jwt-test
# 你项目使用的虚拟环境的根目录 绝对地址
home = /home/aeasringnar/.envs/jwt-test
#你的日志目录,注意的是,你的django控制台输出的日志都会在这里输出,uwsgi的相关日志也在这里
daemonize = /home/aeasringnar/my_Project/jwt-test/test.log
####下面的配置可以有 也可以没有,看个人需求,不建议配置
# 主进程
master = true
# 多站模式
vhost = true
# 多站模式时不设置入口模块和文件
no-site = true
# 子进程数
workers = 2
# 退出、重启时清理文件
vacuum = true
uwsgi 启动方法
-
配置文件启动
uwsgi --ini uwsgi .ini # 或 uwsgi uwsgi .ini -d 参数可以后台启动
-
命令直接启动
uwsgi --http :8001 --module library_manage.wsgi # 即参数带着
启动失败
[uWSGI] getting INI configuration from test.ini
大部分都是由于端口被占用了,查看端口情况
netstat -ntulp |grep 80
如果是后台有多个uwsgi启动,关闭所有uwsgi进程命令:
killall -9 uwsgi
kill -9 `ps -ef |grep uwsgi|awk {'print $2'}`
参考:
全
https://blog.csdn.net/qq_30501975/article/details/80423547
https://blog.csdn.net/u011089760/article/details/90136609
反向代理重定向301
https://www.jianshu.com/p/293afe746323
宝塔配置:
https://blog.dyboy.cn/program/144.html
坑
https://blog.csdn.net/weixin_39584758/article/details/80354983