nginx实现负载均衡

一、Nginx的基本信息

Nginx是一个高性能的HTTP和反向代理服务,也是一个IMAP/POP3/SMTP服务。其特点是占有内存少,并发能力强。在连接高并发的情况下,Nginx是Apache服务不错的替代品,能够支持高达 50,000 个并发连接数的响应。
nginx的官方网站:http://nginx.org

二、搭建nginx服务器

1、安装包下载及解压

[root@server2 ~]# tar zxf nginx-1.14.0.tar.gz 

2、修改配置文件
(1)不显示版本号

[root@nginx ~]# cd  nginx-1.14.0/src/core/
[root@nginx core]# vim nginx.h

在这里插入图片描述
(2)关掉debug功能

[root@nginx nginx-1.14.0]# cd  auto/cc/
[root@nginx cc]# vim gcc 

在这里插入图片描述
3、源码安装nginx
(1)安装所需依赖包
[root@nginx nginx-1.14.0]# yum install gcc pcre-devel openssl-devel -y
(2)源码安装三部曲

[root@nginx nginx-1.14.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio
[root@nginx nginx-1.14.0]# make
[root@nginx nginx-1.14.0]# make install
补充:
configure 支持下面的选项:
--prefix=<path> - Nginx安装路径。如果没有指定,默认为 /usr/local/nginx。
--sbin-path=<path> - Nginx可执行文件安装路径。只能安装时指定,如果没有指定,默认为<prefix>/sbin/nginx。
--conf-path=<path> - 在没有给定-c选项下默认的nginx.conf的路径。如果没有指定,默认为<prefix>/conf/nginx.conf。
--pid-path=<path> - 在nginx.conf中没有指定pid指令的情况下,默认的nginx.pid的路径。如果没有指定,默认为 <prefix>/logs/nginx.pid。
--lock-path=<path> - nginx.lock文件的路径。
--error-log-path=<path> - 在nginx.conf中没有指定error_log指令的情况下,默认的错误日志的路径。如果没有指定,默认为 <prefix>/logs/error.log。
--http-log-path=<path> - 在nginx.conf中没有指定access_log指令的情况下,默认的访问日志的路径。如果没有指定,默认为 <prefix>/logs/access.log。
--user=<user> - 在nginx.conf中没有指定user指令的情况下,默认的nginx使用的用户。如果没有指定,默认为 nobody。
--group=<group> - 在nginx.conf中没有指定group指令的情况下,默认的nginx使用的组。如果没有指定,默认为 nobody。
--builddir=DIR - 指定编译的目录
--with-rtsig_module - 启用 rtsig模块
--with-http_realip_module - 启用 ngx_http_realip_module
--with-http_addition_module - 启用 ngx_http_addition_module
--with-http_sub_module - 启用 ngx_http_sub_module
--with-http_dav_module - 启用 ngx_http_dav_module
--with-http_flv_module - 启用 ngx_http_flv_module
--with-http_stub_status_module - 启用 "server status" 页
--without-http_charset_module - 禁用 ngx_http_charset_module
--without-http_gzip_module - 禁用 ngx_http_gzip_module. 如果启用,需要 zlib。
--without-http_ssi_module - 禁用 ngx_http_ssi_module
--without-http_userid_module - 禁用 ngx_http_userid_module
--without-http_access_module - 禁用 ngx_http_access_module
--without-http_auth_basic_module - 禁用 ngx_http_auth_basic_module
--without-http_autoindex_module - 禁用 ngx_http_autoindex_module
--without-http_geo_module - 禁用 ngx_http_geo_module
--without-http_map_module - 禁用 ngx_http_map_module
--without-http_referer_module - 禁用 ngx_http_referer_module
--without-http_rewrite_module - 禁用 ngx_http_rewrite_module. 如果启用需要 PCRE。
--without-http_proxy_module - 禁用 ngx_http_proxy_module
--without-http_fastcgi_module - 禁用 ngx_http_fastcgi_module
--without-http_memcached_module - 禁用 ngx_http_memcached_module
--without-http_limit_zone_module - 禁用 ngx_http_limit_zone_module
--without-http_empty_gif_module - 禁用 ngx_http_empty_gif_module
--without-http_browser_module - 禁用 ngx_http_browser_module
--without-http_upstream_ip_hash_module - 禁用 ngx_http_upstream_ip_hash_module
--with-http_perl_module - 启用 ngx_http_perl_module
--with-perl_modules_path=PATH - 指定 perl模块的路径
--with-perl=PATH - 指定 perl 执行文件的路径
--http-log-path=PATH - Set path to the http access log
--http-client-body-temp-path=PATH - Set path to the http client request body temporary files
--http-proxy-temp-path=PATH - Set path to the http proxy temporary files
--http-fastcgi-temp-path=PATH - Set path to the http fastcgi temporary files
--without-http - 禁用 HTTP server
--with-mail_ssl_module - 启用 ngx_mail_ssl_module
--with-cc=PATH - 指定 C编译器的路径
--without-pcre - 禁止 PCRE 库的使用。同时也会禁止 HTTP rewrite 模块。在 "location" 配置指令中的正则表达式也需要 PCRE。
--with-pcre=DIR - 指定 PCRE 库的源代码的路径。
--with-openssl=DIR - Set path to OpenSSL library sources
--with-openssl-opt=OPTIONS - Set additional options for OpenSSL building
--with-debug - 启用调试日志
--add-module=PATH - Add in a third-party module found in directory PATH

4、创建软链接(创建完成后可直接调用)

[root@server nginx-1.14.0]# cd /usr/local/nginx/
[root@server nginx]# ln -s /usr/local/nginx/sbin/nginx /sbin/

编辑text.html页面
[root@server html]# pwd
/usr/local/nginx/html
[root@server html]# vim test.html
在这里插入图片描述
在物理机中测试:
在这里插入图片描述

三、nginx实现负载均衡

1、配置
(1)修改worker_processes的值
[root@server ~]# cd /usr/local/nginx/conf
[root@server conf]# vim nginx.conf
worker_processes进程数,一般来说 ,拥有几个cpu,就设置几个进程数
查看cpu :lscpu
在这里插入图片描述
(2)修改 用户和用户组
首先添加nginx用户并指定家目录

[root@server conf]# useradd -M -d /usr/local/nginx/ -u 800 nginx

在这里插入图片描述
当cpu 为两个时 worker_cpu_affinity 01 10;
当cpu为四个时 worker_cpu_affinity 0001 0010 0100 1000;
(3)修改最大链接数

[root@server conf]# vim nginx.conf

在这里插入图片描述
在操作系统上进行修改

[root@server conf]# vim /etc/security/limits.conf

在这里插入图片描述
(4)设置轮叫uptream

[root@server conf]# vim nginx.conf

在这里插入图片描述
添加定位:当访问www.westos.org的80端口时把页面定位到upstream westos
在这里插入图片描述
(5)检测语法并重新加载
在这里插入图片描述
(6)测试
1)负载均衡算法rr
在测试主机(物理主机)中添加本地解析在这里插入图片描述
开启server3和server4中httpd服务
在物理主机中测试:访问www.westos.org
在这里插入图片描述
在这里插入图片描述
2)负载均衡算法给定权重:

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

