Nginx+uwsgi+celery+supervisor導入Django前後端分離プロジェクト

8286 ワード

本実験では,負荷等化,逆エージェント,動静分離を実現し,クライアントデバイスuser-agentによる転送,すなわちモバイル側とPC側でアクセスするページの違いを実現した.
1.プロジェクト配置論理図
2.環境準備
サーバ:6台VMオペレーティングシステム:CentOS 7 LB、www、wap:インストールNginxuwsgi 1、uwsgi 2:インストールnfs-utils、Python 3解釈器、virtualenvNFS:インストールNFSMRCS:インストールMySQL、Redis、virtualenv
注意:ここではソフトウェアのインストールNginxインストールの参考を紹介しません:https://blog.51cto.com/ljmict/2150752NFSインストールリファレンス:https://blog.51cto.com/ljmict/1919738
3.ロードバランシングとリバースプロキシの構成
LBサーバ上での構成
cat nginx.conf
#       
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    upstream static_pools {
    #         
        server 172.16.1.13:80 weight=1;
    }
    upstream meiduo {
    #         
        server 172.16.1.11:8001 weight=1;
        server 172.16.1.12:8001 weight=1;
    }
    upstream iphone_pools {
    #        
        server 172.16.1.16:80 weight=1;
    }
    server { #   api   
        listen       8000;
        server_name  api.meiduo.site;
        location / {
           include uwsgi_params;
           uwsgi_pass meiduo;
        }
        access_log off;
    }
    server { #         
        listen       80;
        server_name  www.meiduo.site;
        location /xadmin {
           #     url :http://www.meiduo.site/xadmin   meiduo 
           include uwsgi_params;
           uwsgi_pass meiduo;
        }
        location /cheditor {
           #     url :http://www.meiduo.site/cheditor   meiduo 
           include uwsgi_params;
           uwsgi_pass meiduo;
        }
        location / {
          #   uri /       location      ,  :/static
           if ($http_user_agent ~* "iphone")
           #        user_agent iphone,   iphone_pools 
           {
              proxy_pass http://iphone_pools;
           }
          # PC      static_pools 
           proxy_pass http://static_pools;
        }
        access_log off;
    }
}

nginxサービスの開始
/application/nginx/sbin/nginx

4.NFSサーバー構成
mkdir project
#       
cat /etc/exports
#     
/project 172.16.1.*(rw,sync)
#   NFS  
systemctl restart rpcbind
systemctl restart nfs

構成が完了したら/projectディレクトリにプロジェクトをアップロードします
4.uwsgi 1およびuwsgi 2サーバの構成
uwsgi 1およびuwsig 2サーバで操作:
#       
mkvirtualenv -p python3 meiduo
#    meiduo    
workon mediuo
#   uwsgi 
pip install uwsgi
mkdir /project
#   NFS   /project   uwsgi    /project
mount -t nfs 172.16.1.14:/project /project
#            
df -h
#   
                                  %    
/dev/mapper/centos-root   36G  2.4G   33G    7% /
devtmpfs                 226M     0  226M    0% /dev
tmpfs                    237M     0  237M    0% /dev/shm
tmpfs                    237M  8.8M  228M    4% /run
tmpfs                    237M     0  237M    0% /sys/fs/cgroup
/dev/sda1               1014M  143M  872M   15% /boot
tmpfs                     48M     0   48M    0% /run/user/0
172.16.1.14:/project      17G  1.8G   16G   11% /project
#         pip 
cd /project/meiduo/meiduo_mall
pip install -r requirements.txt  #              pip freeze >requirements.txt   
cd /root
#   uwsgi.ini  
touch uwsgi.ini

uwsgi 1構成
[uwsgi]
#  nginx     ,Django         
socket=172.16.1.11:8001
#   web     ,Django         
#http=172.16.1.11:8000
#    
chdir=/project/meiduo/meiduo_mall
#   wsgi.py     ,       
wsgi-file=meiduo_mall/wsgi.py
#    
processes=4
#    
threads=2
# uwsgi      
master=True
#          
pidfile=uwsgi.pid
#     ,  uwsgi           ,     。     runserver      
daemonize=uwsgi.log
#          
virtualenv=/root/.virtualenvs/meiduo

