2024.7.30(nginx反向代理、nginx负载均衡)

一、nginx反向代理

1、动态服务器 后端服务器 对标Java服务器
1. 修改index.html文件,并且发布web服务

root@git \~\]# echo "this is java web server" \> /usr/local/nginx/html/index.html \[root@git \~\]# /usr/local/nginx/sbin/nginx \[root@git \~\]# /usr/local/nginx/sbin/nginx -s reload

2. 使用curl访问当前目录

root@git \~\]# curl git this is java web server ![](https://i-blog.csdnimg.cn/direct/3e9e5f3fb4434a248a7c2b4ed68e928f.png)

2、启动静态服务器(代理服务器)

要求使用128主机代理134,当用户访问128时,128不响应,而是134主机响应

使用128主机nginx反向代理134的服务器

nginx代理其他服务器时,不需要对方同意,更加方便了模块化操作,如果代理一个服务器,双方都需要同意

1. 启动服务

root@tomcat \~\]# echo "this is static server" \> /usr/local/nginx/html/index.html \[root@tomcat \~\]# /usr/local/nginx/sbin/nginx -s reload \[root@tomcat \~\]# ps -aux \| grep nginx ![](https://i-blog.csdnimg.cn/direct/14466c0e631942c29e59125fb78338eb.png) \[root@tomcat \~\]# curl tomcat this is static server \[root@tomcat \~\]# curl 192.168.8.134 this is java web server

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

location proxy_pass 协议 域名 端口

root@tomcat \~\]# vim /usr/local/nginx/conf/nginx.conf ![](https://i-blog.csdnimg.cn/direct/48cbfd86ca6142a18721b5f82d9f9501.png) \[root@tomcat \~\]# /usr/local/nginx/sbin/nginx -s reload ![](https://i-blog.csdnimg.cn/direct/8babc86adf2f4695ae2069104b131bdb.png)

3、设置黑名单和白名单
1. 源码编译安装

root@allowdeny \~\]# scp -r [email protected]:\~/nginx-1.26.1.tar.gz ./ \[root@allowdeny \~\]# yum -y install gcc gcc-c++ pcre-devel openssl-devel \[root@allowdeny \~\]# tar -zxvf nginx-1.26.1.tar.gz \[root@allowdeny \~\]# 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

2. 修改index.html

root@allowdeny nginx-1.26.1\]# echo "you are luckly" \> /usr/local/nginx/html/index.html \[root@allowdeny nginx-1.26.1\]# curl allowdeny you are luckly ![](https://i-blog.csdnimg.cn/direct/254b4244323d4f28b4da9186ad1cfa76.png) \[root@git \~\]# curl 192.168.8.136 you are luckly \[root@tomcat \~\]# curl 192.168.8.136 you are luckly

3. 设置除了134主机可以访问,其他主机都不可以访问

server模块中设置 allow允许 deny禁止 可以对ip生效,也可以对网段生效

root@allowdeny nginx-1.26.1\]# vim /usr/local/nginx/conf/nginx.conf ![](https://i-blog.csdnimg.cn/direct/787f2dd1593840ac8e70932a2d88a581.png) \[root@allowdeny nginx-1.26.1\]# /usr/local/nginx/sbin/nginx -s reload ![](https://i-blog.csdnimg.cn/direct/387381d9177e4f479cb18697ec5fab26.png) \[root@git \~\]# curl 192.168.8.136 you are luckly

4、错误日志

root@allowdeny \~\]# cd /usr/local/nginx/ \[root@allowdeny nginx\]# tree logs/ logs/ ├── access.log ├── error.log └── nginx.pid 0 directories, 3 files \[root@allowdeny nginx\]# cat logs/access.log

二、负载均衡

让每一台主机能够获得相应的压力

轮询 依次将任务部署给不同的主机 (权重)

|-----------------|---------------|
| staticserver | 192.168.8.128 |
| dynamicserver00 | 192.168.8.134 |
| dynamicserver01 | 192.168.8.135 |
| dynamicserver02 | 192.168.8.136 |

1、启动四台主机

root@tomcat \~\]# echo "I am static server" \> /usr/local/nginx/html/index.html \[root@git \~\]# echo "I am d00 server" \> /usr/local/nginx/html/index.html \[root@dns \~\]# echo "I am d01 server" \> /usr/local/nginx/html/index.html \[root@allowdeny \~\]# echo "I am d02 server" \> /usr/local/nginx/html/index.html

2、在staticserver主机上添加模块(upstream)

常用的状态有:

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@tomcat \~\]# vim /usr/local/nginx/conf/nginx.conf ![](https://i-blog.csdnimg.cn/direct/c0db0b5a8675430cb84b91aa63d61f33.png) ![](https://i-blog.csdnimg.cn/direct/b159219edfaa49809a9df2a10b861426.png) \[root@tomcat \~\]# /usr/local/nginx/sbin/nginx -s reload 刷新 ![](https://i-blog.csdnimg.cn/direct/f55f76b309754281a314015c53080c3f.png) ![](https://i-blog.csdnimg.cn/direct/ef55e3d4ed774e3c8d21066009bc56ea.png) ![](https://i-blog.csdnimg.cn/direct/89d154d9d44945c3a7e0e71f4a1f811c.png) \[root@tomcat \~\]# vim /usr/local/nginx/conf/nginx.conf ![](https://i-blog.csdnimg.cn/direct/17b53d53e61a4d4d9f7027184d31bbc1.png) \[root@tomcat \~\]# /usr/local/nginx/sbin/nginx -s reload ![](https://i-blog.csdnimg.cn/direct/5cba746e31984d0d8d752d5f248431a2.png) ![](https://i-blog.csdnimg.cn/direct/d99a724542f04dc786b984397b184389.png)