在这里插入图片描述
在物理主机中测试:

[root@foundation66 kiosk]# for i in {1..10}; do curl www.westos.org;done

在这里插入图片描述
3)负载均衡算法ip_hash(同一个ip访问后端服务器不变)
Nginx中的ip_hash技术能够将某个ip的请求定向到同一个后端web机器中,这样,这个ip下的客户端和某个后端web机器就能建立起稳固的链接
配置nginx服务

[root@server conf]# vim nginx.conf

在这里插入图片描述

[root@server conf]# nginx -s reload
[root@server conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

测试:物理主机中测试:

[root@foundation66 kiosk]# for i in {1..10}; do curl www.westos.org;done

在这里插入图片描述
(7)健康检查
配置nginx文件

[root@server conf]# vim nginx.conf

在这里插入图片描述

[root@server conf]# nginx -s reload
[root@server conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

在浏览器中测试(关闭server3和server4中的httpd服务)
在这里插入图片描述

四、在nginx中添加模块sticky

sticky是nginx的一个模块,它时基于cookie的一种nginx的负载均衡解决方案,通过分发和识别cookie,来使得同一个客户端的请求落在同一台服务器上,默认标识名为route
1.客户端首次发起访问请求,nginx接收后,发现请求头没有cookie,则以轮询方式将请求分发给后端服务器。
2.后端服务器处理完请求,将响应数据返回给nginx。
3.此时nginx生成带route的cookie,返回给客户端。route的值与后端服务器对应,可能是明文,也可能是md5、sha1等Hash值
4.客户端接收请求,并保存带route的cookie。
5.当客户端下一次发送请求时,会带上route,nginx根据接收到的cookie中的route值,转发给对应的后端服务器。

1、关闭nginx

[root@server conf]# nginx -s stop

2、解压nginx-goodies-nginx-sticky-module-ng-08a395c66e42.zip

[root@server ~]# yum install -y unzip	#安装解压工具
[root@server ~]# unzip nginx-goodies-nginx-sticky-module-ng-08a395c66e42.zip	#解压 

3、编译安装

[root@server nginx-1.14.0]# make clean	#清理缓存
rm -rf Makefile objs
[root@server nginx-1.14.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio --add-module=/root/nginx-goodies-nginx-sticky-module-ng-08a395c66e42
[root@server nginx-1.14.0]# make && make install

4、修改配置文件

[root@server nginx-1.14.0]# cd /usr/local/nginx/conf/
[root@server conf]# vim nginx.conf
[root@server conf]# nginx 

在这里插入图片描述
5、测试
在这里插入图片描述
在浏览器中测试
在这里插入图片描述

内容概要:本文档介绍了一个多目标规划模型,该模型旨在优化与水资源分配相关的多个目标。它包含四个目标函数:最小化F1(x),最大化F2(x),最小化F3(x)和最小化F4(x),分别对应于同的资源或环境指标。每个目标函数都有具体的数值目标,如F1的目标值为1695亿立方米水,而F2则追求达到195.54亿立方米等。此外,模型还设定了若干约束条件,包括各区域内的水量限制以及确保某些变量低于特定百分比的下限。特别地,为了保证模型的有效性和合理性,提出需要解决目标函数间数据尺度一致的问题,并建议采用遗传算法或其他先进算法进行求解,以获得符合预期的决策变量Xi(i=1,2,...,14)的结果。 适合人群:对数学建模、运筹学、水资源管理等领域感兴趣的科研人员、高校师生及从业者。 使用场景及目标:①适用于研究涉及多目标优化问题的实际案例,尤其是水资源分配领域;②帮助读者理解如何构建和求解复杂的多目标规划问题,掌握处理同尺度数据的方法;③为从事相关工作的专业人士提供理论参考和技术支持。 阅读建议:由于文档涉及到复杂的数学公式和专业术语,在阅读时应先熟悉基本概念,重点关注目标函数的具体定义及其背后的物理意义,同时注意理解各个约束条件的设计意图。对于提到的数据尺度一致问题,建议深入探讨可能的解决方案,
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值