Nginx+Flask+UWSGI

发布 : 2017-11-07 分类 : 大数据 浏览 :
1
2
3
4
5
6
7
8
9
10
11
使用的代理一共有两个,nginx和uwsgi,使用nginx的目的是为了安全和负载均衡

配置了nginx做前端代理
uwsgi作后端代理的服务器

在处理来自Internet的请求时,要先经过nginx的处理,nginx把请求再交给uwsgi,经过uwsgi才能访问到项目本身

没有nginx而只有uwsgi的服务器,则是Internet请求直接由uwsgi处理,并反馈到我们的项目中。
nginx可以实现安全过滤,防DDOS等保护安全的操作,并且如果配置了多台服务器,nginx可以保证服务器的负载相对均衡

而uwsgi则是一个web服务器,实现了WSGI协议(Web Server Gateway Interface),http协议等,它可以接收和处理请求,发出响应等

Nginx安装

  • Mainline version:开发版
  • Stable version:稳定版
1
[root@sparsematrix ~]# vim /etc/yum.repos.d/nginx.repo
1
2
3
4
5
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

All text

查看yum源中nginx相关版本

1
[root@sparsematrix ~]# yum list | grep nginx

使用yum源安装Nginx

1
[root@sparsematrix ~]# yum install nginx -y

查看当前安装nginx版本

1
[root@sparsematrix ~]# nginx -v

All text

查看Nginx编译参数

1
[root@sparsematrix ~]# nginx -V

All text

启动nginx

1
[root@sparsematrix ~]# /usr/sbin/nginx

All text

关闭nginx

1
nginx -s stop

平滑启动 nginx

1
nginx -s reload

安装uWSGI

1
在安装uWSGI前,需要解决 uWSGI 的依赖问题,因为uWSGI是一个C语言写的应用,所以我们需要C编译器,以及python开发相关组件:
1
[root@sparsematrix ~]# /usr/local/python3/bin/pip install uwsgi

在项目根目录下创建一个配置文件runapp.ini(uwsgi支持多种配置文件格式,xml,ini,json等)

1
2
[root@sparsematrix ~]# cd /data/python_server/code/flask-blueprint/
[root@sparsematrix flask-blueprint]# vi runapp.ini
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[uwsgi]
; 启动程序时所使用的地址和端口,通常在本地运行flask项目,
; 地址和端口是127.0.0.1:5000,
; 不过在服务器上是通过uwsgi设置端口,通过uwsgi来启动项目,
; 也就是说启动了uwsgi,也就启动了项目。
; socket = 0.0.0.0:8000
; socket file's location
; socket 指定的是与 nginx 进行通信的端口文件
socket = /data/python_server/code/flask-blueprint/run/%n.sock
; 项目目录
chdir = /data/python_server/code/flask-blueprint
; flask程序的启动文件,通常在本地是通过运行python manage.py runserver 来启动项目的
wsgi-file = manage.py
; 程序内启用的application变量名
callable = app
; 处理器个数
processes = 4
; 线程个数
threads = 2
; 获取uwsgi统计信息的服务地址
; stats = 127.0.0.1:9191
#permissions for the socket file
chmod-socket = 666
#the variable that holds a flask application inside the module imported at line #6
callable = app
#location of log files
logto = /data/python_server/code/flask-blueprint/run/%n.log

启动uwsgi

1
uwsgi --ini runapp.ini &

All text

使用nginx承担flask的web服务

修改nginx的默认配置文件

1
vim /etc/nginx/conf.d/default.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
server {
listen 8088;
server_name localhost;

location /mystatus {
stub_status;
}

location / {
include uwsgi_params;
uwsgi_pass unix:///data/python_server/code/flask-blueprint/run/runapp.sock; # Nginx与uwsgi的交流方式
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}

平滑启动 nginx

1
nginx -s reload

在浏览器访问:

http://115.28.240.96:8088/hello

All text

http://115.28.240.96:8088/spider

All text

http://115.28.240.96:8088/data

All text

本文作者 : Matrix
原文链接 : https://matrixsparse.github.io/2017/11/07/Nginx+Flask+UWSGI/
版权声明 : 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!

知识 & 情怀 | 二者兼得

微信扫一扫, 向我投食

微信扫一扫, 向我投食

支付宝扫一扫, 向我投食

支付宝扫一扫, 向我投食

留下足迹