2. weight 加权

root@tomcat \~\]# vim /usr/local/nginx/conf/nginx.conf 数字越大负担任务越重 ![](https://i-blog.csdnimg.cn/direct/b25d88dc975d49b2867a0ab45a91b95f.png) \[root@tomcat \~\]# /usr/local/nginx/sbin/nginx -s reload

3. ip_hash

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

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

root@tomcat \~\]# vim /usr/local/nginx/conf/nginx.conf ![](https://i-blog.csdnimg.cn/direct/39187facd3c3406c9bbb35e61b80d032.png) \[root@tomcat \~\]# /usr/local/nginx/sbin/nginx -s reload ![](https://i-blog.csdnimg.cn/direct/179a0a7cd07c4aa1ac8a1b862c4f8f3b.png)

4. least_conn

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

三、平滑升级

1、查看nginx当前版本

root@tomcat \~\]# /usr/local/nginx/sbin/nginx -v nginx version: nginx/1.26.1

2、平滑升级到1.27
1. 服务持续期间对nginx升级

root@tomcat \~\]# wget https://nginx.org/download/nginx-1.27.0.tar.gz \[root@tomcat \~\]# tar -zxvf nginx-1.27.1.tar.gz \[root@tomcat \~\]# cd nginx-1.27.0/ \[root@tomcat nginx-1.27.0\]# ./configure --prefix=/usr/loacal/nginx/ --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-stream \[root@tomcat nginx-1.27.0\]# make \&\& make install \[root@tomcat nginx-1.27.0\]# ls /usr/local/nginx/sbin/ ![](https://i-blog.csdnimg.cn/direct/6e6966107a4c48999754c224a7a8a79d.png)

2. 杀 nginx-1.26.1进程

root@tomcat nginx-1.27.0\]# ps -aux \| grep nginx root 1321 0.0 0.2 46172 2040 ? Ss 09:34 0:00 nginx: master process /usr/local/nginx/sbin/nginx nginx 11370 0.0 0.1 46600 1932 ? S 16:35 0:00 nginx: worker process root 14365 0.0 0.0 112824 980 pts/0 R+ 16:39 0:00 grep --color=auto nginx \[root@tomcat nginx-1.27.0\]# kill -USR2 1321 \[root@tomcat nginx-1.27.0\]# kill -WINCH 11370 \[root@tomcat nginx-1.27.0\]# kill -QUIT 1321 \[root@tomcat 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

3、安装jdk

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

配置tomcat 10 运行环境

root@git \~\]# wget https://download.oracle.com/java/22/latest/jdk-22_linux-x64_bin.tar.gz \[root@git \~\]# tar -zxvf jdk-22_linux-x64_bin.tar.gz \[root@git \~\]# cd jdk-22.0.2/ \[root@git jdk-22.0.2\]# cd bin/ \[root@git bin\]# ls \[root@git \~\]# mv jdk-22.0.2/ /usr/local/jdk22/ \[root@git \~\]# ls /usr/local/jdk22/ \[root@git \~\]# ls \[root@git \~\]# cd /usr/local/jdk22/ \[root@git jdk22\]# pwd /usr/local/jdk22 \[root@git jdk22\]# sed -n '$p' /etc/profile unset -f pathmunge \[root@git jdk22\]# sed -i '$aexport JAVA_HOME=/usr/local/jdk22' /etc/profile \[root@git jdk22\]# source /etc/profile \[root@git jdk22\]# $JAVA_HOME -bash: /usr/local/jdk22: 是一个目录 \[root@git jdk22\]# sed -i '$aPATH=$JAVA_HOME/bin:$PATH' /etc/profile \[root@git jdk22\]# sed -n '$p' /etc/profile PATH=$JAVA_HOME/bin:$PATH \[root@git jdk22\]# source /etc/profile \[root@git jdk22\]# java -version \[root@dns \~\]# wget https://dlcdn.apache.org/tomcat/tomcat-10/v10.1.26/bin/apache-tomcat-10.1.26.tar.gz

相关推荐
进击的CJR1 小时前
MySQL 8.0 OCP 英文题库解析(五)
数据库·mysql·开闭原则
✿ ༺ ོIT技术༻2 小时前
Linux:网络层的重要协议或技术
linux·服务器·网络
DanmF--2 小时前
Protobuf协议生成和使用
网络·unity·c#·游戏引擎·游戏程序
ICT系统集成阿祥3 小时前
负载均衡—会话保持技术详解
运维·负载均衡
付出不多3 小时前
linux——mysql高可用
linux·运维·mysql
观无4 小时前
数据库DDL
数据库·oracle
消失在人海中4 小时前
Oracle 内存优化
数据库·oracle
昭阳~5 小时前
MySQL读写分离
数据库·mysql
L汐8 小时前
07 负载均衡
运维·nginx·负载均衡
pjx9878 小时前
服务间的“握手”:OpenFeign声明式调用与客户端负载均衡
java·运维·spring·负载均衡