nginx负载均衡

image

一、测试环境

1.1 环境准备

lb02服务器(内网:10.0.0.6,外网:172.16.1.6)
web01服务器(内网:10.0.0.7,外网:172.16.1.7)
wen02服务器(内网:10.0.0.8,外网:172.16.1.8)

1.2 配置环境

1>每台服务器配置nginx的yum源

[root@web ~]# vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0      \\检查模块
enabled=1      \\开启模块

2>每台服务器安装nginx

yum install -y nginx

3>启动nginx服务,并设置开机自启动

启动nginx服务:systemctl start nginx
设置开机自启动:systemctl enable nginx

二、nginx负载均衡的配置

2.1 在web01和web02都配置 www.oldboy.comblog.oldboy.com域名

[root@web01 /etc/nginx/conf.d]# vim 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]# vim 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;
    }
}

检查语法,并都平滑重启服务

[root@web01 /etc/nginx/conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 /etc/nginx/conf.d]# systemctl reload nginx 

2.2 在web01和web02创建新的站点目录和index.html文件

[root@web01 ~]# mkdir -p /app/{www,blog}
[root@web01 ~]# for n  in  www blog  ; do echo  `hostname` $n.oldboy.com >/app/$n/index.html ;done
[root@web01 ~]# tree /app/
/app/
├── blog
│   └── index.html
└── www
    └── index.html

2 directories, 2 files
[root@web01 ~]# 

2.3 检查web01和web02的配置是否正常

[root@web01 ~]# curl -H Host:www.oldboy.com 10.0.0.[7-8]

[1/2]: 10.0.0.7 --> <stdout>
--_curl_--10.0.0.7
web01 www.oldboy.com

[2/2]: 10.0.0.8 --> <stdout>
--_curl_--10.0.0.8
web02 www.oldboy.com
[root@web01 ~]# curl -H Host:blog.oldboy.com 10.0.0.[7-8]

[1/2]: 10.0.0.7 --> <stdout>
--_curl_--10.0.0.7
web01 blog.oldboy.com

[2/2]: 10.0.0.8 --> <stdout>
--_curl_--10.0.0.8
web02 blog.oldboy.com

2.4 在lb02进行反向代理配置

ngx_http_upstream_module----负载均衡
ngx_http_proxy_module-----反向代理

image

1>在nginx服务进行以下配置

[root@lb02 /etc/nginx]# cat nginx.conf 
……               
 #   include /etc/nginx/conf.d/*.conf;

    upstream web_pools{
        server 10.0.0.7:80;
        server 10.0.0.8:80;
    }
    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;
        }   
    }   

2>检查语法,平滑重启服务

[root@lb02 /etc/nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb02 /etc/nginx]# systemctl reload nginx

3>在命令行用curl进行测试

[root@lb02 /etc/nginx]# curl 10.0.0.6
web02 www.oldboy.com
[root@lb02 /etc/nginx]# curl 10.0.0.6
web01 www.oldboy.com

4>抓包测试

首先在Windows本地hosts文件中配置解析

image

抓包

image

三、负载均衡相关配置详解

3.1 upstream模块内部server标签参数说明

server :ip或域名,如果端口不写,默认时80端口
weight:权重
max_fails :失败次数
fail_timeout :多久后在检查一遍
backup :如果加上backup 会在池塘中其他机器都挂掉 才会启动

image

1>weight权重测试

[root@lb02 /etc/nginx]# cat nginx.conf 
……
    upstream web_pools{
        server 10.0.0.7:80 weight=2;
        server 10.0.0.8:80 weight=1;
    }
……

[root@lb02 /etc/nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb02 /etc/nginx]# systemctl reload nginx
[root@lb02 /etc/nginx]# curl 10.0.0.6
web01 www.oldboy.com
[root@lb02 /etc/nginx]# curl 10.0.0.6
web01 www.oldboy.com
[root@lb02 /etc/nginx]# curl 10.0.0.6
web02 www.oldboy.com

2>fail_timeout(多久后在检查一遍)测试

[root@lb02 /etc/nginx]# cat nginx.conf 
……
    upstream web_pools{
        server 10.0.0.7:80 weight=1 max_fails=1 fail_timeout=4s;
        server 10.0.0.8:80 weight=1 max_fails=1 fail_timeout=4s;
    }
 ……
[root@lb02 /etc/nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb02 /etc/nginx]# systemctl reload nginx

for n in {1..1000};do curl 10.0.0.5/index.html ;sleep 1;done

image

3.2 用域名访问

3.3 多个域名访问/虚拟主机

多个虚拟主机的通过负载均衡

如:请求访问blog.oldboy.com,但一直显示www的内容

