实现负载均衡

1.安装依赖

sudo apt insta11 libgd-dev

2.下载nginx

wget http://nginx.org/download/nginx-1.22.1.tar.gz

3.解压nginx

tar -zvxf nginx-1.22.1.tar.g2

4.编译安装

cd nginx-1.22.1

5.编译并指定安装位置,执行安装之后会创建指定文件夹/www/env/nginx

./configure--prefix=/www/env/nginx--with-pcre

--with-http_ssl_module--with-http_v2_module

--with-http_realip_module--with-http_addition_module--with-http_sub_module

--with-http_dav_module

--with-http_fiv_module

--with-http_mp4_module

--with-http_gunzip_module

--with-http_gzip_static_module--with-http_random_index_module--with-http_secure_ink_module--with-http_stub_status_module--with-http_auth_request_module--with-http image filter module

./configure --prefix=/home/nginx--with-pcre

--with-http_ss1_module

--with-http_v2_module

--with-http_realip_module--with-http_addition_module--with-http_sub_module

--with-http_dav_module

--with-http_fiv_module

--with-http_mp4_module

--with-http_gunzip_module

--with-http_gzip_static_module--with-http_random_index_module--with-http_secure_link_module--with-http_stub_status_module--with-http_auth_request_module--with-http image filter_module--with-http_slice_module

--with-mai]

--with-threads

--with-file-aio

--with-stream

--with-mail_ss1_module

--with-stream_ssl module

安装结果:

nginx常用命令

nginx -s reload修改配置后重新加载生效

快速停止nginx

nginx -s stop

nginx -s start

启动nginx

nginx -s quit

完整有序的停止nginx

查看nginx的版本nginx -v

nginx -V

查看版本和nginx的配置选项

nginx-t-c/path/to/nginx.conf 测试nginx配置文件是否正确重新打开日志文件

nginx -s reopen、

启动nginx: ps -ef | grep nginx

首先通过SpringBoot+Freemarker快速搭建一个WEB项目:springboot-web-nginx然后在该项目中,创建一个IndexNginxController.java文件,逻辑如下:

复制代码
public class IndexNginxController {
    @Value("${server.port}")
    private String port;
​
    @RequestMapping("/")
    public ModelAndView index(){
        ModelAndView model = new ModelAndView();
        model.addObject("port", port);
        model.setViewName("index");
        return model;
    }
}

在该Controller类中,存在一个成员变量:port,它的值即是从application.properties配置文件中获取server.port值。当出现访问/资源的请求时,跳转前端index页面,并将该值携带返回。

前端的index.ftl文件代码如下:

复制代码
<html>
    <head>
        <title>Nginx演示页面</title>
        <link href="nginx_style.css" rel="stylesheet" type="text/css"/>
    </head>
    <body>
        <div style="border: 2px solid red;margin: auto;width: 800px;text-align: center">
            <div  id="nginx_title">
                <h1>$hello world 172.16.100.10{port}号!</h1>
            </div>
        </div>
    </body>
</html>
复制代码
复制代码

从上可以看出其逻辑并不复杂,仅是从响应中获取了port输出。

前提工作准备就绪后,再简单修改一下nginx.conf的配置即可:

复制代码
upstream nginx_boot{
   # 30s内检查心跳发送两次包,未回复就代表该机器宕机,请求分发权重比为1:2
   server 192.168.100.25:8080 weight=100 max_fails=2 fail_timeout=30s; 
   server 192.168.100.10:8090 weight=200 max_fails=2 fail_timeout=30s;
   # 这里的IP请配置成你WEB服务所在的机器IP
}
​
server {
    location / {
        root   html;
        # 配置一下index的地址,最后加上index.ftl。
        index  index.html index.htm index.jsp index.ftl;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        # 请求交给名为nginx_boot的upstream上
        proxy_pass http://nginx_boot;
    }
}

至此,所有的前提工作准备就绪,紧接着再启动Nginx,然后再启动两个web服务,第一个WEB服务启动时,在application.properties配置文件中,将端口号改为8080,第二个WEB服务启动时,将其端口号改为8090。

运行结果:

相关推荐
Ivanqhz10 小时前
激活函数在 Transformer 中的作用及各种变体简述
java·linux·数据库·人工智能·深度学习
殷色玫瑰12 小时前
C/C++ 内存管理详解:从内存分布到 new/delete 底层原理
java·linux·c语言·c++
quweiie13 小时前
Neo4j Community 安全访问配置总结
数据库·centos·neo4j
꯭自꯭闭꯭14 小时前
DM7主备升级方案
linux·服务器·数据库
用户41071920132514 小时前
VMware 虚拟机Ubuntu 环境linux远程连接失败
linux
lisanmengmeng14 小时前
Nagios邮件报警的配置
linux·运维·服务器
Ruiery14 小时前
Linux 6.6内核 CPU 深度解析(六):cpufreq
linux·运维·服务器
倔强的石头10614 小时前
【Linux指南】动静态库系列(十一):PLT 与延迟绑定:第一次调用动态库函数时发生了什么
linux·人工智能·python
硅基手札15 小时前
【linux内核专栏 08】时间与定时器
linux·运维·服务器
想做小南娘,发现自己是女生喵15 小时前
Linux之Ubuntu入门篇知识总结
linux·运维·服务器