linux篇—Nginx反向代理负载均衡

一、环境准备

反向代理功能架构
3台web服务器,组建出web服务器集群
web01 10.0.0.7 172.16.1.7
web02 10.0.0.8 172.16.1.8
web03 10.0.0.9 172.16.1.9
1台负载均衡服务器
lb01 10.0.0.5 172.16.1.5

 

二、部署web服务和负载均衡服务

1. 安装部署nginx

 在三台web服务器和负载均衡服务器上都部署上nginx

mkdir /server/tools -p
cd /server/tools
wget http://nginx.org/download/nginx-1.12.2.tar.gz
tar xf nginx-1.12.2.tar.gz
yum install -y pcre-devel openssl-devel
useradd -M -s /sbin/nologin www
cd nginx-1.12.2
./configure --prefix=/application/nginx-1.12.2 --user=www --group=www --with-http_ssl_module --with-http_stub_status_module
make && make install
ln -s /application/nginx-1.12.2 /application/nginx
/application/nginx/sbin/nginx
netstat -lntup|grep nginx

 

 

2. 编辑nginx配置文件

 

server {
    listen       80;
    server_name  www.etiantian.org;
    root   html/www;
    index  index.html index.htm;
}
server {
    listen       80;
    server_name  bbs.etiantian.org;
    root   html/bbs;
    index  index.html index.htm;
}
#将配置文件分发到其他两台web服务器 scp
-rp /application/nginx/conf/nginx.conf 172.16.1.8:/application/nginx/conf/ scp -rp /application/nginx/conf/nginx.conf 172.16.1.8:/application/nginx/conf/

 

 

3. 在三台web服务器上创建模拟测试环境

mkdir /application/nginx/html/{www,bbs} -p
for name in www bbs;do echo "$(hostname) $name.etiantian.org" >/application/nginx/html/$name/test.html;done
for name in www bbs;do cat /application/nginx/html/$name/test.html;done

 

 

4. 在负载均衡服务器上,进行测试访问

curl -H host:www.etiantian.org 10.0.0.7/test.html
web01 www.etiantian.org
curl -H host:bbs.etiantian.org 10.0.0.7/test.html
web01 bbs.etiantian.org
curl -H host:www.etiantian.org 10.0.0.8/test.html
web02 www.etiantian.org
curl -H host:bbs.etiantian.org 10.0.0.8/test.html
web02 bbs.etiantian.org
curl -H host:www.etiantian.org 10.0.0.9/test.html
web03 www.etiantian.org
curl -H host:bbs.etiantian.org 10.0.0.9/test.html
web03 bbs.etiantian.org

 

 

5. 在负载均衡lb01服务器上编写nginx反向代理配置文件

01. 简化配置文件
cd /application/nginx/conf/

grep -Ev "#|^$" nginx.conf.default >nginx.conf

 

  

02. 在nginx.conf中编写upstream和proxy_pass模块

说明:upstream模块就类似定一个一个地址池或者说定一个web服务器组

官方链接:http://nginx.org/en/docs/http/ngx_http_upstream_module.html#upstream

语法:

Syntax:    upstream name { ... }
Default:    —
Context:    http

 

eg:

#添加web服务器节点
upstream oldboy { server
10.0.0.7:80; server 10.0.0.8:80; server 10.0.0.9:80; }

 

 

说明:proxy_pass主要用于进行抛送用户访问请求给upstream模块中的相应节点服务器

官方文档:http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass

Syntax:    proxy_pass URL;
Default:    —
Context:    location, if in location, limit_except
eg:
location / {
   proxy_pass http://test;
}

 

eg:

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    upstream test {
        server 10.0.0.7:80;
        server 10.0.0.8:80;
        server 10.0.0.9:80;
    }
    server {
        listen       80;
        server_name  localhost;
        root   html;
        index  index.html index.htm;
      location / {
        proxy_pass http://test;
       }   
    }
}

 

编写完后重启nginx服务

/application/nginx/sbin/nginx -t
/application/nginx/sbin/nginx -s reload

 

 

6. 进行访问负载均衡服务器测试

1)利用浏览器进行测试
   进行hosts解析
   http://www.etiantian.org/oldboy.html  <--利用ctrl+F5刷新测试,检查是否进行负载调度
2)利用curl命令进行测试
   [root@lb01 conf]# curl -H host:www.etiantian.org 10.0.0.5/test.html
   web01 www.etiantian.org
   [root@lb01 conf]# curl -H host:www.etiantian.org 10.0.0.5/test.html
   web02 www.etiantian.org
   [root@lb01 conf]# curl -H host:www.etiantian.org 10.0.0.5/test.html
   web03 www.etiantian.org

 

转载于:https://www.cnblogs.com/xh-0205/p/11132680.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值