一 uwsgi介绍

uWSGI是一个Web服务器,它实现了WSGI协议,uwsgi, http等协议。 Nginx中HttpUwsgiMoule的作用是与uWSGI服务器进行交换

1 WSGI是一种Web服务器网关接口。它是一个Web服务器(如nginx,uWSGI等服务器)与web应用(如用Flask框架写的程序)通信的一种规范。
2 uwsgi是一种线路协议而不是通信协议,在此常用于在uWSGI服务器与其他网络服务器的数据通信。
3 而uWSGI是实现了uwsgi和WSGI两种协议的Web服务器。
3 uwsgi协议是一个uWSGI服务器自有的协议,它用于定义传输信息的类型(type of information),每一个uwsgi packet前4byte为传输信息类型描述,它与WSGI相比是两样东西。

uWSGI的主要特点如下:

超快的性能
低内存占用(实测为apache2的mod_wsgi的一半左右)
多app管理(终于不用冥思苦想下个app用哪个端口比较好了)
详尽的日志功能(可以用来分析app性能和瓶颈)
高度可定制(内存大小限制,服务一定次数后重启等)

二 Uwsgi 安装使用

# Install the latest stable release:
pip install uwsgi
# ... or if you want to install the latest LTS (long term support) release,
pip install https://projects.unbit.it/downloads/uwsgi-lts.tar.gz

基本测试

Create a file called test.py:

# test.py
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"] # python3
#return ["Hello World"] # python2

运行
uwsgi --http :8000 --wsgi-file test.py

用uwsgi 启动django

uwsgi --http :8000 --module [项目名称.wsgi]
实例:uwsgi --http 0.0.0.0:8080 --chdir /project/CNBlog/ --module cnblog.wsgi

可以把参数写到配置文件里

[uwsgi]
socket = 172.16.123.203:9000 # nginx链接的ip和端口
#http = 172.16.123.203:9000 # 访问HTTP协议监听IP和端口
# Django-related settings
# the django project directory (full path)
chdir = /project/SchoolInfomationSystem # 项目根目录
# Django's wsgi file
module = SchoolInfomationSystem.wsgi # django项目下的wsgi文件
# process-related settings
client_max_body_size 75M
# master
master = true
# maximum number of worker processes
processes = 4 # 开启4个进程(按需更改)
threads = 2 # 每个进程开启2个线程
max-requests = 6000
#daemonize = /var/log/uwsgi.log # 后台启动,并把日志记录到指定文件
# ... with appropriate permissions - may be needed
chmod-socket = 664
# clear environment on exit
vacuum = true
static-map = /static=/data/www/Order/web/static   # 静态文件
pidfile=/data/www/logs/order.pid            # pid存放位置
socket=/data/www/logs/order.sock            # socket存放位置
daemonize=/data/www/logs/order.log           # 日志文件

示例中用的是ini配置文件,如需使用xml配置,请另行百度xml配置文件。更多的参数使用也可以自行百度添加上去

当ini配置文件写好后执行
uwsgi --ini /project/crazye-uwsgi.ini #--ini 表示使用ini配置文件,xml文件就用--xm

常用选项:

http : 协议类型和端口号
processes : 开启的进程数量
workers : 开启的进程数量,等同于processes(官网的说法是spawn the specified number ofworkers / processes)
chdir : 指定运行目录(chdir to specified directory before apps loading)
wsgi-file : 载入wsgi-file(load .wsgi file)
stats : 在指定的地址上,开启状态服务(enable the stats server on the specified address)
threads : 运行线程。由于GIL的存在,我觉得这个真心没啥用。(run each worker in prethreaded mode with the specified number of threads)
master : 允许主进程存在(enable master process)
daemonize : 使进程在后台运行,并将日志打到指定的日志文件或者udp服务器(daemonize uWSGI)。实际上最常用的,还是把运行记录输出到一个本地文件上。
pidfile : 指定pid文件的位置,记录主进程的pid号。
vacuum : 当服务器退出的时候自动清理环境,删除unix socket文件和pid文件(try to remove all of the generated file/sockets)

三 Nginx的安装和配置

安装 nginx yum install -y nginx

再接下来要做的就是修改nginx.conf配置文件。打开/etc/nginx/nginx.conf文件,添加如下内容。

        server {
listen 8099;
server_name 127.0.0.1
charset UTF-8;
access_log /var/log/nginx/myweb_access.log;
error_log /var/log/nginx/myweb_error.log; client_max_body_size 75M; location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8000;
uwsgi_read_timeout 2;
}
location /static {
expires 30d;
autoindex on;
add_header Cache-Control private;
alias /home/fnngj/pydj/myweb/static/;
}
}

配置二

        # the upstream component nginx needs to connect to
upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:8001; # for a web port socket (we'll use this first)
} # configuration of the server
server {
# the port your site will be served on
listen 8000;
# the domain name it will serve for
server_name .example.com; # substitute your machine's IP address or FQDN
charset utf-8; # max upload size
client_max_body_size 75M; # adjust to taste # Django media
location /media {
alias /path/to/your/mysite/media; # your Django project's media files - amend as required
} location /static {
alias /path/to/your/mysite/static; # your Django project's static files - amend as required
} # Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /path/to/your/mysite/uwsgi_params; # the uwsgi_params file you installed
}
}

listen 指定的是nginx代理uwsgi对外的端口号。

server_name 网上大多资料都是设置的一个网址(例,www.example.com),我这里如果设置成网址无法访问,所以,指定的到了本机默认ip。

