一、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 
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  \[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  \[root@tomcat \~\]# /usr/local/nginx/sbin/nginx -s reload 
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  \[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  \[root@allowdeny nginx-1.26.1\]# /usr/local/nginx/sbin/nginx -s reload  \[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   \[root@tomcat \~\]# /usr/local/nginx/sbin/nginx -s reload 刷新    \[root@tomcat \~\]# vim /usr/local/nginx/conf/nginx.conf  \[root@tomcat \~\]# /usr/local/nginx/sbin/nginx -s reload  
2. weight 加权
root@tomcat \~\]# vim /usr/local/nginx/conf/nginx.conf 数字越大负担任务越重  \[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  \[root@tomcat \~\]# /usr/local/nginx/sbin/nginx -s reload 
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/ 
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