b.对后端服务器的健康检查,只支持通过端口来检测,不支持通过url来检测。不支持Session的直接保持,但能通过ip_hash来解决。
1.nginx的安装:
a.tar -zxf nginx-1.10.1.tar.gz
b.cd nginx-1.10.1/
c.cd src/ ##进入源码包
d.cd core/ (nginx.h)
e.cd ..
f.cd auto/
g.cd cc (gcc)
h.cd ..
i../configure --help
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module ##源码安装第一步,生成rpm包
yum install pcre-devel openssl-devel zlib-devel -y
重新执行第一步,直到没有报错
make ##源码安装第二步(编译)
make install ##源码安装第三步(下载)
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
cd /usr/local/sbin
./nginx ##直接开启nginx
which nginx ##查看nginx安装在哪
2.要改变一个应用程序中的参数值,先要确保内核是否支持,再确保系统是否支持,最后才能确保修改的应用程序的参数值成功生效.
a.查看内核参数的命令:sysctl -a (/etc/sysctl)
b.修改系统中的值:/etc/security/limits.conf
c.应用:/usr/local/nginx/conf/nginx.conf
3.nginx的应用:
a.自建虚拟目录:
location / {
root html;
index index.html index.htm;
}
location /status {
stub_status on;
access_log off;
allow 172.25.31.250;
deny all;
}
测试:172.25.31.2:/status
b.虚拟主机的应用:
server {
listen 80;
server_name www.wzw.com;
location /{
root /www1;
index index.html;
}
}
server {
listen 8080;
server_name bbs.wzw.com;
location /{
root /www2;
index index.html;
}
}
nginx -s reload ##改好配置文件后记的重新加载nginx服务
测试:curl www.wzw.com 若curl www.wzw.com down掉以后,客户端会出现报错所以可以应用它的负载均衡来解决。
注意:在测试主机上要做好解析,并在nginx服务器上建立好发布目录和发布文件
c.负载均衡的应用:
http {
upstream westos{
server 172.25.31.3:80;
server 172.25.31.4:8080;
} ##定义了apache服务的负载均衡
include mime.types;
default_type application/octet-stream;
server {
listen 80;
server_name www.wzw.com;
location /{
# root /www1; ##从本机上读取数据
# index index.html;
proxy_pass http://westos; ##从后台服务器上去取数据
}
}
server {
listen 8080;
server_name bbs.wzw.com;
location /{
# root /www2;
# index index.html;
proxy_pass http://westos;
}
}
}
4.nginx的高可用:
a.在两台nginx服务器上安装代理: yum install ricci -y
passwd ricci
/etc/init.d/ricci start
chkconfig ricci on
b.在管理端服务器上安装管理服务: yum install luci -y
/etc/init.d/luci start
注意:在两台nginx服务器上都要做节点的解析,关掉iptables
三.haproxy
3.HAProxy跟LVS类似,本身就只是一款负载均衡软件;单纯从效率上来讲HAProxy会比Nginx有更出色的负载均衡速度,在并发处理上也是优于Nginx的
1.haproxy的安装:
yum install rpm-build -y
rpmbuild -ta haproxy-1.6.11.tar.gz
vim /etc/security/limits.conf
sysctl -a | grep file
cd /root/rpmbuild/
cd RPMS/ ##RPMS中是存放编译后的
cd x86_64/
rpm -ivh haproxy-1.6.11-1.x86_64.rpm
cd /etc/haproxy/
/etc/init.d/haproxy start
vim haproxy.cfg
552 netstat -antlp
553 vim /etc/rsyslog.conf
554 /etc/init.d/rsyslog restart
555 tail -f /var/log/haproxy.log
556 vim /etc/rsyslog.conf
557 /etc/init.d/rsyslog restart
558 tail -f /var/log/haproxy.log
2.配置文件的修改
vim /etc/haproxy
global
maxconn 65535
stats socket /var/run/haproxy.stat mode 600 level admin
log 127.0.0.1 local0
uid 188
gid 188
chroot /var/empty
daemon
defaults
mode http
log global
option httplog
option dontlognull
monitor-uri /monitoruri
maxconn 8000
timeout client 30s
retries 2
option redispatch
timeout connect 5s
timeout server 5s
stats uri /admin/stats
stats auth wzw:westos
# The public 'www' address in the DMZ
frontend public
bind 172.25.31.2:80 name clear
#bind 192.168.1.10:443 ssl crt /etc/haproxy/haproxy.pem
# acl badlist src 172.25.31.250
# http-request deny if badlist
# errorloc 403 http://172.25.31.2:8080
# redirect location http://172.25.31.2:8080
acl read methon GET
acl read methon HEAD
acl write methon PUT
acl write methon POST
#use_backend static if { hdr_beg(host) -i img }
#use_backend static if { path_beg /img /css }
use_backend dynamic if { path_end .php }
use_backend static if write
default_backend static
# The static backend backend for 'Host: img', /img and /css.
backend static
# balance roundrobin
balance source
server web1 172.25.31.3:80 check inter 2000
server web2 172.25.31.4:80 check inter 1000
backend dynamic
balance roundrobin
server web1 172.25.31.3:80 check inter 1000
注意:haproxy安装完后,没有haproxy.cfg文件,可以参考/root/rpmbuild/DUILD/ haproxy-1.6.11/doc/configuration.txt文件来配置。