nginx负载均衡、动静分离

nginx负载均衡(动静分离)

工作原理:

Nginx是一个高性能的开源Web服务器和反向代理服务器,它也可以用于负载均衡。在动静分离方案中,Nginx可以作为一个负载均衡器来分发动态请求和静态请求到不同的后端服务器。

具体来说,Nginx负载均衡的原理如下:

1、请求到达Nginx:当客户端发送请求到Nginx服务器时,Nginx会接收到这个请求。
2、轮询方式分发请求:Nginx使用一种简单的轮询算法,按照事先配置好的服务器列表,将请求分发给后端服务器。它可以根据每个后端服务器的性能指标(如负载、响应时间等)进行调度,以实现负载均衡。
3、动态请求和静态请求的分离:Nginx可以根据URL或文件类型来判断请求是动态请求还是静态请求。通常静态请求是指直接返回文件,而动态请求则需要后端服务器进行处理并返回动态生成的内容。
4、静态请求的处理:当Nginx接收到静态请求时,它会直接从本地的静态文件目录中返回对应文件,无需后端服务器的参与。这样可以减轻后端服务器的负担,提高静态资源的访问速度。
5、动态请求的转发:当Nginx接收到动态请求时,它会转发该请求到后端服务器进行处理。后端服务器处理完请求后,将动态生成的内容返回给Nginx,再由Nginx将结果返回给客户端。

通过这种方式,Nginx可以实现对动态请求和静态请求的分离,并将负载均衡应用于后端服务器,从而提高系统的性能和可扩展性。

环境说明:

主机名称IP地址充当角色系统版本
lcy192.168.134.148负载均衡(调度器)centos 8
lnmp192.168.134.155动态网页centos 8
nginx192.168.134.154静态网页centos 8

首先需要部署好上述三台主机充当角色,在lcy上部署nginx服务、在lnmp上部署lnmp、在nginx上部署nginx

实现:

静态页面:

在这里插入图片描述

动态页面:

在这里插入图片描述

部署nginx负载均衡步骤:
在负载均衡(NGINX)主机上做配置:

proxy_pass:
proxy_pass是Nginx配置中的一个指令,用于将请求转发给指定的后端服务器。它可以将客户端请求的数据通过代理服务器转发到后端服务器,并将后端服务器的响应返回给客户端。这个指令通常用于实现反向代理、负载均衡、缓存等功能。

[root@lcy ~]# vim /usr/local/nginx/conf/nginx.conf

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


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 dynamic {                          //使用upstream定义动态服务器和静态服务器
        server 192.168.195.133:80 weight=1;     //此处192.168.195.133是动态页面的IP地址
    }

    upstream static {
        server 192.168.195.137:80 weight=1;     //192.168.195.137是静态页面的IP地址
    }

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {                   
            proxy_pass http://static;        //设置默认访问时,转发到静态页面上去
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        location ~ \.php$ {               //此处开启php的反向代理
            proxy_pass   http://dynamic;  //当我们访问php动态网页时跳转到动态页面上去
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

[root@lcy ~]# nginx -s reload      //重新加载

在浏览器上用负载均衡ip访问服务器

在这里插入图片描述

加上index.php访问

在这里插入图片描述

成功实现动静分离

配置多个静态访问网页

若想配置负载均衡多个网页,可以直接在upstream static选项中添加

[root@lcy ~]# vim /usr/local/nginx/conf/nginx.conf
...
upstream static {
        server 192.168.134.154:80 weight=1;
        server 192.168.134.154:8080 weight=1;  //直接在该项中添加我们想要加入负载均衡的ip及端口
    }
...

[root@lcy ~]# nginx -s reload     //重新加载

在nginx主机上添加一个server项(也就是虚拟主机)
[root@nginx conf]# pwd
/usr/local/nginx/conf
[root@nginx conf]# vim nginx.conf
[root@nginx conf]# cat nginx.conf      //添加如下内容
. . . . .
server {
        listen       8080;
        server_name  www.lcy.com;

    #access_log  logs/host.access.log  main;
    
    location / {
            root   html/www.lcy.com;
            index  index.html index.htm;
        }
    }
. . . . .
[root@nginx conf]# nginx -s reload     //重新加载
[root@nginx ~]# ss -antl     //查看8080端口是否启用
State        Recv-Q        Send-Q                  Local Address:Port                 Peer Address:Port        
LISTEN       0             128                           0.0.0.0:8080                      0.0.0.0:*           
LISTEN       0             128                           0.0.0.0:80                        0.0.0.0:*           
LISTEN       0             128                           0.0.0.0:22                        0.0.0.0:*           
LISTEN       0             128                              [::]:22      

配置完成

访问:
在这里插入图片描述
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值