負(fù)載均衡
所有工作均在root權(quán)限下執(zhí)行
1.準(zhǔn)備工作16.04
倆臺apache服務(wù)器,一臺lnmp(nginx)服務(wù)器
apache2:
apt-get install apache2
2.在apache服務(wù)器下放入html文檔
放置目錄:
/var/www/html
倆個服務(wù)器分別寫入index.html,內(nèi)容分別為:
<h1>this is server one</h1>
<h1>this is server two</h1>
3.在windows下檢測倆臺apache是否安裝成功!
打開對應(yīng)的ip地址,會輸出this isserver one 或者 this is server two
4.配置nginx
1.打開目錄/etc/nginx/
cd /etc/nginx/
2.備份nginx.conf
cp nginx.conf nginx.conf.back
3.打開nginx.conf并編輯
vim nginx.conf
加入倆臺apache的ip地址:
upstream site {
server 192.168.3.82:8040;
server 192.168.3.82:8041;
}
配置域名
server {
listen 8080;
server_name {nginx的域名};
}
配置目錄及加入那倆臺服務(wù)器
location / {
root html;//這個地方是文檔放置目錄,默認(rèn)打開的目錄
index index.html index.htm;
proxy_pass http://site;//site及代表上面的綁定的那倆個apache服務(wù)器
}
4.調(diào)試nginx檢測是否有錯
nginx -t //沒有錯誤則 第一行顯示ok 否則報出錯誤位置
5.配置完成,保存wq,重啟nginx
service nginx restart
6.配置windows端的域名
windows->system32->drives->etc->hosts
7.在瀏覽器輸入域名,沒刷新一次換一次服務(wù)器
會依次顯示:this isserver one
this is server two
ok,負(fù)載均衡搭建成功!
負(fù)載均衡實(shí)現(xiàn)動靜分離
1.準(zhǔn)備工作
倆臺存儲php,java,js等程序(動態(tài)服務(wù)器)
一臺存儲靜態(tài)文件{html、css、images}(靜態(tài)服務(wù)器)
2.為動態(tài)服務(wù)器安裝解析程序,本次僅展示安裝php,并在目錄下寫入php程序。
apt-get install php
apt-get install libapache2-mod-php7.0 將php與apache2整合
3.配置nginx
cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.back
打開nginx.conf
vi /etc/nginx/nginx.conf
4.開始配置
在http{}里面加入:
location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
proxy_pass http://192.168.x.x;//如果是nginx做靜態(tài)服務(wù)器的話 格式如下:root 文檔路勁
#expires定義用戶瀏覽器緩存的時間為3天,如果靜態(tài)頁面不常更新,可以設(shè)置更長,這樣可以節(jié)省帶寬和緩解服務(wù)器的壓力.
expires 3d;
}
5.保存,調(diào)試nginx,成功后重啟nginx
6.測試
瀏覽器里輸入nginx域名/test.html
會打開靜態(tài)服務(wù)器下的靜態(tài)文件test.html
瀏覽器里輸入nginx域名/test.php
會打開動態(tài)服務(wù)器下的動態(tài)文件test.php