192.168.10.147nginx前端代理服务器,192.168.10.140做后端lamp1192.168.10.148lamp2mysql服务器安装在140上,148做为一个数据库客户端来访问140,现在要实现的需求是访问192.168.10.147会自动均衡到后端的两台lamp上,并且访问静态页面直接从本地的nginx来解析,访问动态页面会发送到后端的lamp

wKioL1XYTYPC-8iBAADBSHIZXEI484.jpg

一、安装环境

147上操作:

下载nginx  wget  http://nginx.org/download/nginx-1.4.7.tar.gz

tar –zxvf nginx-1.4.7.tar.gz

cd nginx-1.4.7

yum install  pcre-devel  -y  安装pcre库,支持rewrite 重写

注:安装源码的pcre时,指定pcre路径为解压的路径,而不是编译后的路径,否则会报错

yum install  -y openssl-devel

useradd -s  /sbin/nologing   -M  www

./configure --user=www  --group=www --prefix=/usr/local/nginx --with-http_stub_status_module  --with-http_ssl_module

make  &&   make  install

 

140148上搭建好lamp环境,可以参考http://pc1990.blog.51cto.com/10541224/1682753

140上操作

搭建discuz论坛:

mysql  #进入mysql数据库

grant all on discuz.* todiscuz@'192.168.10.140' identified by '123456';  

##创建一个普通用户discuz,并给它discuz数据库的所有权限

grant all on discuz.* todiscuz@'192.168.10.148' identified by '123456';

148同样的权限

discuz论坛的源码放到/usr/local/apache/htdocs/ 下,并将htdocs的目录的所属主,所属组改为运行apache的账户,然后安装即可

wKiom1XYS5ShUL9eAAFElCfEiDo410.jpg

安装完成后在140上把网站源代码同步到148上:

rsync -r /usr/local/apache/htdocs/* root@192.168.10.148:/usr/local/apache/htdocs/

 

然后在148上把htdocs目录的所属主,所属组改为运行apache的账户

这时候访问140148时候都是同一个论坛

wKioL1XYTb2wmgHdAAH4DgT0QXI019.jpg

wKioL1XYTcmBU6VjAAIRC84w57I871.jpg


二、做负载均衡:

147上编辑nginx的配置文件/usr/local/nginx/conf/nginx.conf

加入一段:

upstream web1 {

       server 192.168.10.140 weight=1 max_fails=2 fail_timeout=30s;

       server 192.168.10.148 weight=1 max_fails=2 fail_timeout=30s;

}

启用代理:

wKiom1XYS-aCVlX8AAEoAyDFH6w875.jpg

然后保存配置文件,启动nginx  /usr/local/nginx/bin/nginx

这时候访问192.168.10.147,就会将请求平均分配给后端的lamp

 

三、做动静分离

先将网站代码文件拷贝一份到nginx的发布目录

rsync -r /usr/local/apache/htdocs/* root@192.168.10.147:/usr/local/nginx/html/

编辑nginx配置文件,在server模块下加一段location匹配静态文件走本地解析:

wKioL1XYThTREGKNAACtOfEHPFM923.jpg

保存,重新加载nginx的配置文件/usr/local/nginx/sbin/nginx  -s  reload

这样静态就会走本地的nginx解析,其他文件就会走后端的代理

可以做个实验,在147140,148上各做个静态文件1.html,分别写入

this is 147 this is 140   this  is 148

然后访问192.168.10.147/1.html,无论怎么访问都是this is 147,如果把147上的1.html删掉,就会404,这是因为静态文件只会走本地nginx来解析,不会走代理。至此nginx负载均衡,动静分离做完


附:完整的nginx配置文件如下:

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    upstream web1 {
        server 192.168.10.140 weight=1 max_fails=2 fail_timeout=30s;
        server 192.168.10.148 weight=1 max_fails=2 fail_timeout=30s;
}
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
            proxy_set_header Host  $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass  http://web1;
        }
        location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
                {
                        root /usr/local/nginx/html;
                        expires      1d;
                }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}