1.負(fù)載均衡
當(dāng)超大流量請(qǐng)求時(shí)剥槐,就可能導(dǎo)致請(qǐng)求等待或者服務(wù)器死機(jī)的情況,為了解決大流量訪問的問題,可以搭建分布式,將請(qǐng)求分發(fā)到不同計(jì)算機(jī)可解決大流量問題
長見的負(fù)載均衡方案有如下幾種:
1萍嬉、http重定向
2、反向代理負(fù)載均衡
3、 IP負(fù)載均衡
4、DNS負(fù)載均衡
5、DNS/GSLB負(fù)載均衡
2.準(zhǔn)備三臺(tái)服務(wù)器(本人是兩臺(tái)ubuntu,和本地W)
- 192.168.1.150 本地,主
36.110.39.222:8081
36.110.39.222:8082
2.由于沒有域名,所以直接用hosts指定域名(本人本地環(huán)境用的phpstudy)
Win+r 輸入 drivers 復(fù)制編輯hosts
##添加
127.0.0.1 a.com
3.主服務(wù)器添加配置
主服務(wù)器http節(jié)點(diǎn)下添加(8080是我本地測試laravel框架的端口)
server {
listen 8080;
server_name a.com;
root "D:/workspace/lartest/public";
index index.php;
location / {
if (!-e $request_filename){
rewrite (.*) /index.php;
}
}
#rewrite ^/article\.html?arcid=([0-9]+)$ /index.php?c=page&m=article&arcid=$1 last;
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
location ~ /\. {
deny all;
}
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
}
#負(fù)載均衡模塊子房,分發(fā)的服務(wù)器
upstream a.com{
#ip_hash;#每個(gè)請(qǐng)求按訪問ip的hash結(jié)果分配,這樣每個(gè)訪客固定訪問一個(gè)后端服務(wù)器,可以解決session的問題
server 36.110.39.222:8081 weight=1;
server 36.110.39.222:8082 weight=1;
server 127.0.0.1:8080 weight=1; #可以使用本機(jī)提供服務(wù),如果不加本行的話,主服務(wù)器只起到轉(zhuǎn)發(fā)的作用,有些浪費(fèi)
#weight #權(quán)重值大的被訪問的概率就高
#backup; #其它所有的非backup機(jī)器down或者忙的時(shí)候证杭,請(qǐng)求backup機(jī)器
#down; #down 表示單前的server暫時(shí)不參與負(fù)載
#fair; #按后端服務(wù)器的響應(yīng)時(shí)間來分配請(qǐng)求田度,響應(yīng)時(shí)間短的優(yōu)先分配
}
#監(jiān)聽80端口,并分發(fā)請(qǐng)求到其他服務(wù)器
server {
listen 80;
server_name a.com;
location / {
proxy_pass http://a.com;
}
}
因?yàn)?0端口用于nginx監(jiān)聽用戶請(qǐng)求解愤,所以需要用8080端口接受nginx轉(zhuǎn)發(fā)過來的請(qǐng)求镇饺。
4.其他兩臺(tái)配置(ubuntu nginx默認(rèn)配置文件/etc/nginx/sites-enabled/default)
server {
listen 80;
server_name a.com;
root /var/www/html;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
}
兩臺(tái)機(jī)器分別在在/var/www/html 目錄下修改靜態(tài)文件
Welcome to nginx!我是8081
Welcome to nginx!我是8082
最后重啟三臺(tái)服務(wù)器,本地瀏覽器輸入 a.com
連續(xù)點(diǎn)擊刷新后本人頁面輸出:
第一次輸出:laravel歡迎頁
第二次輸出:Welcome to nginx!我是8081
第三次輸出:Welcome to nginx!我是8082
第四次輸出:laravel歡迎頁
.
.
.