image

原因:
负载均衡向web服务器发出请求的时候,请求Host 域名时池塘名字(web_pools),web_pools IP地址,所以默认匹配第1个虚拟主机

image

解决:
用proxy_set_header修改请求头的内容,如:proxy_set_header Host $host;

[root@lb02 /etc/nginx]# cat nginx.conf 
……
    upstream web_pools{
        server 10.0.0.7:80 weight=1 max_fails=1 fail_timeout=4s;
        server 10.0.0.8:80 weight=1 max_fails=1 fail_timeout=4s;
    }
    server{ 
        listen 80;
        server_name www.oldboy.com;
        location / {
            proxy_pass http://web_pools;
            proxy_set_header Host $host;
        }
    }
    server{
        listen 80;
        server_name blog.oldboy.com;
        location / {
            proxy_pass http://web_pools;
            proxy_set_header Host $host;
        }
    }
[root@lb02 /etc/nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb02 /etc/nginx]# systemctl reload nginx
[root@lb02 /etc/nginx]# 

image

3.4 web服务器上面访问日志 记录用户IP

proxy_set_header X-Forwarded-For $remote_addr;

[root@lb02 /etc/nginx]# cat nginx.conf 
……
    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;
        }
    }
……
[root@lb02 /etc/nginx]# 
[root@lb02 /etc/nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb02 /etc/nginx]# systemctl reload nginx

image
image

四、添加访问控制

如果某些网段访问量成千上万,特别高的话,可能是被入侵了
需要给这个网址做限制访问

server {
listen 80;
server_name www.oldboy.com;
location / {
   if ($remote_addr ~ "^192.168.22.") {   \\指定禁止访问的网段
   return 403 "biedaoluan";  \\定义的是指定网段中,客户访问后返回的内容
   }
   proxy_pass http://web_pools;
   proxy_set_header Host $host;
   proxy_set_header X-Forwarded-For $remote_addr;
}

五、iptables命令的详解

详细介绍:http://man.linuxde.net/iptables

iptables命令是Linux上常用的防火墙软件,是netfilter项目的一部分。可以直接配置,也可以通过许多前端和图形界面配置。

语法:iptables(选项)(参数)

如:iptables -A INPUT -p tcp -s 192.168.22.0/24 -j DROP
-A:向规则链中添加条目;
-P:定义规则链中的默认目标;
-s:指定要匹配的数据包源
-j<目标>:指定要跳转的目标;
INPUT链:处理输入数据包。
DROP:丢弃数据包

5.1 清除已有iptables规则

iptables -F
iptables -X
iptables -Z

5.2 开放指定的端口

iptables -A INPUT -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT #允许本地回环接口(即运行本机访问本机)
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT #允许已建立的或相关连的通行
iptables -A OUTPUT -j ACCEPT #允许所有本机向外的访问
iptables -A INPUT -p tcp --dport 22 -j ACCEPT #允许访问22端口
iptables -A INPUT -p tcp --dport 80 -j ACCEPT #允许访问80端口
iptables -A INPUT -p tcp --dport 21 -j ACCEPT #允许ftp服务的21端口
iptables -A INPUT -p tcp --dport 20 -j ACCEPT #允许FTP服务的20端口
iptables -A INPUT -j reject #禁止其他未允许的规则访问
iptables -A FORWARD -j REJECT #禁止其他未允许的规则访问

5.3 屏蔽IP

iptables -I INPUT -s 123.45.6.7 -j DROP #屏蔽单个IP的命令
iptables -I INPUT -s 123.0.0.0/8 -j DROP #封整个段即从123.0.0.1到123.255.255.254的命令
iptables -I INPUT -s 124.45.0.0/16 -j DROP #封IP段即从123.45.0.1到123.45.255.254的命令
iptables -I INPUT -s 123.45.6.0/24 -j DROP #封IP段即从123.45.6.1到123.45.6.254的命令是

5.4 查看已添加的iptables规则

iptables -L -n -v
Chain INPUT (policy DROP 48106 packets, 2690K bytes)
 pkts bytes target     prot opt in     out     source               destination         
 5075  589K ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
 191K   90M ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:22
1499K  133M ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:80
4364K 6351M ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED
 6256  327K ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 3382K packets, 1819M bytes)
 pkts bytes target     prot opt in     out     source               destination         
 5075  589K ACCEPT     all  --  *      lo      0.0.0.0/0            0.0.0.0/0  
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,701评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,649评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,037评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,994评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,018评论 6 395
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,796评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,481评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,370评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,868评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,014评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,153评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,832评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,494评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,039评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,156评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,437评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,131评论 2 356

推荐阅读更多精彩内容