1. 概念解析(wsgi协议,uwsgi协议,uWSGI)

  参考:https://www.cnblogs.com/wspblog/p/8575101.html 

      1.1 现实世界的web请求:

uwsgi+nginx部署django项目

  1.2  wsgi协议,uwsgi协议和uWSGI

     a. WSGI(wsgi): 全称 Web Server Gateway Interface,或者 Python Web Server Gateway Interface ,是为 Python 语言定义的 Web 服务器和 Web 应用程序或框架之间的一种简单而通用的接口。从名字就可以看出来,这东西是一个Gateway,也就是网关。网关的作用就是在协议之间进行转换。

    总结:WSGI(wsgi)只是一种规范,描述web server如何与web application通信的规范

    b. uWSGI: uWSGI是一个Web服务器,它实现了WSGI协议、uwsgi、http等协议。

    c. uwsgi: 与WSGI一样是一种通信协议,但与WSGI协议是两种东西,uwsgi协议是uWSGI服务器的独占协议,用于定义传输信息的类型(type of                                    information),每一个uwsgi packet前4byte为传输信息类型的描述.

    下图简单描述在web请求过程中三者的位置: uwsgi+nginx部署django项目

2. web请求流程

  1,首先客户端请求服务资源,

     2,nginx作为直接对外的服务接口,接收到客户端发送过来的http请求,会解包、分析,如果是静态文件请求就根据nginx配置的静态文件目录,返回请求的资      源,如果是动态的请求,nginx就通过配置文件,将请求传递给uWSGI;

  3,uWSGI 将接收到的包进行处理,并转发给wsgi, wsgi根据请求调用django工程的某个文件或函数,处理完后django将返回值交给wsgi,wsgi将返回值进行打包,转发给uWSGI, uWSGI接收后转发给nginx,nginx最终将返回值返回给客户端(如浏览器)。

    *注:不同的组件之间传递信息涉及到数据格式和协议的转换

  整个流程如下图所示:

uwsgi+nginx部署django项目

  nginx的作用:
    1. 第一级的nginx并不是必须的,uwsgi完全可以完成整个的和浏览器交互的流程;
    2. 在nginx上加上安全性或其他的限制,可以达到保护程序的作用;
    3. uWSGI本身是内网接口,开启多个work和processes可能也不够用,而nginx可以代理多台uWSGI完成uWSGI的负载均衡;
    4. django在debug=False下对静态文件的处理能力不是很好,而用nginx来处理更加高效。

3. 部署环境及准备

    购买服务器(国外谷歌云,亚马逊aws;国内阿里云,腾讯云等),需安装:python,django,uwsgi、nginx、libmysqld-dev、mysql-server;

  3.1. 安装和测试uWSGI  

安装
sudo pip install uwsgi

测试:
新建test.py,内容如下

#/usr/bin/python
def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return "Hello World"

运行 uwsgi --http 0.0.0.0:8000 --wsgi-file test.py
浏览器访问 http://127.0.0.1:8000   页面出现Hello World,则安装成功

