前端服务器:用于接收和响应客户端,代理另一台主机
Nginx 安装
(1).下载安装nginx
[root@web ~]# wget https://nginx.org/download/nginx-1.26.1.tar.gz
(2).解压
[root@web ~ ]# tar -zxvf nginx-1.26.1.tar.gz
(3).编译安装 nginx
(1)安装 pcre
⾸先安装 pcre。此软件是为了⽀持 rewrite(重写、复写)功能⽽存在的。rewrite 是实现 url 重定向的重要命令,它会根据正则表达式来匹配内容,从⽽跳转到⽬标上⾯去。
[root@web ~]# yum -y install pcre-devel
(2)安装 OpenSSL
当没有使⽤ ssl 证书对服务器数据进⾏加密认证时,⽤户的数据将会以明⽂的形式进⾏传输,⽽此时,⽤户的数据可以被⼀些抓包⼯具获取,就容易造成⽤户的信息泄露。
所以为了改善这种情况,作为运维⼈员需要去为⽹站配置 ssl 证书,实现 https 协议的访问
[root@web ~]# yum -y install openssl-devel
(3)在安装之前还要安装⽤来编译的⼯具
gcc、gcc-c++、make;lrzsz 是⽤来从 Windows 上直接将⽂件导⼊到 Linux 内的⼯具。
[root@web ~]# yum -y install gcc gcc-c++ make
(4)编译安装 nginx
如果之前在系统上有 yum 安装的 nginx,那么在编译安装之前 需要先卸载掉原来的 nginx。
卸载nginx:yum -y remove nginx
(5)进行编译
[root@web ~]# ls nginx-1.26.1 nginx-1.26.1.tar.gz
cd到安装包⽬录下
[root@web ~]# cd nginx-1.26.1/
[root@web nginx-1.26.1]# https://blog.csdn.net/qq_70752758/article/details/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@web nginx-1.26.1]# make && make install
[root@web nginx-1.26.1]# useradd -s /bin/nologin -M nginx
(6)检查目录
[root@web nginx-1.26.1]# tree /usr/local/nginx
[root@web nginx-1.26.1]# cd /usr/local/nginx/
[root@web nginx]# ls
conf html logs sbin
(7)备份目录
[root@web nginx]# cp -r conf/ conf.bak
[root@web nginx]# https://blog.csdn.net/qq_70752758/article/details/sbin/nginx
(8)开放端口或者服务
[root@web nginx]# firewall-cmd --zone=public --add-port=80/tcp --permanent success [root@web nginx]# firewall-cmd --reload success
[root@web nginx]# vim /usr/local/nginx/conf/nginx.conf
(9)创建符号链接(软链接)
[root@web nginx]# ln -s /usr/local/nginx/sbin/nginx /usr/bin/
[root@web nginx]# ls -l
/usr/bin/nginxlrwxrwxrwx. 1 root root 27 7月 29 16:16 /usr/bin/nginx -> /usr/local/nginx/sbin/nginx
[root@web nginx]# nginx
[root@web nginx]# nginx -s stop
[root@web nginx]# netstat -lnput|grep nginx
[root@web nginx]# nginx
[root@web nginx]# netstat -lnput|grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 17228/nginx: master
(10)启动和关闭nginx服务
https://blog.csdn.net/qq_70752758/article/details/nginx https://blog.csdn.net/qq_70752758/article/details/nginx -s reload
修改了配置文件后,重载nginx服务网
https://blog.csdn.net/qq_70752758/article/details/nginx -s reload
(11)脚本启动nginx服务:
[root@web nginx]# vim ~/nginx.sh
[root@web nginx]# bash ~/nginx.sh
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 17228/nginx: master
nginx正在执行,或者是80端口被占用
[root@web nginx]# nginx -s stop
[root@web nginx]# bash ~/nginx.sh
12)以systemctl控制nginx
[root@web nginx]# vim /usr/lib/systemd/system/nginx.service
如果直接使用sbin目录下的nginx,就无法使用systemctl
这两套命令只能用一套
(1)[root@web nginx]# systemctl daemon-reload
[root@web nginx]# systemctl stop nginx
(2)[root@web nginx]# nginx -s reload [root@web nginx]# nginx -s stop
(13)添加监控块
[root@web nginx]# vim /usr/local/nginx/conf/nginx.conf
[root@web nginx]# systemctl restart nginx
[root@web ~]# wget https://nginx.org/download/nginx-1.26.1.tar.gz
[root@web ~ ]# tar -zxvf nginx-1.26.1.tar.gz
[root@web ~]# yum -y install gcc gcc-c++
[root@web ~]# yum -y install openssl-devel pcre-devel make
[root@web ~]# lsnginx-1.26.1 nginx-1.26.1.tar.gz
[root@web ~]# cd nginx-1.26.1/
[root@web nginx-1.26.1]# https://blog.csdn.net/qq_70752758/article/details/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@web nginx-1.26.1]# make && make install
[root@web nginx-1.26.1]# useradd -s /bin/nologin -M nginx
[root@nginx nginx-1.26.1]# echo "我是后端服务" > /usr/local/nginx/html/index.html
[root@nginx nginx-1.26.1]# firewall-cmd --zone=public --add-port=80/tcp --permanent
success [root@nginx nginx-1.26.1]# firewall-cmd --reload
success
修改配置文件
[root@nginx nginx-1.26.1]# vim /usr/local/nginx/conf/nginx.conf
将其修改为utf-8(防止出现乱码)
[root@nginx nginx-1.26.1]# /usr/local/nginx/sbin/nginx
[root@nginx nginx-1.26.1]# /usr/local/nginx/sbin/nginx -s reload
浏览器访问:192.168.2.27
[root@nginx ~]# echo "this is java web server" > /usr/local/nginx/html/index.html
[root@nginx ~]# #启动nginx
[root@nginx ~]# /usr/local/nginx/sbin/nginx
[root@nginx ~]# #使用curl访问当前项目
[root@nginx ~]# curl localhost
this is java web server
修改配置文件 /usr/local/nginx/conf/nginx.conf
[root@web ~]# vim /usr/local/nginx/conf/nginx.conf
[root@web ~]# /usr/local/nginx/sbin/nginx -s reload
浏览器访问IP:192168.2.25,访问到的内容是后端服务器的
现在的静态服务器实际上是代理服务器,nginx代理其他服务的时候,不需要对方同意
二、设置黑名单、白名单
1.安装配置nginx环境
2.修改一下index.html中的内容
[root@nginx-1 nginx-1.26.1]# echo "you are luckly" > /usr/local/nginx/html/index.html [root@nginx-1 nginx-1.26.1]# curl localhost
you are luckly
3.修改配置文件 此台服务器IP:192.168.2.28
前端服务器IP:192.168.2.25
后端服务器IP:192.1682.27
设置除开27(后端服务器)可以访问,其他主机都不可以访问 在配置文件中的server模块中设置,allow为允许,deny为禁止,可以对IP生效,也可以对网段生效
[root@nginx-1 nginx-1.26.1]# vim /usr/local/nginx/conf/nginx.conf
4.重启服务
[root@nginx-1 nginx-1.26.1]# /usr/local/nginx/sbin/nginx -s reload
5.测试
分别在前端服务器(25)、后端服务器(27)进行测试:
前端服务器(25):
后端服务器(27):
6.说明:
让每一台主机能够获得相应的压力
轮询:依次的将任务部署给不同的主机
1.准备四台机器:
2.都配置nginx环境
wget https://nginx.org/download/nginx-1.26.1.tar.gz
tar -zxvf nginx-1.26.1.tar.gz yum -y install pcre-devel
yum -y install openssl-devel yum -y install gcc gcc-c++ make
cd nginx-1.26.1/
https://blog.csdn.net/qq_70752758/article/details/configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-stream
make && make install
useradd -s /bin/nologin -M nginx
3.写测试页面:
[root@static-server ~]# echo "I am static server" > /usr/local/nginx/html/index.html
[root@001 ~]# echo "I am 001 server" > /usr/local/nginx/html/index.html
[root@002 ~]# echo "I am 002 server" > /usr/local/nginx/html/index.html
[root@003 ~]# echo "I am 003 server" > /usr/local/nginx/html/index.html
4.启动服务(static-server、001、002、003):
/usr/local/nginx/sbin/nginx
(1)配置代理和轮询
[root@static-server ~]# vim /usr/local/nginx/conf/nginx.conf
轮询:
代理:
[root@static-server ~]# /usr/local/nginx/sbin/nginx -s reload
浏览器访问static-server的IP:192.168.2.25
static-server会代理001、002、003这三台机器,刷新浏览器会出现轮询
(2)weight加权
权重默认为1,谁权重大,谁优先处理请求
[root@static-server ~]# vim /usr/local/nginx/conf/nginx.conf
[root@static-server ~]# /usr/local/nginx/sbin/nginx -s reload
浏览器访问IP:192.168.2.25
001出现的次数最多,003出现的次数最少
(3)ip_hash
当对后端的多台动态应用服务器做负载均衡时,ip_hash指令能够将某个客户端IP的请求通过哈希算法定位到同一台后端服务器上。
这样,当来自某一个IP的用户在后端Web服务器A上登录后,再访问该站点的其他URL,能保证其访问的还是后端web服务器A。
注意: 使用ip_hash指令无法保证后端服务器的负载均衡,可能导致有些后端服务器接收到的请求多,有些后端服务器接受的请求少,而且设置后端服务器权重等方法将不起作用
[root@static-server ~]# vim /usr/local/nginx/conf/nginx.conf
[root@static-server ~]# /usr/local/nginx/sbin/nginx -s reload
浏览器访问IP:192.168.2.25
第一次访问到001的测试页面,之后刷新浏览器一直是001的测试页面,不会变。
(4)least_conn
least_conn:最少连接,把请求转发给连接数较少的后端服务器。轮询算法是把请求平均地转发给各个后端,使它们的负载大致相同;但是,有些请求占用的时间很长,会导致其所在的后端负载较高。这种情况下,leastconn这种方式就可以达到更好的负载均衡效果。
(5)url_hash
按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,要配合缓存命中来使用。同一个资源多次请求,可能会到达不同的服务器上,导致不必要的多次下载,缓存命中率不高,以及一些资源时间的浪费。而使用ur_hash,可以使得同一个url (也就是同一个资源请求)会到达同一台服务器,一旦缓存住了资源,再次收到请求,就可以从 缓存中读取。
不停用业务,使用平滑升级
需要有kill命令的支持
kill不仅仅用于杀死进程,还可以向软件进程发送信号
常用的-9和-15一个是强杀,一个是正常杀
格式:kill 信号 进程信号
-USR2 平滑启动一个进程,平滑升级
-WINCH 优雅关闭子进程
-QUIT 优雅关闭主进程
步骤:
1.不停止原有服务,但是必须使用原生方式启动或者更改nginx脚本(会创建一个新的进程)
2.重新编译nginx新版本
3.使用kill -USR@启动新版本
4.把旧的nginx子进程全部退出
5.优雅的退出nginx的老进程,系统里就只剩下新的nginx了
1.查看nginx当前版本
2.上传新的新nginx版本并解压
平滑升级到1.27
服务持续期间对nginx进行升级
3.对新版本进行编译安装,安装目录必须和旧版本一致
4.对新版本进行编译安装,安装目录必须和旧版本一致
5.查看nginx的新旧版本
6.查看进程
[root@static-server nginx-1.27.0]# ps -aux|grep nginx
7.使用老的nginx进程创建新的进程
8.关闭老版本的所有子进程和老版本的主进程
9.使用curl查看当前服务器的版本
配置tomcat 10运行环境 tomcat9 可以在jdk8的环境运行
tomcat10必须在jdk17以上的版本运行
tomcat-22版本下载网址:https://download.oracle.com/java/22/latest/jdk-22_linux-x64_bin.tar.gz