Nginx+Flask+UWSGI

来源:互联网 发布:windows loader win8 编辑:程序博客网 时间:2024/06/06 04:16
使用的代理一共有两个,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:稳定版
[root@sparsematrix ~]# vim /etc/yum.repos.d/nginx.repo
[nginx]name=nginx repobaseurl=http://nginx.org/packages/centos/7/$basearch/gpgcheck=0enabled=1

All text

查看yum源中nginx相关版本

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

使用yum源安装Nginx

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

查看当前安装nginx版本

[root@sparsematrix ~]# nginx -v

All text

查看Nginx编译参数

[root@sparsematrix ~]# nginx -V

All text

启动nginx

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

All text

关闭nginx

nginx -s stop

平滑启动 nginx

nginx -s reload

安装uWSGI

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

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

[root@sparsematrix ~]# cd /data/python_server/code/flask-blueprint/[root@sparsematrix flask-blueprint]# vi runapp.ini
[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 filechmod-socket = 666#the variable that holds a flask application inside the module imported at line #6callable = app#location of log fileslogto = /data/python_server/code/flask-blueprint/run/%n.log

启动uwsgi

uwsgi --ini runapp.ini &

All text

使用nginx承担flask的web服务

修改nginx的默认配置文件

vim /etc/nginx/conf.d/default.conf
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

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

原创粉丝点击