本實驗實現了負載均衡、反向代理、動靜分離,還實現了根據客戶端設備user-agent進行轉發,也就是移動端和PC端訪問的頁面不一樣。
1. 項目部署邏輯圖
2. 環境準備
服務器:6臺VM
操作系統:CentOS7
LB、www、wap:安裝Nginx
uwsgi1、uwsgi2:安裝nfs-utils、Python3解釋器、virtualenv
NFS:安裝NFS
MRCS:安裝MySQL、Redis、virtualenv
注意:這裏不介紹軟件的安裝
Nginx安裝參考:http://blog.51cto.com/ljmict/2150752
NFS安裝參考:http://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. 配置uwsgi1和uwsgi2服務器
在uwsgi1和uwsig2服務器上操作:
# 創建虛擬環境
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
uwsgi1配置
[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
uwsgi2配置
[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服務器(資源有限,所以把這些東西安裝在一臺服務器上面)
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文件路徑:系統盤\windows\system32\drivers\etc
Linux系統和MAC系統hosts文件路徑:/etc/hosts
172.16.1.10 www.meiduo.site api.meiduo.site
PC端訪問效果
移動端訪問效果
這裏其他功能測試就不演示了。