(问题:若8000端口被占用,换其他端口; uwsgi找不到命令,建立软连接 sudo ln -s /your-python-dir/bin/uwsgi /usr/bin/uwsgi)

  3.2  安装和测试nginx

    安装:sudo yum install nginx

    启动:sudo nginx         (ps -aux| grep nginx 查看nginx进程)

    测试:浏览器访问http://127.0.0.1:80 (命令行 curl http://127.0.0.1:80),返回nginx则运行正常

    其他nigix命令:

      启动:nginx  

      退出或重启:nginx -s stop(quit, reload)

      命令帮助: nginx -h

            验证配置文件 nginx -t

   3.3 建立django 项目和测试

    创建项目:django-admin.py startproject notebook  (找不到命令时 :/usr/local/python-3.5.2/bin/django-admin.py startproject notebook)

    创建app:python manage.py startapp planlist          (并在settings.py文件的INSTALLED_APPS中添加planlist.apps.PlanlistConfig)

    运行测试: python manage.py runserver 127.0.0.1:8000   (浏览器打开http://127.0.0.1:8000, 出现django页面,项目创建成功) 

      3.4 uWSGI运行django项目测试

    关闭上述django项目后,运行命令:uwsgi --http :8000 --module notebook.wsgi   

      (notebook为django项目名称,创建django项目时notebook文件夹下自动创建wsgi.py文件)

    测试:在浏览器访问http://127.0.0.1:8000,出现django页面,表示web-client <-> uWSGI <-> Django 是连通的。(就剩下nginx了)

    (注意:1. --http和 :8000之间有空格,否则会报错unrecognized ...

       2. 在项目根目录下运行上述命令,即notebook项目文件夹下,否则报错import error notebook.wsgi)

  3.5 nginx配置和测试

    找到nginx默认的配置文件路径:/etc/nginx/nginx.conf( 通过命令可以查看:sudo nginx -t),确保nginx.conf的同目录下有uwsgi_params文件(/etc/nginx/uwsgi_params),没有的话根据链接获取。

    在django项目目录下,创建单独的nginx配置文件notebook.conf (notebook/notebook/notebook.conf),将nginx.conf的内容复制到notebook.conf,将其中的server部分全部替换成下面:


server {
        listen       80;
        server_name  localhost;
        charset       utf-8;
        access_log    /home/gCloud/djangoProject/notebook/notebook/nginx_access.log;
        error_log    /home/gCloud/djangoProject/notebook/notebook/nginx_error.log;
        client_max_body_size    75M;

        # Load configuration files for the default server block.

        location /static {
                alias /home/gCloud/djangoProject/notebook/collect_static;
        }

        location / {
                include /etc/nginx/uwsgi_params;
                uwsgi_pass 127.0.0.1:9090;
    }

}

     其中的 listen 80代表服务器开放80端口;

  access_log 和error_log是定义nginx访问日志和错误日志的存放路径。

  location [目录名]代表项目路径的引导;
    “location /static”中的”/static”是自己定义的项目引用静态文件时,浏览器中显示的静态资源所在的根目录名;这样的话,用户在浏览器中查看到的所有      image,css或js资源都是处在http://127.0.0.1/static下的。(django静态文件的绝对路径是根据自己的实际情况来确定的,一般在自己的django的app/static目录下,或自己python manage.py collectstatic后的路径下。)

  如果还有media文件之类的静态目录,仿照static的写法,自己补充。

  “location /”是指访问项目根目录时,nginx要做的事。其中需要指定 uwsgi_params文件的绝对路径,上面已经提到了;

  127.0.0.1:9090是指uWSGI绑定的监听地址,这里使用了9090端口。

  nginx配置参考及含义:

# ocean_monitor.conf

# the upstream component nginx needs to connect to
upstream django_ocean_monitor {
    # server unix:///path/to/your/mysite/mysite.sock; # for a file socket
    # for a web port socket (we'll use this first)
    server 127.0.0.1:8108;
}

# configuration of the server
server {
    # the port your site will be served on
    listen      8008;
    # the domain name it will serve for
    # substitute your machine's IP address or FQDN
    # Django 的 settings.py 文件中的 ALLOWED_HOSTS 要加上这里设置的 server_name
    server_name localhost;
    charset     utf-8;

    gzip on;
    gzip_min_length 1000;
    gzip_buffers 4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 3;
    gzip_vary on;
    # 禁用对 IE 6 使用 gzip 压缩
    gzip_disable "MSIE [1-6]\.";
    gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml application/xml+rss application/json;

    ## Individual nginx logs
    access_log  /var/log/nginx/ocean_monitor_access.log;
    error_log   /var/log/nginx/ocean_monitor_error.log;

    # max upload size
    client_max_body_size 8M;   # adjust to taste

    # Django media
    location /media  {
        # your Django project's media files - amend as required
        alias /home/python/ocean_monitor/media;  
    }

    location /static {
        # your Django project's static files - amend as required
        alias /home/python/ocean_monitor/static; 
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django_ocean_monitor;
        # the uwsgi_params file you installed
        #  增加 nginx 配置, uwsgi_params 文件在 /etc/nginx/ 目录下
        include     /etc/nginx/uwsgi_params; 
    }
}
nginx配置参考 

相关文章:

  • 2020-05-06
  • 2019-11-21
  • 2021-11-14
  • 2022-12-23
  • 2021-09-03
  • 2022-12-23
  • 2022-12-23
  • 2022-01-10
猜你喜欢
  • 2021-07-01
  • 2022-02-26
  • 2021-10-10
  • 2022-12-23
  • 2021-06-21
  • 2022-12-23
  • 2021-12-11
相关资源
相似解决方案