uwsgi 2構成
[uwsgi]
#  nginx     ,Django         
socket=172.16.1.12:8001
#   web     ,Django         
#http=172.16.1.12:8000
#    
chdir=/project/meiduo/meiduo_mall
#   wsgi.py     ,       
wsgi-file=meiduo_mall/wsgi.py
#    
processes=4
#    
threads=2
# uwsgi      
master=True
#          
pidfile=uwsgi.pid
#     ,  uwsgi           ,     。     runserver      
daemonize=uwsgi.log
#          
virtualenv=/root/.virtualenvs/meiduo

uwsgi起動コマンド
#   uwsgi   
uwsgi --ini uwsgi.ini
#   uwsgi   
uwsgi stop uwsgi.ini

5.wwwサーバ
PC側静的ファイル要求の処理
cat nginx.conf
#     
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    sendfile        on;
    keepalive_timeout  65;
    server {
       listen 80;
       server_name www.meiduo.site;
       location / {
          root html/front_end_pc; 
          #     ,        Nginx     html   ,       /application/nginx/html
          index index.html index.htm;
       }
       access_log logs/access_www.log main;
    }
}

nginxサービスの開始
/application/nginx/sbin/nginx

6.wapサーバ
モバイル側の静的ファイルリクエストの処理
cd /application/nginx/html
#            ,               
mkdir wap
echo "mobile_page" > wap/index.html

Nginx構成
cat nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    sendfile        on;
    keepalive_timeout  65;
    server {
       listen 80;
       server_name www.meiduo.site;
       location / {
          root html/wap;
          #     ,        Nginx     html   ,       /application/nginx/html
          index index.html index.htm;
       }
       access_log logs/access_www.log main;
    }
}

nginxサービスの開始
/application/nginx/sbin/nginx

7.MRCSサーバ(リソースが限られているので、これらを1台のサーバにインストール)
mkdir /project
#   NFS   /project   uwsgi    /project
mount -t nfs 172.16.1.14:/project /project
#            
df -h
#   
                                  %    
/dev/mapper/centos-root   36G  2.4G   33G    7% /
devtmpfs                 226M     0  226M    0% /dev
tmpfs                    237M     0  237M    0% /dev/shm
tmpfs                    237M  8.8M  228M    4% /run
tmpfs                    237M     0  237M    0% /sys/fs/cgroup
/dev/sda1               1014M  143M  872M   15% /boot
tmpfs                     48M     0   48M    0% /run/user/0
172.16.1.14:/project      17G  1.8G   16G   11% /project
#       
mkvirtualenv -p python3 meiduo
#    meiduo    
workon mediuo
#         pip 
cd /project/meiduo/meiduo_mall
pip install -r requirements.txt  #              pip freeze >requirements.txt   

supervisorの構成
mkvirtualenv -p python2 supervisor
#     python2     ,  supervisor   python3
workon supervisor
pip install supervisor
#   supervisor    
echo_supervisord_conf > /etc/supervisord.conf
#       
mkdir /var/log/celery

celeryを作成します.iniファイル
cat /etc/celery.ini
#     
[program:celery]
# celery       
command=/root/.virtualenvs/meiduo/bin/celery -A celery_tasks.main worker -l info
#     
directory=/project/meiduo/meiduo_mall
#       
stdout_logfile=/var/log/celery/celery.log
#     
autorestart=true
#      true,            supervisord            
redirect_stderr=true

変更/etc/supervisord.confファイル
[include]
files = celery.ini

supervisorコマンド
#                 。
supervisord
#        supervisord   
supervisorctl update
#          
supervisorctl

8.アクセステスト
hostsファイルにローカル解析windowsシステムhostsファイルパスを追加する:システムディスクwindowssystem 32driversetcLinuxシステムとMACシステムhostsファイルパス:/etc/hosts
172.16.1.10 www.meiduo.site api.meiduo.site

PC側アクセス効果モバイル側アクセス効果ここで他の機能テストはデモしません.
転載先:https://blog.51cto.com/ljmict/2155986