nginx反向代理及负载均衡

node1192.168.136.55Nginx主负载均衡器
node3192.168.136.57Web01服务器
node4192.168.136.58Web02服务器
node5192.168.135.131客户端(测试)

 nginx反向代理

1. 安装nginx

三台机器都安装nginx

yum install nginx -y

2. 配置用于测试的Web服务(以下操作在两台web服务器)。 配置虚拟主机

[root@node3 conf.d]# mkdir -p /usr/share/nginx/html/{www,bbs}/logs 
[root@node3 ~]# cd /etc/nginx/conf.d/
[root@node3 conf.d]# vim vhost.conf
server {
  listen 80;
  server_name bbs.yunjisuan.com;
  location / {
      root /usr/share/nginx/html/bbs;
      index index.html index.htm;
  }
        access_log /usr/share/nginx/html/bbs/logs/access_bbs.log main;
}
server {
    listen 80;
    server_name www.yunjisuan.com;
    location / {
        root /usr/share/nginx/html/www;
        index index.html index.htm;
    }
        access_log /usr/share/nginx/html/www/logs/access_www.log main;
}
# nginx -t 测语法
# systemctl start nginx

node4 一样配置

3. 配置用于测试的Web服务 在两台web服务器上操作。 准备测试页面

echo "`hostname -I `www" > /usr/share/nginx/html/www/index.html
echo "`hostname -I `bbs" > /usr/share/nginx/html/bbs/index.html

 客户端测试:

# vi + /etc/hosts
192.168.136.57    bbs.yunjisuan.com www.yunjisuan.com
192.168.136.58    bbs.yunjisuan.com www.yunjisuan.com
[root@node5 ~]# curl www.yunjisuan.com
192.168.136.57 www
[root@node5 ~]# curl bbs.yunjisuan.com
192.168.136.57 bbs
#或者
[root@node5 ~]# curl -H host:bbs.yunjisuan.com 192.168.136.57
192.168.136.57 bbs
[root@node5 ~]# curl -H host:www.yunjisuan.com 192.168.136.57
192.168.136.57 www

 nginx负载均衡

实现 Nginx 负载均衡的组件说明

Nginx http 功能模块模块说明
ngx_http_proxy_moduleproxy 代理模块,用于把请求后拋给服务器节点或 upstream 服 务器池
ngx_http_upstream_module负载均衡模块, 可以实现网站的负载均衡功能及节点的健康检査

配置简单的负载均衡 以下操作在node1

实现:经过反向代理后的节点服务器记录用户IP

          反向代理多虚拟主机节点服务器

# cd /etc/nginx/conf.d/
# vim vhost.conf
upstream wwwPools {
  server 192.168.136.57;
  server 192.168.136.58;
}

server {
  listen 80;
  server_name www.yunjisuan.com;

  location / {
    proxy_pass http://wwwPools;
    proxy_set_header Host $host;   # 依据host字段信息,识别代理的是哪个虚拟主机
    proxy_set_header X-Forwarded-For $remote_addr;  # 显示客户端的IP地址
  }
}


server {
  listen 80;
  server_name bbs.yunjisuan.com;

  location / {
   proxy_pass http://wwwPools;
   proxy_set_header Host $host;
   proxy_set_header X-Forwarded-For $remote_addr;
  }
}

客户端:  (改成反向代理的地址)

# vim /etc/hosts
192.168.136.55    bbs.yunjisuan.com www.yunjisuan.com
[root@node5 ~]# for ((i=1;i<=4;i++)); do curl http://bbs.yunjisuan.com;done
192.168.136.58  bbs
192.168.136.57  bbs
192.168.136.58  bbs
192.168.136.57  bbs

如果web服务器是apache

# vim vhost.conf
<VirtualHost *:80>
        ServerName bbs.yunjisuan.com
        DocumentRoot "/var/www/html/bbs/"

        CustomLog "/var/www/html/bbs/logs/access_log" combined
</VirtualHost>

<VirtualHost *:80>
        ServerName www.yunjisuan.com
        DocumentRoot "/var/www/html/www/"

        CustomLog "/var/www/html/www/logs/access_log" combined
</VirtualHost>
# cp /usr/share/nginx/html/www/index.html /var/www/html/www
# cp /usr/share/nginx/html/bbs/index.html /var/www/html/bbs

修改配置文件 vim /etc/httpd/conf/httpd.conf

LogFormat "%h %{X-FORWARDED-FOR}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

如果是tomcat

