
先看圖:
一、準(zhǔn)備環(huán)境工作
1.配置nginx安裝源然后安裝
[oot@lb01 ~]# vim /etc/yum.repos.d/nginx.repo
▽
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
2.修改web01 web02配置文件
記得提前備份
[root@web01 /etc/nginx/conf.d]# cat 01-www.conf
server {
listen 80;
server_name www.oldboy.com;
access_log /var/log/nginx/access_www.log main ;
root /app/www;
location / {
index index.html index.htm;
}
}
[root@web01 /etc/nginx/conf.d]# cat 02-blog.conf
server {
listen 80;
server_name blog.oldboy.com;
access_log /var/log/nginx/access_blog.log main;
root /app/blog;
location / {
index index.php index.html index.htm;
}
location ~* \.(php|php5)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_buffers 16 16k;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
重啟nginx檢查語法
3.web01 web02 創(chuàng)建站點(diǎn)目錄與首頁文件
倆邊都相同
[root@web01 /etc/nginx/conf.d]# mkdir -p /app/{www,blog}
[root@web01 /etc/nginx/conf.d]# for n in www blog ; do echo $n.oldboy.com >/app/$n/index.html ;done
[root@web01 /etc/nginx/conf.d]# tree /app/
/app/
├── blog
│ └── index.html
└── www
└── index.html
2 directories, 2 files
[root@web02 conf.d]# tree /app
/app
├── blog
│ └── index.html
└── www
└── index.html
2 directories, 2 files
4.去db01上curl一下
curl -H Host:www.oldboy.com 10.0.0.[7-8]
二、編寫nginx反向代理服務(wù)配置文件(lb01)
ngx_http_upstream_module 負(fù)載均衡
ngx_http_proxy_module 反向代理
[root@lb01 ~]# vim /etc/nginx/nginx.conf
...
upstream web_pools {
server 10.0.0.7:80;
server 10.0.0.8:80;
}
# include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name www.oldboy.com;
location / {
proxy_pass http://web_pools;
}
}
}
[root@lb01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb01 ~]# systemctl restart nginx
2.為web01 web02首頁文件追加內(nèi)容讓容易區(qū)分
for n in www blog ; do echo `hostname` $n.oldboy.com >/app/$n/index.html ;done
[root@web01 conf.d]# cat /app/www/index.html
web01 www.oldboy.com
[root@web02 conf.d]# cat /app/blog/index.html
web02 blog.oldboy.com
3.在lb01上curl一下
[root@lb01 ~]# curl 10.0.0.7
web01 www.oldboy.com
[root@lb01 ~]# curl 10.0.0.8
web02 www.oldboy.com
[root@lb01 ~]#
[root@lb01 ~]#
[root@lb01 ~]# curl 10.0.0.5
web02 www.oldboy.com
[root@lb01 ~]# curl 10.0.0.5
web01 www.oldboy.com
[root@lb01 ~]# curl 10.0.0.5
web02 www.oldboy.com
[root@lb01 ~]# curl 10.0.0.5
web01 www.oldboy.com
三、抓包

四、upstream模塊參數(shù):
server —— RS配置,可以是ip或域名
weight ——權(quán)重
max_fails ——失敗次數(shù)
fail_timeout =10s ——多久后在檢查一遍
backup ——如果加上backup 會(huì)在池塘中其他機(jī)器都掛掉 才會(huì)啟動(dòng)
down 讓服務(wù)器不可用
五、配置權(quán)重
weight=1;
upstream web_pools {
server 10.0.0.7:80 weight=2;
server 10.0.0.8:80 weight=1;
}
[root@lb01 ~]# curl 10.0.0.5
web01 www.oldboy.com
[root@lb01 ~]# curl 10.0.0.5
web01 www.oldboy.com
[root@lb01 ~]# curl 10.0.0.5
web02 www.oldboy.com
[root@lb01 ~]# curl 10.0.0.5
web01 www.oldboy.com
[root@lb01 ~]# curl 10.0.0.5
web01 www.oldboy.com
[root@lb01 ~]# curl 10.0.0.5
web02 www.oldboy.com
六、CND加速緩存
網(wǎng)站加速 緩存網(wǎng)站靜態(tài)頁面 視頻(切片)
用戶先訪問cdn
cdn緩存沒有 就轉(zhuǎn)到源站
cdn公司介紹:藍(lán)汛 網(wǎng)宿 阿里云
七、配置文件中添加server模塊的參數(shù)(lb01)
weight 權(quán)重;
max_fails 健康檢查,失敗次數(shù);
fail_timeout 多久后在檢查一遍
修改配置模塊參數(shù)
upstream web_pools {
server 10.0.0.7:80 weight=2 max_fails=3 fail_timeout=10s;
server 10.0.0.8:80 weight=1 max_fails=3 fail_timeout=10s;
}
測(cè)試關(guān)閉一臺(tái)后,是否還能訪問:
for n in {1..1000};do curl 10.0.0.5/index.html ;sleep 1;done
八、請(qǐng)求訪問第二個(gè)站點(diǎn)blog.oldboy.com
1.抓包看一下情況:
2.修改 請(qǐng)求頭
proxy_set_header Host $host;
server {
listen 80;
server_name www.oldboy.com;
location / {
proxy_pass http://web_pools;
}
}
server {
listen 80;
server_name blog.oldboy.com;
location / {
proxy_pass http://web_pools;
proxy_set_header Host $host;
}
}
[root@lb01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb01 ~]# systemctl restart nginx
3.再訪問就成功了
九、顯示客戶端的地址,并記錄到日志中
proxy_set_header X-Forwarded-For $remote_addr;
server {
listen 80;
server_name www.oldboy.com;
location / {
proxy_pass http://web_pools;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
server {
listen 80;
server_name blog.oldboy.com;
location / {
proxy_pass http://web_pools;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
這里測(cè)試的是在lb01上curl的web01
十、添加訪問控制
如果某些網(wǎng)段訪問量成千上萬,特別高的話,可能是被入侵了
需要給這個(gè)網(wǎng)址做限制訪問
server {
listen 80;
server_name www.oldboy.com;
location / {
if ($remote_addr ~ "^192.168.22.") { \\指定禁止訪問的網(wǎng)段
return 403 "別搗亂"; \\定義的是指定網(wǎng)段中,客戶訪問后返回的內(nèi)容
}
proxy_pass http://web_pools;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
十一、防火墻規(guī)則—iptables
iptables詳細(xì)用法http://man.linuxde.net/iptables
--dport 指定端口號(hào)
iptables -A INPUT -p tcp -s 192.168.22.0/24 -j DROP
-A:向規(guī)則鏈中添加條目;
-P:定義規(guī)則鏈中的默認(rèn)目標(biāo);
-s:指定要匹配的數(shù)據(jù)包源ip地址;
-j<目標(biāo)>:指定要跳轉(zhuǎn)的目標(biāo);
指定網(wǎng)段,配置時(shí)不要把自己擋外面,這就要跑機(jī)房了~
-F:清楚規(guī)則鏈中已有的條目;
-Z:清空規(guī)則鏈中的數(shù)據(jù)包計(jì)算器和字節(jié)計(jì)數(shù)器;
-X:刪除用戶自定義的鏈
未完待續(xù)...























