2019-05-9筆記

Day49

課堂筆記

2019年5月9日

編譯安裝負(fù)載均衡


集群開(kāi)機(jī)順序:

1、從后往前開(kāi)。

編譯安裝nginx負(fù)載均衡

下載:

mkdir -p /server/tools

cd /server/tools

wget http://nginx.org/download/nginx-1.16.0.tar.gz


#安裝依賴(lài)。

yum install pcre pcre-devel -y

yum install openssl openssl-devel -y ?#https加密用他。


#編譯安裝步驟

tar xf nginx-1.16.0.tar.gz

cd nginx-1.16.0/

useradd -u 1111 -s /sbin/nologin nginx -M

id nginx

./configure ?--user=nginx --group=nginx --prefix=/application/nginx-1.16.0/ --with-http_stub_status_module ?--with-http_ssl_module --with-pcre

make

make install

ln -s /application/nginx-1.16.0/ /application/nginx

/application/nginx/sbin/nginx

netstat -lntup|grep nginx

curl 127.0.0.1



負(fù)載均衡模板配置:

upstream backend {

????server 10.0.0.7:80 ?weight=1;

????server 10.0.0.8:80 ?weight=1;

}


server {

????listen ??????80;

????server_name ?blog.etiantian.org;

????location / {

????????proxy_pass http://backend;

????}

}

upstream模塊 負(fù)載均衡池。

backend負(fù)載均衡池名稱(chēng)


[root@lb01 conf]# egrep -v "^$|#" nginx.conf.default >nginx.conf



正式配置:

[root@lb01 conf]# cat nginx.conf

worker_processes ?1;

events {

????worker_connections ?1024;

}

http {

????include ??????mime.types;

????default_type ?application/octet-stream;

????sendfile ???????on;

????keepalive_timeout ?65;

????upstream backend {

????????server 10.0.0.7:80 ?weight=3;

????????server 10.0.0.8:80 ?weight=1;

????}


????server {

????????listen ??????80;

????????server_name ?www.etiantian.org;

????????location / {

????????????proxy_pass http://backend;

????????????proxy_set_header Host ?$host;

????????}

???}

}

別忘了,提前配置web服務(wù)器的www虛擬主機(jī)

WEB01:

[root@web01 /etc/nginx/conf.d]# cat www.conf

server {

????????listen ??????80;

????????server_name ?www.etiantian.org;

????????location / {

????root ??/usr/share/nginx/html/www;

????????????index ?index.html;

????????}

????}


[root@web01 /etc/nginx/conf.d]# cat /usr/share/nginx/html/www/index.html

www7



WEB02:

[root@web02 /application/nginx/html/www]# cd /application/nginx/conf/extra/

[root@web02 /application/nginx/conf/extra]# cat 01_www.conf

????server {

????????listen ??????80;

????????server_name ?etiantian.org;

rewrite ^/(.*) http://www.etiantian.org/$1 permanent;

????}

????server {

????????listen ??????80;

????????server_name ?www.etiantian.org;

????????location / {

????????????root ??html/www;

????????????index ?index.html index.htm;

????????}


autoindex on;

access_log ?logs/access_www.log ?main;

????}

[root@web02 /application/nginx/conf/extra]# cat ../../html/www/index.html

www8


增加blog負(fù)載均衡


[root@lb01 conf]# cat nginx.conf

worker_processes ?1;

events {

????worker_connections ?1024;

}

http {

????include ??????mime.types;

????default_type ?application/octet-stream;

????sendfile ???????on;

????keepalive_timeout ?65;

????upstream backend {

????????server 10.0.0.7:80 ?weight=3;

????????server 10.0.0.8:80 ?weight=2;

????}


????server {

????????listen ??????80;

????????server_name ?www.etiantian.org;

????????location / {

????????????proxy_pass http://backend;

????????????proxy_set_header Host ?$host;

????????}

???}


?server {

????????listen ??????80;

????????server_name ?blog.etiantian.org;

????????location / {

????????????proxy_pass http://backend;

????????????proxy_set_header Host ?$host;

????????}

???}

}



默認(rèn)情況瀏覽器請(qǐng)求負(fù)載均衡器,會(huì)攜帶host字段,但是Nginx代理向后請(qǐng)求節(jié)點(diǎn)

默認(rèn)在請(qǐng)求頭里不帶host字段。

配置Nginx代理向后請(qǐng)求節(jié)點(diǎn)默認(rèn)在請(qǐng)求頭里帶host字段配置參數(shù):

proxy_set_header Host ?$host;




proxy_set_header X-Forwarded-For $remote_addr;

#<==這是反向代理時(shí),節(jié)點(diǎn)服務(wù)器獲取用戶(hù)真實(shí)IP的必要功能配置。

在反向代理請(qǐng)求后端節(jié)點(diǎn)服務(wù)器的請(qǐng)求頭中增加獲取的客戶(hù)端IP的字段信息,然后節(jié)點(diǎn)后端可以通過(guò)程序或者相關(guān)的配置接收X-Forwarded-For傳過(guò)來(lái)的用戶(hù)真實(shí)IP的信息。




負(fù)載均衡模板配置:

upstream backend {

????server 10.0.0.7:80 ?weight=1;

????server 10.0.0.8:80 ?weight=1;

}



upstream blog_server_pool {

server ??10.0.10.6; #<==這一行標(biāo)簽和下一行是等價(jià)的。

???server ??10.0.10.6:80 weight=1 max_fails=1 fail_timeout=10s;

nginx upstream監(jiān)測(cè)節(jié)點(diǎn)狀態(tài),把不好的踢出去。好的時(shí)候加進(jìn)來(lái)。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 事業(yè)是個(gè)好東西, 但在寒冷的夜晚, 你不能把它裹在身上。
    bibobibo100閱讀 523評(píng)論 0 0
  • 五分鐘,十分鐘,一廂又一廂,我不停地在酒店大堂的電梯前焦躁地踱步,集合時(shí)間就要到了。早老公一步擠上電梯的我,原以為...
    慢慢行欣賞啊閱讀 413評(píng)論 0 2

友情鏈接更多精彩內(nèi)容