nginx负载均衡-轮询

news/2024/7/10 0:16:48 标签: nginx, 负载均衡, 运维

实验使用ubuntu做主机

1.安装nginx

安装依赖
sudo apt install libgd-dev
下载nginx
wget http://nginx.org/download/nginx-1.22.1.tar.gz
解压nginx
tar -zvxf nginx-1.22.1.tar.gz
编译安装
cd nginx-1.22.1
编译并指定安装位置,执行安装之后会创建指定文件夹/www/home/nginx
./configure --prefix=/www/home/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_flv_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-mail \
--with-threads \
--with-file-aio \
--with-stream \
--with-mail_ssl_module \
--with-stream_ssl_module 

问题1:

./configure: error: the HTTP rewrite module requires the PCRE library. You can either disable the module by using --without-http_rewrite_module option, or install the PCRE library into the system, or build the PCRE library statically from the source with nginx by using --with-pcre= option. PCRE库

PCRE库支持正则表达式。如果我们在配置文件nginx.conf中使用了正则表达式,那么在编译Nginx时就必须把PCRE库编译进Nginx,因为Nginx的HTTP模块需要靠它来解析正则表达式。另外,pcre-devel是使用PCRE做二次开发时所需要的开发库,包括头文件等,这也是编译Nginx所必须使用的。可以这样安装:

sudo apt update sudo apt install libpcre3 libpcre3-dev 问题2:

./configure: error: SSL modules require the OpenSSL library. You can either do not enable the modules, or install the OpenSSL library into the system, or build the OpenSSL library statically from the source with nginx by using --with-openssl= option OpenSSL库

如果服务器不只是要支持HTTP,还需要在更安全的SSL协议上传输HTTP,那么需要拥有OpenSSL。另外,如果我们想使用MD5、SHA1等散列函数,那么也需要安装它。可以这样安装:

sudo apt-get install openssl libssl-dev 执行编译并安装

make && make install

2.开启nginx

进入nginx目录并测试nginx进程是否开启
root@bx-virtual-machine:cd /www/home/nginx/sbin#
root@bx-virtual-machine:/www/home/nginx/sbin# ./nginx
root@bx-virtual-machine:/www/home/nginx/sbin# ps -ef | grep nginx
root        2626       1  0 18:22 ?        00:00:00 nginx: master process ./nginx
nobody      2627    2626  0 18:22 ?        00:00:00 nginx: worker process
root        2630    2263  0 18:22 pts/1    00:00:00 grep --color=auto nginx

浏览器进入 

 

进入nginx的html修改为显示自己主机 

其他两个主机一样配置,分别是132、136、137 

 

 

3.做负载均衡 

        进入192.168.230.136的/www/home/nginx/conf的nginx.conf中修改
upstream nginx_boot{
   # 30s内检查心跳发送两次包,未回复就代表该机器宕机,请求分发权重比为1:2
   server 192.168.230.132 weight=100 max_fails=2 fail_timeout=30s; 
   server 192.168.230.137 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 
root@bx-virtual-machine:/www/home/nginx/sbin# apt install nginx-core
root@bx-virtual-machine:/www/home/nginx/sbin# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

 4.测试

 一直按刷新会再132和137之间来回切换

 


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

相关文章

C#学习笔记_StringBuilder+程序效率测试

String问题:当程序中进行过多字符串处理操作时,会在内存中产生过多垃圾信息,影响程序效率。 StringBuilder简介 StringBuilder为一个类,属于引用类型。StringBuilder与string的区别在于,StringBuilder对于字符串的操…

(八)springboot实战——springboot3下的webflux项目全局异常处理

前言 在webflux响应式编程中,如何处理系统运行时异常是本节的主要内容。在传统的Servlet阻塞式web项目中主要通过HandlerExceptionResolver处理器来处理,而在webflux响应式web项目中,则是通过DispatchExceptionHandler异常处理器来处理异常。…

【Java万花筒】Java智能编程:探寻Jess、JADE、Neuroph和Apache OpenNLP的奇妙世界

Java中的智能力量:Jess、JADE、Neuroph和Apache OpenNLP深度解析 前言 在当今数字化的时代,处理和分析大量数据的需求越来越迫切。本文将介绍四个强大的Java工具包,它们分别是Jess、JADE、Neuroph和Apache OpenNLP。这些工具包提供了丰富的…

JS日期格式化(yyyy-MM-dd)

大家好,今天给大家分享的知识是前端如何将日期格式化为yyyy-MM-dd格式 一、格式化 在此我自己封装了一个方法,放到了工具类中,以便方便使用,代码如下,供大家参考: //将 2019-10-31 13:51:50.011 类型转为…

【算法专题】动态规划之子序列问题

动态规划5.0 动态规划 - - - 子序列问题(数组中不连续的一段)1. 最长递增子序列2. 摆动序列3. 最长递增子序列的个数4. 最长数对链5. 最长定差子序列6. 最长的斐波那契子序列的长度7. 最长等差数列8. 等差数列划分Ⅱ - 子序列 动态规划 - - - 子序列问题…

【leetcode100-063到068】【二分】六题合集

首先还是说一下通用框架,二分的整体结构基本都是设定搜索范围边界,检查中心元素,根据检查结果移动上界或下界来缩小搜索范围,直到范围中只剩一个可选元素(或没有可选)。 【搜索插入位置】 给定一个排序数…

npm更换镜像

大家好!今天给大家分享的知识是如何更换npm镜像 前言 有时候在加载npm时有时会很慢,那是由于node安装插件是从国外服务器下载,受网络影响大,速度慢且可能出现异常,这时候就需要更换镜像,使插件的安装快捷&…

vue3常用代码

文章目录 监听路由mitt、project/inject 无效解决方案 防抖函数 (已封装)下载函数 (已封装) 菜鸟做项目时发现很多 vue3 常用的代码,所以来总结一下! 监听路由 import { useRoute } from "vue-router"; let router useRoute(); watch(() &g…