改成 

pattern="%{X-FORWARDED-FOR}i %l %u %t %r %s %b %D %q %{User-Agent}i" resolveHosts="false"

根据URL中的目录地址实现代理转发

vim /etc/nginx/conf.d/vhost.conf

upstream static_pools {
        server 192.168.136.57;
}

upstream upload_pools {
        server 192.168.136.58;
}

upstream default_pools {
        server 192.168.136.59;
}

server {
        listen 80;
        server_name www.yunjisuan.com;

        location /static/ {
                proxy_set_header X-Forwarded-For $remote_addr;
                proxy_pass http://static_pools;
        }

         location /upload/ {
                proxy_set_header X-Forwarded-For $remote_addr;
                proxy_pass http://upload_pools;
        }

         location / {
                proxy_set_header X-Forwarded-For $remote_addr;
                proxy_pass http://default_pools;
        }


}

在node3上创建页面 

[root@node3 static]# echo "static web page test." > /usr/share/nginx/html/static/index.html 

在node4上

[root@openEuler-node4 ~]# mkdir /var/www/html/upload
[root@openEuler-node4 ~]# echo "upload test page" > /var/www/html/upload/index.html

在192.168.136.59上

# echo "default test page" > /usr/share/nginx/html/index.html
[root@node5 conf.d]# curl http://www.yunjisuan.com/upload/
upload test page
[root@node5 conf.d]# curl http://www.yunjisuan.com/static/
static web page test.
[root@node5 conf.d]# curl http://www.yunjisuan.com
default test page

根据客户端的设备(user_agent)转发 

[root@node1 ~]# vim /etc/nginx/conf.d/vhost.conf 

upstream static_pools {
        server 192.168.136.57;
}

upstream upload_pools {
        server 192.168.136.58;
}

upstream default_pools {
        server 192.168.136.59;
}

server {
        listen 80;
        server_name www.yunjisuan.com;


        location / {
        if ($http_user_agent ~* "MSIE")
        {       proxy_pass http://static_pools;
        }

        if ($http_user_agent ~* "Chrome")
        {
                proxy_pass http://upload_pools;
        }
                proxy_pass http://default_pools;

        }

}
[root@node3 html]# echo "static page" > /usr/share/nginx/html/index.html
[root@node4 html]# echo "upload test page " > /usr/share/nginx/html/index.html
[root@node5 conf.d]# curl -A chrome -H host:www.yunjisuan.com 192.168.136.55
upload test page 
[root@node5 conf.d]# curl -A MSIE -H host:www.yunjisuan.com 192.168.136.55
static page
[root@node5 conf.d]# curl -A XXX -H host:www.yunjisuan.com 192.168.136.55
default test page

 

upstream其他使用

修改为IP HASH算法

始终只由一台服务器提高 

添加server备份  

当前面都down了 启动backup

注意:backup不能和ip_hash同时开启  

想完全不启用其中的一台server

1、backup所有RS都不能提高服务时才起作用

2、backup 不能和ip_hash算法一起使用

3、down 通常用于RS维护时,不参与调度

4、ip_hash 会话保持

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Nginx是一款常用的Web服务器软件,也可以用作反向代理负载均衡器。反向代理是指Nginx作为一个中间服务器,接收客户端的请求并将其转发给后端的服务器处理,然后将响应返回给客户端。 负载均衡是指将客户端请求分发到多个后端服务器上,以平衡服务器的负载。Nginx通过使用不同的负载均衡算法,如轮询、IP哈希、最少连接等,来决定将请求发送给哪个后端服务器。 配置Nginx作为反向代理负载均衡器需要进行一些设置。首先,你需要在Nginx配置文件中定义后端服务器的地址和端口,并设置相应的负载均衡策略。例如: ```nginx http { upstream backend { server backend1.example.com; server backend2.example.com; server backend3.example.com; } server { listen 80; server_name mywebsite.com; location / { proxy_pass http://backend; } } } ``` 在上述配置中,`upstream`指令定义了后端服务器的地址,在这里我们指定了三个后端服务器。`server`块中的`location`指令指定了代理转发的路径,`proxy_pass`指令将请求转发给定义的`upstream`。 这样配置之后,当有客户端请求到达Nginx时,Nginx会根据定义的负载均衡策略将请求转发给后端服务器,并将后端服务器的响应返回给客户端。 这就是Nginx反向代理负载均衡的基本概念和配置方法。希望对你有所帮助!如果还有其他问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Fish_1112

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值