nginx均衡后端两台lamp,并做动静分离
2015-08-22 18:18
881 查看
192.168.10.147做nginx前端代理服务器,192.168.10.140做后端lamp1,192.168.10.148做lamp2,mysql服务器安装在140上,148做为一个数据库客户端来访问140,现在要实现的需求是访问192.168.10.147会自动均衡到后端的两台lamp上,并且访问静态页面直接从本地的nginx来解析,访问动态页面会发送到后端的lamp
一、安装环境
在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
在140和148上搭建好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的账户,然后安装即可
安装完成后在140上把网站源代码同步到148上:
rsync -r /usr/local/apache/htdocs/* root@192.168.10.148:/usr/local/apache/htdocs/
然后在148上把htdocs目录的所属主,所属组改为运行apache的账户
这时候访问140和148时候都是同一个论坛
二、做负载均衡:
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;
}
启用代理:
然后保存配置文件,启动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匹配静态文件走本地解析:
保存,重新加载nginx的配置文件/usr/local/nginx/sbin/nginx -s reload
这样静态就会走本地的nginx解析,其他文件就会走后端的代理
可以做个实验,在147和140,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配置文件如下:
一、安装环境
在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
在140和148上搭建好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的账户,然后安装即可
安装完成后在140上把网站源代码同步到148上:
rsync -r /usr/local/apache/htdocs/* root@192.168.10.148:/usr/local/apache/htdocs/
然后在148上把htdocs目录的所属主,所属组改为运行apache的账户
这时候访问140和148时候都是同一个论坛
二、做负载均衡:
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;
}
启用代理:
然后保存配置文件,启动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匹配静态文件走本地解析:
保存,重新加载nginx的配置文件/usr/local/nginx/sbin/nginx -s reload
这样静态就会走本地的nginx解析,其他文件就会走后端的代理
可以做个实验,在147和140,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; } } }
相关文章推荐
- nginx代理指定目录
- 访问Nginx发生SSL connection error的一种情况
- Nginx+Naxsi部署专业级Web应用防火墙
- CentOS 6.2实战部署Nginx+MySQL+PHP
- nginx中http核心模块的配置指令2
- 负载均衡技术沙龙1期(关于咱的图片)
- 负载均衡沙龙活动第二期现场问答汇集
- 网站502与504错误分析
- 艰难完成 nginx + puma 部署 rails 4的详细记录
- 把Lua编译进nginx步骤方法
- 浅谈sqlserver的负载均衡问题
- web 应用中常用的各种 cache详解
- Linux系统上配置Nginx+Ruby on Rails+MySQL超攻略
- window+nginx+php环境配置 附配置搭配说明
- PHP开发负载均衡指南
- Win2003利用dfs(分布式文件系统)在负载均衡下的文件同步配置方案
- LVS+Keepalived构建高可用负载均衡(测试篇)
- 解析CI即CodeIgniter框架在Nginx下的重写规则
- 简单测试Apache是如何完成负载均衡策略配置