在开发调试django时,通常只需要直接运行django服务即可,但是线上环境发布django服务时,通常是借助于nginx服务。
觉得主要是借助web服务器的高性能优势吧。
准备一个已经正常运行的django服务
[root@local-virtual-host mysite]# python3 manage.py runserver 0:8000
Performing system checks...
System check identified no issues (0 silenced).
March 15, 2021 - 14:53:18
Django version 2.2.17, using settings 'mysite.settings'
Starting development server at http://0:8000/
Quit the server with CONTROL-C.
部署程序到nginx
nginx是一个性能极高的静态web服务器,当处理一些动态请求时,nginx就无能为力了,就好像nginx解析php文件需要借助于php-fpm一样,解析python代码文件也需要一个中间件,这个就是uwsgi。
我上面的django程序使用的python3,因此需要先安装wsgi,使用如下命令可以直接安装:pip3 install uwsgi
需要说明的是,当系统中有多个python环境时,注意要安装在和上面运行django的同一个python环境中。
在djanog的工程目录下面创建uwsgi.ini文件,文件内容如下:
[root@local-virtual-host mysite]# cat uwsgi.ini
# uwsgi.ini file
[uwsgi]
# Django-related settings
socket = :8001 # 这个用于nginx的转发配置
http = 0:8000 # 用于提供http服务
# the base directory (full path)
chdir = /data/pycode/mysite #django工程路径
# Django s wsgi file
module = mysite.wsgi # mysite为django工程名
# process-related settings
master = true
# maximum number of worker processes
processes = 5
#maximum number of worker threads
threads = 5
# try to remove all of the generated file/sockets
vacuum = true
然后启动uwsgi服务:
[root@local-virtual-host mysite]# pwd
/data/pycode/mysite
[root@local-virtual-host mysite]# uwsgi --ini uwsgi.in
......
查看当前服务器上监听的端口:
[root@local-virtual-host nginx]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 2939/uwsgi
tcp 0 0 0.0.0.0:8001 0.0.0.0:* LISTEN 2939/uwsgi
模拟访问一下django服务的请求,可以正常返回,如下:
[root@local-virtual-host nginx]# curl 'http://192.168.1.121:8000/query/?queryType=subinfo&host=10.9.68.41&port=3309'
{"count": 1, "data": [{"msg": "\u6d4b\u8bd5", "ip": "172.23.34.12", "port": 986, "idc": "\u5317\u6781"}]}
配置nginx
nginx的安装不在说明,直接说配置如下:
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8001;
uwsgi_read_timeout 15;
}
location /static {
expires 30d;
autoindex on;
add_header Cache-Control private;
alias /usr/local/nginx/html/static;
}
}
有关静态文件说明:
我们需要在django的settings.py中配置static_root的绝对路径,这个路径是指,当我们部署live环境时,所使用的web静态资源的位置。
STATIC_ROOT = "/usr/local/nginx/html/static/"
然后执行如下命令,把django的静态文件拷贝到指定的目录下面:
python3 manage.py collectstatic
然后启动nginx服务,nginx默认监听80端口,上面的server并没有修改指定的端口,同样模拟访问80端口请求数据。
[root@local-virtual-host nginx]# curl 'http://192.168.1.121/query/?queryType=subinfo&host=10.9.68.41&port=3309'
{"count": 1, "data": [{"msg": "\u6d4b\u8bd5", "ip": "172.23.34.12", "port": 986, "idc": "\u5317\u6781"}]}
查看nginx的访问日志,如下:
192.168.1.121 - - [15/Mar/2021:23:22:45 +0800] "GET /query/?queryType=subinfo&host=10.9.68.41&port=3309 HTTP/1.1" 200 105 "-" "curl/7.29.0"
错误页面处理
当代码中出现404,502等这些异常时,会希望以一种友好的方式展示;在debug模式下,页面会展示相应的错误提示,但是在生产环境中时,我们就需要自定义页面显示了!
首先在view中定义相应处理函数,如下:
def page_not_fund(request, exception): #注意这个404处理要写两个参数,django版本2.2.17
return render(request, "app/404.html")
def page_error(request):
return HttpResponse("Sorry! The request occur error")
然后在路由(urls.py)中进行赋值处理,如下:
handler404 = page_not_fund
handler500 = page_error