近期外包接單,做了一個(gè)簡單的系統(tǒng),本以為如此簡單一個(gè)Tomcat就足以滿足祭玉,結(jié)果客戶要求需要兩臺(tái)服務(wù)器負(fù)載均衡,之前負(fù)載均衡都是由專門的人負(fù)責(zé)春畔,第一次自己實(shí)現(xiàn)Nginx脱货,走了不少彎路岛都。
由于項(xiàng)目組有附件上傳的功能,沒有專門的文件服務(wù)器振峻,將附件都存放在了ROOT底下臼疫,負(fù)載均衡后發(fā)現(xiàn)經(jīng)常找不到圖片,明顯是因?yàn)閮膳_(tái)服務(wù)器資源不同步的問題造成的扣孟,于是開始研究服務(wù)器目錄同步烫堤,使用Rsync+Sersync實(shí)現(xiàn)目錄同步,具體步驟:
1:制定兩臺(tái)服務(wù)器中的一臺(tái)為主服務(wù)器(安裝軟件rsync+sersync凤价,但是不需要啟動(dòng)rsync)鸽斟,另一臺(tái)為從服務(wù)器(安裝軟件rsync,需要啟動(dòng)rsync)
2:從服務(wù)器安裝完rsync需要對/etc/rsyncd.conf 進(jìn)行配置:
uid = root ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?#擁有目錄權(quán)限用戶
gid = root ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?#擁有目錄權(quán)限的組
use chroot = no ? ? ? ? ? ? ? ? ? ? ? ? #內(nèi)網(wǎng)使用可以不用配置
max connections = 200 ? ? ? ? ? ? ?#最大連接數(shù) ?
timeout = 300 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #超時(shí)時(shí)間
pid file = /var/run/rsyncd.pid ? ? ? ? ? ? ?#啟動(dòng)進(jìn)程寫入此PID文件
lock file = /var/run/rsyncd.lock ? ? ? ? ? ?#lock文件來配合最大連接數(shù)參數(shù)
log file = /var/log/rsyncd.log ? ? ? ? ? ? ? #日志文件
ignore errors = yes ? ? ? ? ? ? ? ? ? ? ? ? #忽略I/O錯(cuò)誤
read only = false ? ? ? ? ? ? ? ? ? ? ? ? ? #允許讀寫 ? ? ?
list = false ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?#不列出列表
hosts allow = 192.168.1.0/24 ? ? ? ? ? ? ? ?#允許網(wǎng)段
hosts deny = * ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?#拒絕其他網(wǎng)段
auth users = users ? ? ? ? ? ? ? ? ? ? ? ? ?#認(rèn)證用戶
secrets file = /opt/app/rsyncd/auth.pass ? ?#密碼文件
[web] ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #同步目錄
path = /backup/web
設(shè)置?/opt/app/rsyncd/auth.pass文件格式為user:password
給?/opt/app/rsyncd/auth.pass添加600權(quán)限 chmod 600??/opt/app/rsyncd/auth.pass
啟動(dòng)rsync? rsnyc --daemon
3:主服務(wù)器部署
主服務(wù)器安裝完sersync后需要配置confxml.xml利诺,在sersync的安裝目錄下
<sersync>
? ? ? ? <localpath watch="/backup/web"> <!--本機(jī)同步目錄-->
? ? <remote ip="192.168.1.201" name="web"/>? ? <!--從服務(wù)器IP及模塊名稱富蓄,需要與從服務(wù)器/etc/rsyncd.conf配置的模塊名完全一致-->
? ? <!--<remote ip="192.168.8.39" name="tongbu"/>-->
? ? <!--<remote ip="192.168.8.40" name="tongbu"/>-->
? ? </localpath>
? ? ?<rsync>
? ? ?<commonParams params="-artuz"/>
????<auth start="true" users="users" passwordfile="/opt/app/sersyncd/auth.pass"/>
? ? ? ? ? ? <!--認(rèn)證用戶及認(rèn)證從服務(wù)器密碼文件目錄,/opt/app/sersyncd/auth.pass(600權(quán)限) 里面只需要設(shè)定密碼慢逾,users 要與從服務(wù)器設(shè)定的用戶一致-->
? ? ? ?<userDefinedPort start="false" port="874"/><!-- port=874 端口需要與從服務(wù)器rsync設(shè)定的一致立倍,如從服務(wù)器設(shè)定端口8080此處的端口也需要修改成8080? start="true" -->
? ? ? ?<timeout start="false" time="100"/><!-- timeout=100 -->
? ? ? ? <ssh start="false"/>
? ? ? ? </rsync>
<failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/>
<!--default every 60mins execute once-->
<crontab start="false" schedule="600"><!--600mins-->
?<crontabfilter start="false">
? ?<exclude expression="*.php"></exclude>
? <exclude expression="info/*"></exclude>
? ? ? ? ? ? </crontabfilter>
? ? ? ? </crontab>
? ? ? ? <plugin start="false" name="command"/>
? ? </sersync>
啟動(dòng)sersync :/opt/soft/sersync/sersync/sersync2 -d -r -o /opt/soft/sersync/sersync/confxml.xml
由于只能由主服務(wù)器同步到從服務(wù)器,因此當(dāng)負(fù)載均衡到從服務(wù)器時(shí)氛改,附件無法被同步到主服務(wù)器帐萎,訪問圖片依然存在404錯(cuò)誤比伏,因此需要對Nginx進(jìn)行配置胜卤,通過動(dòng)靜分離的方式,將所有靜態(tài)圖片訪問的請求都指向從服務(wù)器赁项,因此解決上傳圖片找達(dá)不到問題葛躏。
location ~ .*\.(gif|jpg|jpeg|png|flv|mp3)$ { #靜態(tài)
? ? ? ? ? proxy_pass http://192.168.1.12:80/;
? ? ? ? }