本实验实现了负载均衡、反向代理、动静分离,还实现了根据客户端设备user-agent进行转发,也就是移动端和PC端访问的页面不同。html
服务器:6台VM
操做系统:CentOS7
LB、www、wap:安装Nginx
uwsgi一、uwsgi2:安装nfs-utils、Python3解释器、virtualenv
NFS:安装NFS
MRCS:安装MySQL、Redis、virtualenv前端
注意:这里不介绍软件的安装
Nginx安装参考:http://www.javashuo.com/article/p-zmftgnof-es.html
NFS安装参考:http://www.javashuo.com/article/p-opamcbry-es.htmlpython
在LB服务器上面配置nginx
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服务web
/application/nginx/sbin/nginx
mkdir project # 配置挂载目录 cat /etc/exports # 配置内容 /project 172.16.1.*(rw,sync) # 重启NFS服务 systemctl restart rpcbind systemctl restart nfs
配置完成以后把项目上传到/project目录里面shell
在uwsgi1和uwsig2服务器上操做:windows
# 建立虚拟环境 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配置centos
[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启动命令api
# 启动uwsgi服务器 uwsgi --ini uwsgi.ini # 中止uwsgi服务器 uwsgi stop uwsgi.ini
处理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
处理移动端静态文件请求
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
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
在hosts文件里面添加本地解析
windows系统hosts文件路径:系统盘\windows\system32\drivers\etc
Linux系统和MAC系统hosts文件路径:/etc/hosts
172.16.1.10 www.meiduo.site api.meiduo.site
PC端访问效果
移动端访问效果这里其余功能测试就不演示了。