理解舉例html
listen配置項目一-般爲配置應用模塊參數前端
Haporxy服務器IP地址:192.168.144.175 web1服務器IP地址:192.168.144.151 web2服務器IP地址:192.168.144.176 client測試機
[root@web1 ~]# yum install -y \ //安裝環境須要組件包 > pcre-devel \ //開發包 > zlib-devel \ //壓縮包 > gcc \ > gcc-c++ \ > make [root@web1 ~]# useradd -M -s /sbin/nologin nginx //建立系統用戶 [root@web1 ~]# mkdir /abc ##建立掛載點 [root@web1 ~]# mount.cifs //192.168.100.8/LNMP-C7 /abc/ //掛載 Password for root@//192.168.100.3/LNMP-C7: [root@web1 ~]# cd /abc/ [root@web1 abc]# tar zxvf nginx-1.12.2.tar.gz -C /opt //解壓 [root@web1 abc]# cd /opt/nginx-1.12.2/ [root@web1 nginx-1.12.2]# ./configure \ //進行配置 > --prefix=/usr/local/nginx \ > --user=nginx \ > --group=nginx [root@web1 nginx-1.12.2]# make && make install [root@web1 nginx-1.12.2]# echo "this is kgv web" > /usr/local/nginx/html/test.html //建立站點網頁內容,web2上爲this is accp web [root@web1 nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ //便於系統識別 [root@web1 nginx-1.12.2]# nginx -t //檢查語法 [root@web1 nginx-1.12.2]# nginx //開啓服務 [root@web1 nginx-1.12.2]# systemctl stop firewalld.service //關閉防火牆 [root@web1 nginx-1.12.2]# setenforce 0
[root@haproxy ~]# yum install -y \ //安裝環境組件工具 > pcre-devel \ > bzip2-devel \ > gcc \ > gcc-c++ \ > make [root@haproxy ~]# systemctl stop firewalld.service //關閉防火牆 [root@haproxy ~]# setenforce 0 [root@haproxy ~]# mkdir /abc [root@haproxy ~]# mount.cifs //192.168.100.8/LNMP-C7 /abc/ //掛載 [root@haproxy ~]# cd /abc/ [root@haproxy abc]# tar zxvf haproxy-1.5.19.tar.gz -C /opt/ //解壓 [root@haproxy abc]# cd /opt/haproxy-1.5.19/ [root@haproxy haproxy-1.5.19]# make TARGET=linux26 //編譯 [root@haproxy haproxy-1.5.19]# make install //安裝 [root@haproxy haproxy-1.5.19]# mkdir /etc/haproxy //建立配置文件目錄 [root@haproxy haproxy-1.5.19]# cp examples/haproxy.cfg /etc/haproxy/ //模板複製到配置目錄下 [root@haproxy haproxy-1.5.19]# cd /etc/haproxy/ [root@haproxy haproxy]# vim haproxy.cfg //編輯配置文件 #chroot /usr/share/haproxy //註釋語句 #redispatch //註釋語句 //刪除全部listen項目,並添加 listen webcluster 0.0.0.0:80 option httpchk GET /test.html //web網頁 balance roundrobin //開啓輪詢模式 server inst1 192.168.144.151:80 check inter 2000 fall 3 //健康檢查請求三次 server inst2 192.168.144.176:80 check inter 2000 fall 3 [root@haproxy haproxy]# cp /opt/haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxy //啓動文件 [root@haproxy haproxy]# chmod +x /etc/init.d/haproxy //執行權限 [root@haproxy haproxy]# chkconfig --add /etc/init.d/haproxy //添加到service [root@haproxy haproxy]# ln -s /usr/local/sbin/haproxy /usr/sbin/ //便於系統識別 [root@haproxy haproxy]# service haproxy start //開啓服務 Starting haproxy (via systemctl): [ 肯定 ] [root@haproxy haproxy]# netstat -ntap | grep haproxy //查看端口 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 39884/haproxy
[root@haproxy haproxy]# vim /etc/haproxy/haproxy.cfg ##修改配置文件 global log /dev/log local0 info ##添加兩個級別的日誌文件 log /dev/log local0 notice #log loghost local0 info [root@haproxy haproxy]# service haproxy restart ##重啓服務 [root@haproxy haproxy]# touch /etc/rsyslog.d/haproxy.conf ##建立系統日誌haproxy配置文件 [root@haproxy haproxy]# vim /etc/rsyslog.d/haproxy.conf if ($programname == 'haproxy' and $syslogseverity-text == 'info') ##根據級別建立不一樣的日誌文件 then -/var/log/haproxy/haproxy-info.log &~ if ($programname == 'haproxy' and $syslogseverity-text == 'notice') then -/var/log/haproxy/haproxy-notice.log &~ [root@haproxy haproxy]# systemctl restart rsyslog.service ##重啓系統日誌服務 [root@haproxy haproxy]# cd /var/log/ ##此時是沒有haproxy日誌 ##從新訪問網頁 [root@haproxy haproxy]# cd /var/log/haproxy/ [root@haproxy haproxy]# ls ##此時就生成了info級別的日誌文件 haproxy-info.log