2024年7月30日(nginx 代理 负载均衡 jdk )
创始人
2024-09-26 09:48:01
0
一、nginx代理
1、动态服务器

修改index.html文件,并且发布web项目

当前这台主机1动态服务器,后端服务器对标Java服务器,由于没有部署tomcat,所以使用nginx代理

[root@localhost ~]# echo "this is web server" > /usr/local/nginx/html/index.html 

[root@localhost ~]# source ~/nginx.sh 

[root@localhost ~]# curl localhost
this is web server

2、静态服务器

[root@localhost ~]# echo "this is static server" > /usr/local/nginx/html/index.html 
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost ~]# ps -aux | grep nginx

[root@localhost ~]# curl localhost
this is static server
[root@localhost ~]# curl 192.168.8.151
this is web server

使用152(static server)主机代理151(web server)主机,当用户访问152时,152不响应,而由151主机响应,使用152主机nginx反向代理151主机

location proxy_pass 协议 域名 端口

修改配置文件 /usr/local/nginx/conf/nginx.conf

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

[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload

现在的静态服务器实际尚是代理服务器,nignx代理其他服务的时候,不需要对方同意,更加方便模块化操作,如果代理一个服务器,

3、配置allowdeny服务器

[root@allowdeny ~]# scp root@192.168.8.151:~/nginx-1.26.1.tar.gz ./
[root@allowdeny ~]# yum -y install gcc gcc-c++ pcre-devel openssl-devel
[root@allowdeny ~]# tar -xzvf nginx-1.26.1.tar.gz 

[root@allowdeny nginx-1.26.1]# cd nginx-1.26.1

[root@allowdeny nginx-1.26.1]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-stream
[root@allowdeny nginx-1.26.1]# make &&  make install

[root@allowdeny nginx-1.26.1]# useradd -s /bin/nologin -M nginx
[root@allowdeny nginx-1.26.1]# /usr/local/nginx/sbin/nginx

修改index.html文件内容

[root@allowdeny nginx-1.26.1]# /usr/local/nginx/sbin/nginx 
[root@allowdeny nginx-1.26.1]# echo "your are luckly" > /usr/local/nginx/html/index.html 
[root@allowdeny nginx-1.26.1]# curl localhost
your are luckly

[root@allowdeny nginx-1.26.1]# vim /usr/local/nginx/conf/nginx.conf


[root@allowdeny nginx-1.26.1]# /usr/local/nginx/sbin/nginx -s reload

[root@localhost ~]# curl 192.168.8.153
your are luckly
[root@localhost ~]# curl 192.168.8.153

403 Forbidden

403 Forbidden



nginx/1.26.1


二、负载均衡

让每一台主机能够获取相应的压力,轮询,依次将任务部署给不同主机,权重 

static server192.168.8.159
dynamicserver d0192.168.8.155
dynamicserver d1192.168.8.158
dynamicserver d2192.168.8.160

[root@static ~]# echo "I am static server" > /usr/local/nginx/html/index.html

[root@d0 ~]# echo "I am dynamicserver d0" > /usr/local/nginx/html/index.html

 [root@d0 ~]# echo "I am dynamicserver d1" > /usr/local/nginx/html/index.html

[root@d0 ~]# echo "I am dynamicserver d2" > /usr/local/nginx/html/index.html

常用的状态有:

weight:服务访问的权重,默认是1。
down:表示当前的servel 时不参与负载均衡。
backup:预留的备份机品。当其他所有的非backup机器出现故障或者忙的时候,才会请求
backup机器,因此这台机器的压力最轻。
max_fails:在fail_timeout时间内,允许请求最大的失败次数,默认为1。当达到最大失败时,
会在fail_timeout时间内不允许再次被选择。,返回proxy_next_upstream模块定义的错误。
fail_timeout:单位为秒,默认是10秒。指定一段时间内,请求经历了max_fails次失败后,该
server不能访问的时间(暂停服务的时间)。max_fails可以和fail_timeout-起使用。
注意:当负载调度算法为ip_hash时,后端服务器在负载均衡调度中的状态不能是backup。

1、轮询

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

 

[root@static ~]# /usr/local/nginx/sbin/nginx -s reload

[root@static ~]# vim /usr/local/nginx/conf/nginx.conf  (d2 down)

[root@static ~]# /usr/local/nginx/sbin/nginx -s reload

2、权重

[root@static ~]# vim /usr/local/nginx/conf/nginx.conf  (权重值大的访问次数多)

[root@static ~]# /usr/local/nginx/sbin/nginx -s reload

3、ip_hash

■ 当对后端的多台动态应用服务器做负载均衡时,ip_hash指令能够将某个客户端IP的请求通
过哈希算法定位到同一台后端服务器上。
■ 这样,当来自某一个IP的用户在后端Web服务器A上登录后,再访问该站点的其他URL,能
保证其访问的还是后端web服务器A。

■ 注意:使用ip_hash指令无法保证后端服务器的负载均衡,可能导致有些后端服务器接收到
的请求多,有些后端服务器接受的请求少,而且设置后端服务器权重等方法将不起作用

[root@static ~]# vim /usr/local/nginx/conf/nginx.conf (第一次访问出来是那个就一直是那个)

[root@static ~]# /usr/local/nginx/sbin/nginx -s reload

4、least_conn

least_conn:最少连接,把请求转发给连接数较少的后端服务器。轮询算法是把请求平均
地转发给各个后端,使它们的负载大致相同;但是,有些请求占用的时间农长,会导致其
所在的后端负载较高。这种情况下,leastconn这种方式就可以达到更好的负载均衡效果。

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

 三、平滑升级

1、查看nginx当前版本

[root@static ~]# /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.26.1

2、平滑升级到1.27版本

(1)服务持续期间对nginx升级

下载新的nginx

[root@static ~]# wget https://nginx.org/download/nginx-1.27.0.tar.gz

[root@static ~]# tar -zxvf nginx-1.27.0.tar.gz 

[root@static nginx-1.27.0]# ./configure --prefix=/usr/local/nginx/ --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-stream
[root@static nginx-1.27.0]# make && make install

[root@static nginx-1.27.0]# /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.27.0
[root@static nginx-1.27.0]# /usr/local/nginx/sbin/nginx.old -v
nginx version: nginx/1.26.1

使用kill -USR2 启动新版本的NGINX服务

[root@static nginx-1.27.0]# ps -aux|grep nginx

[root@static nginx-1.27.0]# kill -USR2 1262

[root@static nginx-1.27.0]# kill -WINCH 1294
[root@static nginx-1.27.0]# kill -QUIT 1262

[root@static nginx-1.27.0]# curl -I localhost
HTTP/1.1 200 OK
Server: nginx/1.27.0
Date: Tue, 30 Jul 2024 08:44:10 GMT
Content-Type: text/html
Content-Length: 22
Connection: keep-alive
Last-Modified: Tue, 30 Jul 2024 07:44:21 GMT
ETag: "66a899d5-16"
Accept-Ranges: bytes 

tomcat9 可以在jdk8的环境运行
# tomcat10 必须在jdk17以上的版本运行
#在实际的工作中,不需要这么高的版本,在实训,要求使用最新斑斑
#新版本只换骨不换皮,我们使用新版本,为了让大家知道各个程序之间版本依赖管理

# 配置tomcat 10 运行环境

[root@d0 ~]#  wget https://download.oracle.com/java/22/latest/jdk-22_linux-x64_bin.tar.gz

[root@d0 ~]# tar -xzvf jdk-22_linux-x64_bin.tar.gz 

[root@d0 bin]# cd jdk-22.0.2/

[root@d0 bin]# cd bin/
[root@d0 bin]# ./java

[root@d0 ~]# mv jdk-22.0.2/ /usr/local/jdk22/

[root@d0 ~]# cd /usr/local/jdk22/
[root@d0 jdk22]# ls
[root@d0 jdk22]# pwd
/usr/local/jdk22
[root@d0 jdk22]# sed -n '$p' /etc/profile
unset -f pathmunge

[root@d0 jdk22]# sed -i '$aexport JAVA_HOME=/usr/local/jdk22/' /etc/profile
[root@d0 jdk22]# sed -n '$p' /etc/profile
export JAVA_HOME=/usr/local/jdk22/
[root@d0 jdk22]# source /etc/profile

[root@d0 jdk22]# sed -i '$aPATH=$JAVA_HOME/bin:$PATH' /etc/profile

[root@d0 jdk22]# sed -n '$p' /etc/profile
PATH=$JAVA_HOME/bin:$PATH
[root@d0 jdk22]# source /etc/profile
[root@d0 jdk22]# java -version

[root@d0 ~]# wget https://dlcdn.apache.org/tomcat/tomcat-10/v10.1.26/bin/apache-tomcat-10.1.26.tar.gz

相关内容

热门资讯

安卓系统升级速度,从快速迭代到... 你有没有发现,每次打开手机,安卓系统总在默默地进行升级?这就像是我们的小助手,时不时地给自己充充电,...
安卓苹果系统流畅性,系统流畅性... 你有没有发现,现在手机的世界里,安卓和苹果两大巨头就像是在赛跑,你追我赶,谁都不愿意落后。今天,咱们...
浩顺收银系统下载安卓,助力商家... 你有没有想过,在繁忙的商场里,那些收银员是如何快速而准确地完成每一笔交易的?这其中,浩顺收银系统可是...
安卓系统怎么降级吗,轻松实现系... 你有没有发现,手机用久了,系统更新换代的速度简直比兔子还快!这不,你的安卓手机可能也跟着潮流升级到了...
安卓系统外接鼠标设置吗,轻松实... 你有没有想过,给你的安卓手机或者平板电脑配上一个鼠标,让它瞬间变身成一台迷你电脑呢?这听起来是不是很...
微信拼三张房卡哪里有卖/狂飙大... 拼三张是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:33903369许多玩家在游戏中会购买房卡来享...
微信里上玩拼三张购买房卡/新永... 拼三张是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:55051770许多玩家在游戏中会购买房卡来享...
买房卡的链接炸金花房卡/微信群... 炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:8488009许多玩家在游戏中会购买房卡来享受...
安卓的系统最好用的,探索最实用... 你有没有发现,手机里的安卓系统简直是个宝藏啊!它不仅功能强大,而且用起来超级顺滑,简直让人爱不释手。...
微信玩炸金花房卡找谁买/微信群... 炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:33903369许多玩家在游戏中会购买房卡来享...
微信炸金花房卡找谁购买/牛牛金... 炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:55051770许多玩家在游戏中会购买房卡来享...
怎么创建牛牛房间房卡/新皇豪大... 牛牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:8488009许多玩家在游戏中会购买房卡来享受更...
微信炸金花玩房卡在哪买/微信牛... 炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:33903369许多玩家在游戏中会购买房卡来享...
国产手机脱离安卓系统,探索自主... 你知道吗?最近国产手机界可是掀起了一股不小的风潮呢!那就是——脱离安卓系统!是不是觉得有点不可思议?...
炸金花房卡链接如何购买/微信斗... 炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:55051770许多玩家在游戏中会购买房卡来享...
微信群打炸金花链接房卡/微信大... 炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:8488009许多玩家在游戏中会购买房卡来享受...
炸金花链接房卡从哪里买/玩斗牛... 炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:33903369许多玩家在游戏中会购买房卡来享...
微信群牛牛房间怎么开/新海狮大... 牛牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:55051770许多玩家在游戏中会购买房卡来享受...
微信群炸金花房间买房卡/新九天... 炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:8488009许多玩家在游戏中会购买房卡来享受...
微信玩链接炸金花房卡/青龙大厅... 炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:33903369许多玩家在游戏中会购买房卡来享...