image
一、准备环境工作
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 创建站点目录与首页文件
俩边都相同
[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]
image
二、编写nginx反向代理服务配置文件(lb01)
ngx_http_upstream_module 负载均衡
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首页文件追加内容让容易区分
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
image
三、抓包
image四、upstream模块参数:
server —— RS配置,可以是ip或域名
weight ——权重
max_fails ——失败次数
fail_timeout =10s ——多久后在检查一遍
backup ——如果加上backup 会在池塘中其他机器都挂掉 才会启动
down 让服务器不可用
image image image image
五、配置权重
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加速缓存
网站加速 缓存网站静态页面 视频(切片)
用户先访问cdn
cdn缓存没有 就转到源站
cdn公司介绍:蓝汛 网宿 阿里云
image
七、配置文件中添加server模块的参数(lb01)
weight 权重;
max_fails 健康检查,失败次数;
fail_timeout 多久后在检查一遍
修改配置模块参数
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;
}
测试关闭一台后,是否还能访问:
for n in {1..1000};do curl 10.0.0.5/index.html ;sleep 1;done
image
八、请求访问第二个站点blog.oldboy.com
image
1.抓包看一下情况:
image
2.修改 请求头
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.再访问就成功了
image
九、显示客户端的地址,并记录到日志中
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;
}
}
这里测试的是在lb01上curl的web01
image image
十、添加访问控制
如果某些网段访问量成千上万,特别高的话,可能是被入侵了
需要给这个网址做限制访问
server {
listen 80;
server_name www.oldboy.com;
location / {
if ($remote_addr ~ "^192.168.22.") { \\指定禁止访问的网段
return 403 "别捣乱"; \\定义的是指定网段中,客户访问后返回的内容
}
proxy_pass http://web_pools;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
十一、防火墙规则—iptables
iptables详细用法http://man.linuxde.net/iptables
image image--dport 指定端口号
iptables -A INPUT -p tcp -s 192.168.22.0/24 -j DROP
-A:向规则链中添加条目;
-P:定义规则链中的默认目标;
-s:指定要匹配的数据包源ip地址;
-j<目标>:指定要跳转的目标;
指定网段,配置时不要把自己挡外面,这就要跑机房了~
image image-F:清楚规则链中已有的条目;
-Z:清空规则链中的数据包计算器和字节计数器;
-X:删除用户自定义的链
image image
网友评论