文章目录
前言
Python可以用命令 “python -m http.server” 来启动http服务,Django也有wsgi.py通过 “python manage.py runserver” 来启动web服务器,但他们受限于性能、安全问题,只能作为自己调试程序使用,不能用于商业用途。比较常用的http服务器一般是Nginx、Apache等,web服务器一般有UWSGI、Gunicorn等。
本文示例项目使用Django做web服务其中包含dwebsocket服务, 前端使用ajax请求接口来做前后端分离。
Nginx + uwsgi部署
1.请求流程
Nginx配置(文字格式 方便复制):
## /etc/nginx/conf.d/自定义名字.conf
server {
listen 9532; # 监听端口
server_name 10.1xx.81.xxx; # 你的IP或者网址
location / {
root /home/file_share/web/; # 前端文件根目录
try_files $uri $uri/ /index.html;
add_header 'Access-Control-Allow-Origin' *; # 跨域请求必须加这一行
}
# 当前端请求过来时转发 比如 127.0.0.1:9532/apis 就会请求到这里
location /apis {
rewrite ^.+apis/?(.*)$ /$1 break; # 分配好/apis/xxx后边的xxx
include uwsgi_params;
转发uwsgi端口
uwsgi_pass 0.0.0.0:9436;
websocket的匹配
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
UWSGI配置(文字格式 方便复制):
非websocket版:
[uwsgi]
# 使用nginx就用这个
# 这个端口号务必与nginx配置文件中的location里调用的一致
socket=0.0.0.0:9436
# 直接访问的就用这个
#http=0.0.0.0:10020
# 项目根目录
chdir=/home/file_share/server
# wsgi所在目录
wsgi-file=/home/file_share/server/file_share/wsgi.py
processes=4
master=True
pidfile=uwsgi.pid # 记录pid的文件 用来关闭uwsgi
daemonize=uwsgi.log # log文件
env LANG="en_US.UTF-8"
env LANGUAGE="en_US.UTF-8
async = 30
ugreen = True
http-timeout = 300
websocket版:
[uwsgi]
http=0.0.0.0:10020
http-websockets=True
# 项目根目录
chdir=/home/file_share/server
# wsgi所在目录
wsgi-file=/home/file_share/server/file_share/wsgi.py
processes=4
master=True
pidfile=uwsgi_websocket.pid
daemonize=uwsgi_websocket.log
env LANG="en_US.UTF-8"
env LANGUAGE="en_US.UTF-8"
http-websockets = True
async = 30
ugreen = True
http-timeout = 300
2. 运行
UWSGI:
按照上图写好配置文件后,在项目根目录(也就是uwsgi.ini同级目录)运行uwsgi --ini uwsgi.ini
,如果还有websocket服务,那么再运行一个uwsgi --ini uwsgi_websocket.ini
,至此uwsgi服务已经启动,可以用postman请求一下自己服务器IP:uwsgi的端口看是否返回的是正常的JSON数据。
关闭uwsgi服务可用uwsgi --stop uwsgi.pid
(这个pid文件就是刚刚运行ini文件生成的)
Nginx:
在这之后命令行输入nginx
启动nginx服务,然后浏览器打开自己服务器IP:nginx端口来查看项目,部署完毕。
3. 弊端
可支持的websocket连接数量等于uwsgi配置文件中的processes配置数量,且相互之间的进程不通信,所以协同的功能就没有了。
Nginx + Gunicorn部署
1. 配置文件
Nginx配置(文字格式 方便复制):
## /etc/nginx/conf.d/自定义名字.conf
server {
listen 9532; # 监听端口
server_name 10.1xx.81.xxx; # 你的IP或者网址
location / {
root /home/file_share/web/; # 前端文件根目录
try_files $uri $uri/ /index.html;
add_header 'Access-Control-Allow-Origin' *; # 跨域请求必须加这一行
}
# 当前端请求过来时转发 比如 127.0.0.1:9532/apis 就会请求到这里
location /gun_api {
#gunicorn配置
rewrite ^.+gun_api/?(.*)$ /$1 break;
proxy_pass http://0.0.0.0:9432;
proxy_http_version 1.1;
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_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
Gunicorn配置(文字格式 方便复制):
也能用命令直接启动,但是参数太多,命令很长也不好输,所以保存成py文件方便启动。
项目根目录(manage.py同级目录)新建gunicorn.py文件,写入以下内容
bind = '0.0.0.0:9432' # 绑定ip与端口号
backlog = 512 # 监听队列
chdir = '/home/file_share/server'
# 使用gevent模式,还可以使用sync 模式,默认的是sync模式
worker_class = 'gevent'
loglevel = 'info' # 日志级别,这个日志级别指的是错误日志的级别,而访问日志的级别无法设置
errorlog = '/home/file_share/server/logs/gunicorn.error.log' #错误日志
accesslog = '/home/file_share/server/logs/gunicorn.access.log' #访问日志
daemon = True # 后台运行
2.运行
启动Gunicorn服务:
命令行输入:nohup gunicorn -c gunicorn.py 项目名.wsgi:application
(项目名.wsgi会自动调用django中的wsgi.py文件)
启动Nginx:
命令行输入:nginx
3.查看运行端口
Nginx运行端口:netstat -anp | grep nginx
Gunicorn:netstat -anp | grep python