1.安装varnish
server1下载软件并安装
jemalloc-3.6.0-1.el7.x86_64.rpm varnish-4.0.5-1.el7.x86_64.rpm
jemalloc-devel-3.6.0-1.el7.x86_64.rpm varnish-libs-4.0.5-1.el7.x86_64.rpm
修改配置文件
vim /etc/varnish/default.vcl
backend web1 {
.host = "172.25.14.2";
.port = "80";
}
backend web2 {
.host = "172.25.14.3";
.port = "80";
}
sub vcl_recv {
if (req.http.host ~ "^(www.)?westos.org") {
set req.http.host = "www.westos.org";
set req.backend_hint = web1;
#return(pass);
}
elseif (req.http.host ~ "^bbs.westos.org") {
set req.backend_hint = web2;
#return(pass);
}
else {
return(synth(404,"Not in cache"));
}
# Happens before we check if we have this in cache already.
#
# Typically you clean up the request here, removing cookies you don't need,
# rewriting the request, etc.
}
vim /etc/security/limits.conf 添加下列内容
varnish - nofile 140000
varnish - memlock 100000
开启varnish服务,查看80端口
systemctl start varnish
netstat -antlp
物理机设置解析并访问
vim /etc/hosts
172.25.14.1 server1 www.westos.org bbs.westos.org westos.org
2.负载均衡
vim /etc/varnish/default.vcl
systemctl reload varnish.service
物理机访问
3.nginx代理
server4安装nginx
[root@server4 ~]# lftp 172.25.14.250
lftp 172.25.14.250:~> cd pub/docs/lamp/
lftp 172.25.14.250:/pub/docs/lamp> get nginx-1.20.1.tar.gz
1061461 bytes transferred
lftp 172.25.14.250:/pub/docs/lamp> exit
[root@server4 ~]# ls
nginx-1.20.1.tar.gz
[root@server4 ~]# tar zxf nginx-1.20.1.tar.gz
[root@server4 ~]# cd nginx-1.20.1/
[root@server4 nginx-1.20.1]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
[root@server4 nginx-1.20.1]# yum install -y gcc
[root@server4 nginx-1.20.1]# yum install -y pcre-devel
[root@server4 nginx-1.20.1]# yum install -y openssl-devel
[root@server4 nginx-1.20.1]# ./configure --with-http_ssl_module
[root@server4 nginx-1.20.1]#make
[root@server4 nginx-1.20.1]#make install
[root@server4 ~]# cd /usr/local/nginx/sbin/
[root@server4 sbin]# ls
nginx
[root@server4 sbin]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/
[root@server4 ~]# cd /usr/local/nginx/
[root@server4 nginx]# ls
conf html logs sbin
[root@server4 nginx]# cd conf/
[root@server4 conf]# vim nginx.conf
[root@server4 conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@server4 ~]# nginx
server1修改配置文件
[root@server1 ~]# vim /etc/varnish/default.vcl
[root@server1 ~]# systemctl reload varnish.service
物理机进行访问
server2、server3查看apache日志
显示到是由server4所访问
为了显示出客户端真实ip
server4
vim /usr/local/nginx/conf/nginx.conf
http {
upstream westos {
#server 172.25.14.2:80; #注释掉
server 172.25.14.3:80;
}
nginx -s reload #重启
server3安装nginx
tar zxf nginx-1.20.1.tar.gz #解压
cd nginx-1.20.1/
yum install -y gcc prce-devel openssl-devel
./configure --with-http_realip_module --with-http_ssl_module
make
make install
systemctl stop httpd
ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/
nginx
vim /usr/local/nginx/conf/nginx.conf
nginx -s reload
物理机访问
server3查看nginx日志,显示出物理机真实ip172.25.14.250