• nginx + uWSGI 提高 Django的并发性


    1. uWSGI :
    uWSGI是一个web服务器,实现了WSGI协议、uwsgi协议、http协议等。
    uWSGI的主要特点是:
    超快的性能
    低内存占用
    多app管理
    详尽的日志功能(可以用来分析app的性能和瓶颈)
    高度可定制(内存大小限制,服务一定次数后重启等)
    uWSGI服务器自己实现了基于uwsgi协议的server部分,我们只需要在uwsgi的配置文件中指定application的地址,uWSGI就能直接和应用框架中的WSGI application通信。
    2. nginx :
    Nginx 是一个高性能的负载均衡HTTP和反向代理服务器,Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件代理服务器。
    特点是占有内存少,并发能力强。
    结构与扩展:一个主进程和多个工作进程。工作进程是单线程的,且不需要特殊授权即可运行;
    3. nginx和uWSGI的关系:
    nginx相当于是服务器,负责接收请求
    uwsgi是服务器和服务端应用程序的通信协议,规定了怎么把请求转发给应用程序和返回
    2个基本概念:
    服务器(接收请求),应用程序(处理请求并返回)
    通信过程:
    客户端发送一个http请求,被nginx服务器接收,nginx服务器将请求转发给uwsgi,uwsgi将请求转发给实现uwsgi
    协议的应用程序(flask,gunicorn等等)
    4. uWSGI的安装:
    # pip3 install uwsgi
    2、创建django项目并配置uWSGI配置文件:
    cd 进入到 django 的主目录
    vi test-uwsgi.ini # 添加以下配置文件,按需修改即可:
    [uwsgi]
    # 对外提供 http 服务的端口
    http = :9000
    #用于和 nginx 进行数据交互的端口
    socket = 127.0.0.1:8001
    # django 程序的主目录。
    chdir = /project/django
    # Django's wsgi file
    wsgi-file = /project/django_test/django_test/wsgi.py
    # 最大的工作进程数
    processes = 4
    #在每个进程中的最大线程数
    threads = 2
    # 通过该端口可以监控 uwsgi 的负载情况
    stats = 127.0.0.1:9999
    # 清理环境出口
    vacuum = true
    # 后台运行,并输出日志
    daemonize = /var/log/uwsgi.log
    3、通过 uwsgi 配置文件启动 django 程序
    uwsgi test-uwsgi.ini # 在浏览器中 通过访问 http://ip:9000 可以看到发布的 django 程序
    如果uwsgi没有写入环境变量,需要写绝对路径/usr/local/python3/bin/uwsgi
    查看uwsgi的启动进程状态
    netstat -lnpt | grep uwsgi
    4、安装nginx并配置 :
    yum install nginx
    配置 nginx 的配置文件
    在 django 的主目录下创建下面的 nginx 配置文件,然后做软连接到 nginx 的配置文件目录,或者直接在 nginx 配置文件中添加内容
    默认配置文件路径在/etc/nginx/nginx.conf
    配置内容
    # For more information on configuration, see:
    # * Official English Documentation: http://nginx.org/en/docs/
    # * Official Russian Documentation: http://nginx.org/ru/docs/

    user nginx;
    worker_processes auto;
    error_log /var/log/nginx/error.log;
    pid /run/nginx.pid;

    # Load dynamic modules. See /usr/share/nginx/README.dynamic.
    include /usr/share/nginx/modules/*.conf;

    events {
    worker_connections 1024;
    }

    http {
    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
    '$status $body_bytes_sent "$http_referer" '
    '"$http_user_agent" "$http_x_forwarded_for"';

    access_log /var/log/nginx/access.log main;

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    # 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 10.1.62.123; # 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 /opt/jira_platform/static; # django项目静态文件的路径
    }

    # Finally, send all non-media requests to the Django server.
    location / {
    uwsgi_pass 127.0.0.1:8001; #注意这个ip地址要和uwsgi配置文件里的对应
    include uwsgi_params; # the uwsgi_params file you installed
    }
    }
    }

    5.2 重启nginx 服务
    [root@crazy-acong django_test]# nginx -t
    nginx: the configuration file /data/application/nginx-1.10.3/conf/nginx.conf syntax is ok
    nginx: configuration file /data/application/nginx-1.10.3/conf/nginx.conf test is successful
    [root@crazy-acong django_test]# nginx -s reload
    [root@crazy-acong django_test]# netstat -lnpt | grep 8000
    tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 43492/nginx
    这个时候就可以通过 http://ip:8000 访问 django 程序了,如果发现静态文件貌似没读取到,需要查看静态文件目录路径是否正确
    可以通过下面的方法解决静态文件的问题
    在 django 程序的 settings.py 文件中添加以下内容
    STATIC_ROOT = os.path.join(BASE_DIR, "static_all") #static文件的目录
    # 然后通过执行该命令,将静态文件整合到一个目录中
    [root@crazy-acong django_test]# python3 manage.py collectstatic
    最后重启 nginx 服务即可

  • 相关阅读:
    docker进入容器命令
    docker复制文件到容器内以及从容器内复制文件到宿主机
    在idea中创建maven父子工程,子工程无法导入父工程依赖的问题
    maven merge 其他分支比如master的方法
    Maven 右边的maven 项目为空 pom文件
    Spark Streaming集成Kafka调优
    spark sql/hive小文件问题
    CompletableFuture详解
    样式绑定styleBinding
    jsonArray图片数组实例
  • 原文地址:https://www.cnblogs.com/slqt/p/10825771.html
Copyright © 2020-2023  润新知