nginx负载均衡(动静分离)

news/2024/7/10 2:35:48 标签: nginx, 负载均衡, 运维, linux, 架构, php

nginx_0">nginx负载均衡(动静分离)

文章目录

工作原理:

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

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

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

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

环境说明:

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

首先需要部署好上述三台主机充当角色,部署nginxlnmp详细步骤请阅读:

源码编译安装部署lnmp

实现的效果如下:

静态页面:

在这里插入图片描述

动态页面:
在这里插入图片描述

nginx_39">部署nginx负载均衡步骤:

负载均衡(NGINX)主机上做配置:

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

[root@NGINX ~]# vim /usr/local/nginx/conf/nginx.conf
[root@NGINX ~]# cat /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@NGINX ~]# nginx -s reload         //重新加载

测试:在浏览器上访问负载均衡主机ip

默认访问

在这里插入图片描述

php_182">加上index.php访问

在这里插入图片描述

成功实现动静分离

配置多个静态访问网页

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

在NGINX主机上
[root@NGINX ~]# cat /usr/local/nginx/conf/nginx.conf
. . . . . . 
upstream static {
        server 192.168.195.137:80 weight=1;
        server 192.168.195.137:8080 weight=1;   //直接在该项中添加我们想要加入负载均衡的ip及端口
    }
. . . . . .
[root@NGINX ~]# 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.ftx.com;

    #access_log  logs/host.access.log  main;
    
    location / {
            root   html/www.ftx.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                           [::]:*

配置完成

访问静态页面负载均衡测试

在这里插入图片描述
在这里插入图片描述

配置完成


http://www.niftyadmin.cn/n/5108109.html

相关文章

【微信小程序开发】自定义组件以及页面布局设计

🥳🥳Welcome Huihuis Code World ! !🥳🥳 接下来看看由辉辉所写的关于小程序的相关操作吧 目录 🥳🥳Welcome Huihuis Code World ! !🥳🥳 一.自定义组件的使用步骤(附实…

这个建筑工地神器,工作效率杠杠的!

智慧工地是一种基于现代技术的建筑施工管理方法,它借助物联网、大数据分析和人工智能等技术,提高了工地的安全性、效率和可持续性。通过实时数据监测和智能决策支持,智慧工地正在为建筑行业带来革命性的变革。 ​客户案例 建筑公司 浙江某建设…

面试八股文:C++ 多态 继承 重载 虚函数

C 支持多态、继承和函数重载,这些是面向对象编程(OOP)的基本概念。以下是这些概念的简要介绍: 多态(Polymorphism): 多态是面向对象编程的核心概念之一,它允许不同的子类对象对相同的…

NewStarCTF 2023 公开赛道 WEEK2|MISC1-序章

题目 172.17.0.1 - - [20/Aug/2023:00:08:21 0800] "GET /app/action/edit_sell.php?pid%5B0%5D-1%20or%20if(ascii(substr((select%20group_concat(username,password)%20from%20user),1,1))40,sleep(1),1)--&totalPrice0 HTTP/1.1" 500 353 "-" &q…

网上可以赚钱的软件,闲暇时间可用来薅羊毛做副业

正因为有了互联网,我们的生活变得越来越便利。我们可以在网上购物、休闲娱乐、与朋友交流,甚至可以通过网上做副业来赚取额外收入。生活中总会有一些业余时间,而很多人也不想浪费它,总想着做一点事情来弥补经济上的不足。因此&…

Allegro如何快速修线

在Allegro PCB设计中,通常修改比较乱的走线,都是用Slide修线功能,但Allegro的自动平滑走线功能,比手动修线更方面,更好用。用好了,可以提高PCB设计的效率。 杂乱的走线如下图 选择菜单Route 选择Custom Smooth(自定义平滑),或者点击自己设置的快捷键。

window10/11 光学系统建模之Light Tools8.6 软件安装教程(亲测可用+附带免费资源可直接下载)

1.下载链接 (免费分享) 链接:https://pan.baidu.com/s/1qVubyRSC93xT-GKeK-k3ow 提取码:vkho 2.安装顺序 即按照图里的1234的顺序先安装完注册表,驱动,和lighttools的程序 我个人在win10系统安装这些程…

MD-MTSP:粒子群优化算法PSO求解多仓库多旅行商问题MATLAB(可更改数据集,旅行商的数量和起点)

一、多仓库多旅行商问题MD-MTSP 多旅行商问题(Multiple Traveling Salesman Problem, MTSP)是著名的旅行商问题(Traveling Salesman Problem, TSP)的延伸,多旅行商问题定义为:给定一个𝑛座城市…