uWSGI建议我们可以将静态文件部署到Nginx这样的服务器上,Nginx比uWSGI有更好的静态文件处理能力,但由于Nginx服务器无法直接处理python这样的程序代码,可将对动态内容的请求交给uWSGI来处理.
我们的uWSGI已经配置好,现在只需要将静态部分内容交由Nginx处理,动态请求转发到uWSGI处理就可以:
首先我们的uWSGI并不是作为我们的唯一的web服务器来使用,那么需要修改配置下wsgi配置文件:
[uwsgi]
# 配置服务器的监听ip和端口
# http = 127.0.0.1:3309
scoket = 127.0.0.1:3309
# 配置项目目录
chdir = /home/administrator/PyPros/Django_Test
# 配置入口模块
module = Django_Test.wsgi:application
# 开启master, 将会多开一个管理进程, 管理其他服务进程
master = True
# 服务器开启的进程数量
processes = 2
# 服务器进程开启的线程数量
threads = 4
# 退出的时候清空环境变量
vacuum = true
# 进程pid
pidfile=uwsgi.pid
# 配uWSGI搜索静态文件目录
check-static = /home/administrator/PyPros/Django_Test/static
Nginx通过socket将请求发送至uWSGI服务器, 将http修改为socket.
以我nginx安装为例,我的配置文件在/opt/nginx/conf/nginx.conf,在server配置块中增加如下配置:
location / {
# 将nginx所有请求转到uwsgi
include uwsgi_params;
# uwsgi的ip与端口
uwsgi_pass 127.0.0.1:3309;
}
重新启动uWSGIS, Nginx服务器, 访问localhost, 这时不需要填写端口,Nginx默认监听在80端口, 运行效果:
现在我们注释wsgi配置文件静态文件部分内容:
[uwsgi]
# 配置服务器的监听ip和端口
# http = 127.0.0.1:3309
scoket = 127.0.0.1:3309
# 配置项目目录
chdir = /home/administrator/PyPros/Django_Test
# 配置入口模块
module = Django_Test.wsgi:application
# 开启master, 将会多开一个管理进程, 管理其他服务进程
master = True
# 服务器开启的进程数量
processes = 2
# 服务器进程开启的线程数量
threads = 4
# 退出的时候清空环境变量
vacuum = true
# 进程pid
pidfile=uwsgi.pid
# 配uWSGI搜索静态文件目录
# check-static = /home/administrator/PyPros/Django_Test/static
重启uWSGI服务器:
uWSGI不再提供静态请求服务,现在我们将这部分请求移至Nginx中处理, 修改Nginx配置文件nginx.conf文件, 在nginx配置文件增加如下:
location /static {
alias /home/administrator/PyPros/Django_Test/static;
}
当用户请求/static/images/a.png, Nginx会根据配置信息判断路径是/static开头的,会将所有的/static/*.*内容转换为:
/home/administrator/PyPros/Django_Test/static/images/a.png
运行效果: