概述
主 dd
从 dd1 dd2
#从dd1 dd2同配置
useradd -M -s /sbin/logoin nginx
./configure
-prefix=/usr/local/nginx
-user=nginx
-group=nginx
make&&make install
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
echo "<h1>SERVER1</h1>">/usr/local/nginx/html/index.html
#主dd
tar haproxy
make TARGET=linux26
make install
PREFIX 为指定的安装路径
TARGET则根据当前操作系统内核版本指定
- linux22 for Linux 2.2
- linux24 for Linux 2.4 and above (default)
- linux24e for Linux 2.4 with support for a working epoll (> 0.21)
- linux26 for Linux 2.6 and above
- linux2628 for Linux 2.6.28, 3.x, and above (enables splice and tproxy)
mkdir /etc/haproxy
groupadd haproxy
useradd -s /sbin/nologin -M -g haproxy haproxy
vi /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local2
#chroot /usr/local/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000 ###最大连接数,默认 4000
user haproxy
group haproxy
daemon ###创建 1 个进程进入 deamon 模式运行
defaults
mode http ###默认的模式 mode { tcp|http|health }
log global ###采用全局定义的日志
option dontlognull ###不记录健康检查的日志信息
option httpclose ###每次请求完毕后主动关闭 http 通道
option httplog ###日志类别 http 日志格式
option forwardfor ###后端服务器可以从 Http Header 中获得客户端 ip
option redispatch ###serverid 服务器挂掉后强制定向到其他健康服务器
timeout connect 10s #如果 backend 没有指定,默认为 10s
timeout client 10s ###客户端连接超时
timeout server 10s ###服务器连接超时
maxconn 60000 ###最大连接数
retries 3 ###3 次连接失败就认为服务不可用,也可以通过后面设置
listen stats
bind 0.0.0.0:8089 #监听端口
stats refresh 30s #统计页面自动刷新时间
stats uri /stats #统计页面 url
stats realm Haproxy Manager #统计页面密码框上提示文本
stats auth admin:admin #统计页面用户名和密码设置
stats admin if TRUE
stats hide-version #隐藏统计页面上 HAProxy 的版本信息
#WEB设置
listen webcluster
bind 0.0.0.0:80
mode http
option httpchk GET /index.html
log global
maxconn 3000
balance roundrobin
server web1 192.168.221.138:80 check inter 2000 fall 3
server web2 192.168.221.139:80 check inter 2000 fall 3
WEB查看:
http://192.168.221.136:8089/stats
cp examples/haproxy.init /etc/init.d/haproxy
chmod 755 /etc/init.d/haproxy
chkconfig --add haproxy
ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
service haproxy start
netstat -tlnp | grep haproxy
错误一:第 26 行:[: =: 期待一元表达式,[]里的变量用加上引号
vi /etc/rc.d/init.d/haproxy
改为[ "${NETWORKING}" = "no" ] && exit 0 注意间隔
错误二:bind 80端口会和本地apache80冲突,apache改了端口一定要重启apache
改apache端口
错误三:cannot bind socket
echo "net.ipv4.ip_nonlocal_bind = 1" >>/etc/sysctl.conf
错误四:/etc/init.d/haproxy: line 98: /usr/sbin/haproxy: No such file or directory
vi /etc/rc.d/init.d/haproxy
BIN=/usr/sbin/$BASENAME ---> BIN=/usr/local/sbin/$BASENAME
配置启动haproxy
vi /etc/init.d/haproxy
#!/bin/sh
#
# haproxy
#
# chkconfig: - 85 15
# description: HAProxy is a free, very fast and reliable solution
# offering high availability, load balancing, and
# proxying for TCP and HTTP-based applications
# processname: haproxy
# config: /etc/haproxy/haproxy.cfg
# pidfile: /var/run/haproxy.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
exec="/usr/local/sbin/haproxy"
prog=$(basename $exec)
[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog
cfgfile=/etc/haproxy/haproxy.cfg
pidfile=/var/run/haproxy.pid
lockfile=/var/lock/subsys/haproxy
check() {
$exec -c -V -f $cfgfile $OPTIONS
}
start() {
$exec -c -q -f $cfgfile $OPTIONS
if [ $? -ne 0 ]; then
echo "Errors in configuration file, check with $prog check."
return 1
fi
echo -n $"Starting $prog: "
# start it up here, usually something like "daemon $exec"
daemon $exec -D -f $cfgfile -p $pidfile $OPTIONS
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
# stop it here, often "killproc $prog"
killproc $prog
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
$exec -c -q -f $cfgfile $OPTIONS
if [ $? -ne 0 ]; then
echo "Errors in configuration file, check with $prog check."
return 1
fi
stop
start
}
reload() {
$exec -c -q -f $cfgfile $OPTIONS
if [ $? -ne 0 ]; then
echo "Errors in configuration file, check with $prog check."
return 1
fi
echo -n $"Reloading $prog: "
$exec -D -f $cfgfile -p $pidfile $OPTIONS -sf $(cat $pidfile)
retval=$?
echo
return $retval
}
force_reload() {
restart
}
fdr_status() {
status $prog
}
case "$1" in
start|stop|restart|reload)
$1
;;
force-reload)
force_reload
;;
check)
check
;;
status)
fdr_status
;;
condrestart|try-restart)
[ ! -f $lockfile ] || restart
;;
*)
echo $"Usage: $0 {start|stop|status|restart|try-restart|reload|force-reload}"
exit 2
esac
chmod 777 /etc/init.d/haproxy
service haproxy start
转载于:https://www.cnblogs.com/feizhuanye/p/10395047.html
最后
以上就是淡然羽毛为你收集整理的Haproxy+Nginx的全部内容,希望文章能够帮你解决Haproxy+Nginx所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复