在进行配置的时候,我有个问题一直想不通。nginx到底是如何uwsgi产生关联。现在看来大概最主要的就是这两行配置。
include uwsgi_params;
uwsgi_pass 127.0.0.1:8000; 注意要和wsgi监听的ip和端口一致
include 必须指定为uwsgi_params;而uwsgi_pass指的本机IP的端口与myweb_uwsgi.ini配置文件中的必须一直。

Django+Uwsgi+Nginx部署的更多相关文章

  1. virtualvenv+django+uWSGI+nginx 部署

    原创博文 转载请注明出处! 1. virtualvenv 2. django 3. uWSGI 4. nginx 5. 踩坑记录 1. virtualvenv virtualvenv install ...

  2. django+uwsgi+nginx部署(非常详细)

    django+uwsgi+nginx部署 1.介绍: 在网上看了很多教程,但自己部署了很久都没有成功,这篇博文记录自己所踩过得坑. 2.环境: Ubuntu 16.04.1 LTS (GNU/Linu ...

  3. Linux 集群概念 , wsgi , Nginx负载均衡实验 , 部署CRM(Django+uwsgi+nginx), 部署学城项目(vue+uwsgi+nginx)

    Linux 集群概念 , wsgi , Nginx负载均衡实验 , 部署CRM(Django+uwsgi+nginx), 部署学城项目(vue+uwsgi+nginx) 一丶集群和Nginx反向代理 ...

  4. Django+uWSGI+Nginx 部署网站

    Django 1.11设置 保证Django在本地调试没有问题: 当然这是前提^_^ 收集静态文件至指定文件夹 Django静态文件设置具体参考:https://docs.djangoproject. ...

  5. Ubuntu下Django+uWSGI+nginx部署

    本文采用uwsgi+nginx来部署django 这种方式是将nginx作为服务端前端,将接受web所有的请求,统一管理,Nginx把所有的静态请求自己处理,然后把所有非静态请求通过uwsgi传递给D ...

  6. virtualvenv+django+uWSGI+nginx 部署 踩坑记录

    原创博文 转载请注明出处! uwsgi: unrecognized option '--http:8089' uwsgi: unrecognized option '--http' uwsgi trk ...

  7. django+uwsgi+nginx 部署生产环境

    一.Uwsgi安装 python3 -m pip install uwsgi cp /usr/local/python3/bin/uwsgi /usr/bin/ 测试 在django项目主目录下cre ...

  8. Ubuntu+Django+uWSGI+Nginx部署Django项目

    安装uWSGI,pip依据自己要使用的python版本自行选择,python2.x版本使用pip进行安装,python3.x版本使用pip3进行安装 pip install uwsgi 配置uWSGI ...

  9. Django Uwsgi Nginx 部署

    1.django的settings配置 参照博客 https://www.cnblogs.com/xiaonq/p/8932266.html # 1.修改配置 # 正式上线关闭调试模式, 不会暴露服务 ...

随机推荐

  1. git使用 从远程库克隆和更新到本地

    从远程库克隆到本地. git clone git@github.com:kingbook/Framework.git 或 git clone http://github.com/kingBook/Fr ...

  2. Haskell语言学习笔记(85)Async

    安装 async $ cabal install async async-2.2.1 installed async / wait / concurrently async :: IO a -> ...

  3. JDBC是如何执行SQL脚本的

    (1) 加载JDBC驱动程序: Cllass.forName(" 驱动程序" );   //你要连接的数据库对象 (2) 建立连接 Connection conn=DriverMa ...

  4. Linux下MySQL5.7.18二进制包安装(手动添加配置文件my_default.cnf)

    本文出处:http://www.cnblogs.com/wy123/p/6815049.html 最新在学习MySQL,纯新手,对Linux了解的也不多,因为是下载的最新版的MySQL(MySQL5. ...

  5. Unity3d资源管理分析

    原创链接:http://blog.csdn.net/ox_thedarkness/article/details/9197453 分离资源管理 参考 1.Unity3D占用内存太大的解决方法 - 星尘 ...

  6. vs2017中char* str = "1234asd56";会报错,——const char*类型的值不能用于初始化char*类型的实体

    原因: "1234asd56"是常量 ,正确的写法本身就是:const char* str = "1234asd56"; 之所以之前的vs版本可以写成char* ...

  7. Incompatible shapes during the half way training---Invalid argument: Incompatible shapes: [1,63,4] vs. [1,64,4]

    这是tensorflow model 中我使用它的faster--cnn,但是就是训练过程中,代码执行到一半 一般是step=40~120的时候就报错了: INFO:tensorflow:global ...

  8. Sql中EXISTS与IN的使用及效率

    in 和exists 对于以上两种查询条件,in是把外表和内表作hash 连接,而exists 是对外表作loop 循环,每次loop 循环再对内表进行查询. 一直以来认为exists 比in 效率高 ...

  9. 修复回写PR时到料日期重复扣减检验周期的问题:

    问题描述: 修复回写PR时到料日期重复扣减检验周期的问题:系统回写的外购半成品PR交货日期未按采购周期回写,从8-10日开始均于10天交期回写,例以下9-5日今天回写的PR,采购周期12天,结果回写到 ...

  10. 用户维护 UI 检验周期更新逻辑

    在SAP系统中建立UI,供用户维护物料组对应的检验周期,FP按照物料组对应的物料编码取UI维护的检验周期进FP系统规划. --物料组对应检验周期维护表(新UI)add by landor on 201 ...