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)。