配置Nginx负载均衡方法

一、相关概念

正向代理::通常的代理服务器,只用于代理内部网络对Internet的连接请求,客户机必须指定代理服务器,并将本来要直接发送到Web服务器上的http请求发送到代理服务器中。

反向代理:代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个反向代理服务器。

负载均衡: 负载均衡是扩展应用程序并提高其性能和冗余的绝佳方法。在负载 均衡配置中,nginx充当在多个单独服务器上工作的分布式Web应用程序的单个入口点。
在这里插入图片描述
补充理解
反向代理:用户访问服务器,服务器知道客户信息,客户不知道服务器信息,理解为隐藏服务端。
正向代理:用户访问服务器,服务器不知道客户信息,客户知道服务器信息,理解为隐藏客户端。

二、环境说明

node1:作为代理服务器
node2:作为web+server01
node3:作为web+server02
control:作为客户端进行测试

二、测试nginx服务

1.测试服务开启状态

[root@node1 ~]# systemctl status nginx.service 
● nginx.service - nginx
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Sun 2021-05-09 23:22:15 CST; 2min 4s ago
  Process: 881 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
 Main PID: 900 (nginx)
    Tasks: 2 (limit: 12404)
   Memory: 2.5M
   CGroup: /system.slice/nginx.service
           ├─900 nginx: master process /usr/local/nginx/sbin/nginx
           └─901 nginx: worker process

May 09 23:22:14 node1 systemd[1]: Starting nginx...
May 09 23:22:15 node1 systemd[1]: Started nginx.

2.测试客户端访问nginx的文web是否正常

在这里插入图片描述

三、配置负载均衡

1.配置nginx.conf

http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    upstream web {
       server 192.168.200.136;
       server 192.168.200.133;
        }
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://web;
          }
        }


2.测试node2和node3的web服务器状态

[root@control ansible]# curl node2
welcome to node2.exampel.com!
[root@control ansible]# curl node3
welcome to node3.exampel.com!

四、测试反向代理与负载均衡

[root@control ansible]# 
[root@control ansible]# curl node2
welcome to node2.exampel.com!
[root@control ansible]# curl node3
welcome to node3.exampel.com!
[root@control ansible]# curl node1
welcome to node3.exampel.com!
[root@control ansible]# curl node1
welcome to node2.exampel.com!
[root@control ansible]# curl node1
welcome to node3.exampel.com!
[root@control ansible]# curl node1
welcome to node2.exampel.com!
[root@control ansible]# curl node1
welcome to node3.exampel.com!
[root@control ansible]# curl node1
welcome to node2.exampel.com!
[root@control ansible]# curl node1
welcome to node3.exampel.com!
[root@control ansible]# curl node1
welcome to node2.exampel.com!
[root@control ansible]# curl node1
welcome to node3.exampel.com!

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

江湖有缘

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

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

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

打赏作者

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

抵扣说明:

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

余额充值