nginx配置属性监控、nginx代理动态服务器、nginx访客IP黑名单、负载均衡、平滑升级及tomcat安装与配置

Day 13

nginx配置属性监控

环境准备

两台主机:static-server(192.168.2.22)   dynamic-server(192.168.2.31)

部署一台静态服务器static-server

[root@static ~ ]#yum -y install gcc gcc-c++ pcre-devel openssl-devel

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

[root@static ~ ]# ls

anaconda-ks.cfg  index.html  nginx-1.26.1  nginx-1.26.1.tar.gz  nginx.sh

# 编译

[root@static 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@static nginx-1.26.1]# make && make install

[root@static nginx-1.26.1]# useradd -s /bin/nologin -M nginx  # 创建用户

# 不要使用systemctl启动,也不要使用脚本启动

[root@static nginx-1.26.1]# /usr/local/nginx/sbin/nginx

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

location /status {

            stub_status   on;

access_log   off;

}

# 重新加载配置文件

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

nginx代理动态服务器(如代理tomcat发布的动态web服务)

部署一台动态服务器(即后端服务器、对标、java服务器)dynamic-server

# 修改index.html并发布web服务

[root@dy001 ~]# ls

anaconda-ks.cfg  index.html  nginx-1.26.1  nginx-1.26.1.tar.gz  nginx.sh

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

# 启动nginx

[root@dy001 ~]# cd nginx-1.26.1/

[root@dy001 nginx-1.26.1]# /usr/local/nginx/sbin/nginx

# 使用curl访问当前项目

[root@dy001 nginx-1.26.1]# curl localhost

this is java web-server

# 浏览器测试

部署一台静态服务器static-server

# 将当前的主机作为前端服务器用于接收和响应客户端,代理另一台主机

# 启动nginx服务

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

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

root       1417  0.0  0.0  46128  1152 ?        Ss   10:19   0:00 nginx: master process /usr/local/nginx/sbin/nginx

nginx      1418  0.0  0.0  46580  1900 ?        S    10:19   0:00 nginx: worker process

root       1420  0.0  0.0 112824   980 pts/0    S+   10:19   0:00 grep --color=auto nginx

[root@static ~]# curl localhost

this is static-server

[root@static ~]# curl 192.168.2.31

this is java web-server

# 使用static主机反向代理dy001

指令格式: location / {

   proxy_pass 协议 域名 端口

        }

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

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

 location / {

            #root   html;

            #index  index.html index.htm;

            proxy_pass  http://192.168.2.31:80;   # 使用static代理dy服务器

        }

# 浏览器测试,访问本机返回代理服务器的页面内容

nginx访客IP黑名单

环境准备

allow-deny(192.168.2.32)

[root@allow-deny ~]# yum -y install gcc gcc-c++ pcre-devel openssl-devel

[root@allow-deny ~]# tar -zxvf nginx-1.26.1.tar.gz

[root@allow-deny ~]#cd nginx-1.26.1/

[root@allow-deny 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@allow-deny nginx-1.26.1]# make && make install

[root@allow-deny nginx-1.26.1]# useradd -s /bin/nologin -M nginx 

# 修改index,html文件

[root@allow-deny nginx-1.26.1]# /usr/local/nginx/sbin/nginx

[root@allow-deny nginx-1.26.1]# echo "you are lucky" > /usr/local/nginx/html/index.html

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

[root@allow-deny nginx-1.26.1]# curl localhost

you are lucky

# 在配置文件中的server模块中设置,设置除了dy001(192.168.2.31)主机可以访问,其他主机都不可访问

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

server {

        listen       80;

        server_name  localhost;

        charset utf-8;

        allow 192.168.2.31;

        deny 192.168.2.0/24;

        deny all;

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

[root@allow-deny nginx-1.26.1]# curl 192.168.2.32

<html>

<head><title>403 Forbidden</title></head>

<body>

<center><h1>403 Forbidden</h1></center>

<hr><center>nginx/1.26.1</center>

</body>

</html>

[root@allow-deny nginx-1.26.1]# curl 192.168.2.31

this is java web-server

[root@allow-deny nginx-1.26.1]# curl 192.168.2.22

this is java web-server

[root@allow-deny nginx]# tree logs/

logs/

├── access.log

├── error.log

└── nginx.pid

0 directories, 3 files

错误日志

1、控制⽇志参数

(1)nginx 访问日志主要控制参数

log_format:⽤来定义记录日志的格式(可以定义多种日志格式,取不同名字即可)

access_log:⽤来指定日志文件的路径及使⽤何种日志格式记录日志

  1. log_format 日志格式变量

$remote_addr:记录访问⽹站的客户端地址。

$remote_user:远程客户端⽤户名。

$time_local:记录访问时间与时区。

$request:⽤户的http请求起始行信息。

$status:http状态码,记录请求返回的状态码,例如:200、301、404等。

$body_bytes_sent:服务器发送给客户端的响应body字节数。

$http_referer:记录此次请求是从哪个连接访问过来的,可以根据该参数进⾏防盗链设置。

$http_user_agent:记录客户端访问信息,例如:浏览器、手机客户端等。

$http_x_forwarded_for:当前端有代理服务器时,设置web节点记录客户端地址的配置,此参数生效的前提是代理服务器也要进行相关的x_forwarded_for设置。

(3)access_log 访问⽇志

[root@allow-deny nginx]# cat logs/access.log

127.0.0.1 - - [30/Jul/2024:11:24:35 +0800] "GET / HTTP/1.1" 200 615 "-" "curl/7.29.0"

127.0.0.1 - - [30/Jul/2024:11:25:29 +0800] "GET / HTTP/1.1" 200 14 "-" "curl/7.29.0"

192.168.2.32 - - [30/Jul/2024:11:30:09 +0800] "GET / HTTP/1.1" 403 153 "-" "curl/7.29.0"

127.0.0.1 - - [30/Jul/2024:11:32:33 +0800] "GET / HTTP/1.1" 403 153 "-" "curl/7.29.0"

(4)error_log 错误⽇志

1)说明

错误日志:error_log 不能改变

日志文件:指定日志的存放目录

错误日志级别:debug、info、notice、warn、error、crit、alert、emerg,级别越⾼记录的信息越少。

生产环境⼀般为 warn、error、crit 这三个级别之一。

不要配置 info 等级较低的级别,会造成磁盘消耗。

2)举例

[root@allow-deny nginx]# cat logs/error.log

2024/07/30 11:25:26 [notice] 4826#0: signal process started

2024/07/30 11:29:08 [notice] 4832#0: signal process started

2024/07/30 11:30:09 [error] 4833#0: *3 access forbidden by rule, client: 192.168.2.32, server: localhost, request: "GET / HTTP/1.1", host: "192.168.2.32"

2024/07/30 11:32:33 [error] 4833#0: *4 access forbidden by rule, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", host: "localhost"

负载均衡

概念及原理

让每台主机获得相应的压力,默认情况下同一个文件只允许1024人访问。负载均衡是将负载分摊到不同的服务单元,既保证服务的可用性,又保证响应足够快。nginx就是其中一个,在linux下有nginx、Ivs、haproxy等服务,可以提供复杂均衡服务,nginx做负载均衡额外基础——反向代理。

负载均衡的作用

  1. 提高系统性能  2、监控服务器的运行状态  3、提供服务一致性  4、摆脱停机时间  

5、管理服务器故障  6、转发功能  7、恢复添加  8、分发流量

解决服务器的高并发压力,提高应用程序的处理性能;提供故障转移,实现高可用;通过添加或减少服务器数量,增强网站的可扩展性;在负载均衡器上进行过滤,可以提高系统的安全性;

负载均衡常用处理方式(负载均衡分为四层负载均衡和七层负载均衡)

1、四层负载均衡是工作在 OSI 七层协议的第四层——传输层,基于IP+PORT的负载均衡,主要工作是转发。

2、它在接收到客户端的流量以后通过修改数据包的地址信息(目标地址和端口和源地址)将流量转发到应用服务器。

3、实现四层负载均衡的方式:

硬件:F5、BIG-IP、Radware等;软件:LVS、Nginx、Haproxy等。

四层和七层负载均衡的区别

1、四层负载均衡数据包是在底层就进行了分发,而七层负载均衡数据包则

在最顶端进行分发,所以四层负载均衡的效率比七层负载均衡的效率要

高;

2、四层负载均衡不识别域名,而七层负载均衡识别域名。

3.、除了四层和七层负载均衡以外其实还有二层、三层负载均衡。二层负载

均衡是在数据链路层基于MAC地址来实现负载均衡,三层是在网络层一

般采用虚拟IP地址的方式实现负载均衡。

4、实际环境采用的方式:四层负载 (LVS) +七层负载 (Nginx)

负载均衡状态

5种策略

环境准备

static-server(192.168.2.22)  dy001-server(192.168.2.31)  dy002-server(192.168.2.32)  dy003-server(192.168.2.33)
四台主机均发布nginx服务

1)轮询——依次将任务部署给不同的主机

简述:把请求平均地转发给各个后端,使它们的负载大致相同;但是,有些请求占用的时间很长,会导致其所在的后端负载较高。

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

[root@dy001 ~]# echo "i am dynamic-server001" > /usr/local/nginx/html/index.html

[root@dy002 ~]# echo "i am dynamic-server002" > /usr/local/nginx/html/index.html

[root@dy003 ~]# echo "i am dynamic-server003" > /usr/local/nginx/html/index.html

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

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

[root@dy001 ~]# /usr/local/nginx/sbin/nginx

[root@dy002 ~]# /usr/local/nginx/sbin/nginx

[root@dy003~]# /usr/local/nginx/sbin/nginx

static-server主机上增加upstream模块

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

upstream server_group_name {

                server 192.168.2.31:80;

                server 192.168.2.32:80;

                server 192.168.2.33:80;

server www.baudu.com;

}

location / {

            #root   html;

            #index  index.html index.htm;

            proxy_pass  http://server_group_name;       

}

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

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

upstream server_group_name {

                server 192.168.2.32:80 down;    # 手动down掉dy002

}

[root@dy002 ~]# /usr/local/nginx/sbin/nginx -s stop  # 此时只轮询dy001和dy003

2)weight 加权——根据权重的大小分配不同的任务,数字越大,权重越重

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

upstream server_group_name {

                server 192.168.2.31:80 weight=4;  # 权重默认为1,谁权重大,谁优先处理请求

                server 192.168.2.32:80 weight=2;

                server 192.168.2.33:80 weight=1;

                server www.baidu.com;

}

[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   # 总是dy001响应

upstream server_group_name {

                ip_hash;

                server 192.168.2.31:80 weight=4;

                server 192.168.2.32:80 weight=2;

                server 192.168.2.33:80 weight=1;

                server www.baidu.com;

}

4)最少连接

简述:即把请求转发给连接数较少的后端服务器,解决轮询导致后端负载较高的问题,可以达到更好的负载均衡效果。

upstream server_group_name {

                least_conn;  # 将请求转发给连接数较少的后端服务器

server 192.168.2.31:80 weight=4;

                server 192.168.2.32:80 weight=2;

                server 192.168.2.33:80 weight=1;

                server www.baidu.com;

}

5)url_hash

简述:按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,要配合缓存命中来使用。同一个资源多次请求,可能会到达不同的服务器上,导致不必要的多次下载,缓存命中率不高,以及一些资源时间的浪费。而使用ur_hash,可以使得同一个url (也就是同一个资源请求)会到达同一台服务器,一旦缓存住了资源,再次收到请求,就可以从缓存中读取。

upstream server_group_name {

                hash $request_uri;

                server 192.168.2.31:80 weight=4;

                server 192.168.2.32:80 weight=2;

                server 192.168.2.33:80 weight=1;

                server www.baidu.com;

}

平滑升级(不停用业务)

概述

  1. 需要有kill命令的支持;kill不仅仅用于杀死进程,还可以向软件进程发送信号
  2. 常用的-9和-15一个是强杀,一个是正常杀

3、指令格式:kill 信号 进程编号

-USR2 平滑启动一个进程,平滑升级

-WINCH 优雅关闭子进程

-QUIT 优雅关闭主进程

步骤

1)不停止原有服务,但是必须使用原生方式启动或者更改nginx脚本(会创建一个新的进程)

2)重新编译nginx新版本

3)使用kill -USR@ 启动新版本

4)把旧的Nginx子进程全部退出

5)优雅的退出Nginx的老进程 系统就只剩下新的nginx了

查看nginx当前的版本

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

nginx version: nginx/1.26.1

[root@static ~]# kill -l

 1) SIGHUP  2) SIGINT  3) SIGQUIT  4) SIGILL  5) SIGTRAP

 6) SIGABRT  7) SIGBUS  8) SIGFPE  9) SIGKILL 10) SIGUSR1

11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM

16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP

21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ

26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR

31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3

38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8

43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13

48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12

53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7

58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2

63) SIGRTMAX-1 64) SIGRTMAX

使用原生方式启动nginx

[root@static ~]# sbin/nginx -c /usr/local/nginx/sbin/nginx  # 重新加载nginx配置文件

[root@static ~]# ps -aux|grep nginx  # 查看进程,分主进程和work进程

root 1277 0.0 0.1 46128 1156 ? Ss 7月29

0:00 nginx: master process /usr/local/nginx/sbin/nginx

nginx 1278 0.0 0.2 46584 2148 ? S 7月29

0:00 nginx: worker process

root 1405 0.0 0.0 112828 988 pts/1 R+ 00:10

0:00 grep --color=auto nginx

平滑升级到1.27版本

# 查看服务是否启动,在服务持续期间对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 ~]# ls

anaconda-ks.cfg initserver.sh nginx-1.26.1 nginx1.26.1.tar.gz nginx-1.27.0 nginx-1.27.0.tar.g

[root@static nginx-1.27.0]# cd nginx-1.27.0/

# 对新版本进行编译安装,安装目录必须和旧版本一致

[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]# ls /usr/local/nginx/sbin/

nginx  nginx.old

[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.0

# 再次查看进程,找到老版本的pid

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

root 1277 1 0 7月29 ? 00:00:00 nginx:

master process /usr/local/nginx/sbin/nginx

nginx 1278 1277 0 7月29 ? 00:00:00 nginx:

worker process

root 4412 1281 0 00:33 pts/1 00:00:00 grep --

color=auto nginx

# 使用kill -USR2 启用新版本的Nginx的软件,格式为:kill -USR2 老版本的pid编号

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

# 使用老的nginx进程创建新的进程,此时会出现两套master进程,这个时候处理客户请求的就是新的nginx服务了

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

root 1277 1 0 7月29 ? 00:00:00 nginx:

master process /usr/local/nginx/sbin/nginx

nginx 1278 1277 0 7月29 ? 00:00:00 nginx:

worker process

root 4413 1277 0 00:34 ? 00:00:00 nginx:

master process /usr/local/nginx/sbin/nginx

nginx 4414 4413 0 00:34 ? 00:00:00 nginx:

worker process

root 4418 1281 0 00:34 pts/1 00:00:00 grep --

color=auto nginx

# 关闭老版本的所有子进程,关闭老版本的主进程

[root@static nginx-1.27.0]# kill -WINCH 1278

[root@static nginx-1.27.0]# kill -QUIT 1277

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

root 4413 1 0 00:34 ? 00:00:00 nginx:

master process /usr/local/nginx/sbin/nginx

nginx 4414 4413 0 00:34 ? 00:00:00 nginx:

worker process

root 4424 1281 0 00:40 pts/1 00:00:00 grep --

color=auto nginx

# 使用curl 查看当前服务器的版本

[root@static nginx-1.27.0]# curl -I localhost

HTTP/1.1 403 Forbidden

Server: nginx/1.27.0

Date: Mon, 29 Jul 2024 16:42:32 GMT

Content-Type: text/html; charset=utf-8

Content-Length: 153

Connection: keep-alive

tomcat安装与配置

概述:tomcat必须在jdk17以上的版本运行,在实际中生产中,不需要在这么高的版本配置tomcat。

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

10运行环境,而tomcat9可以在jdk8环境运行。

[root@dy001 ~]# ls

anaconda-ks.cfg              nginx-1.26.1

index.html                   nginx-1.26.1.tar.gz

jdk-22.0.2                   nginx.sh

jdk-22_linux-x64_bin.tar.gz

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

[root@dy001 ~]#ls /usr/local/jdk22/

[root@dy001 ~]# cd /usr/local/jdk22/

[root@dy001 jdk22]# pwd

/usr/local/jdk22

[root@dy001 jdk22]# sed -n '$p' /etc/profile

unset -f pathmunge

[root@dy001 jdk22]# sed -i '$aexport JAVA_HOME=/usr/local/jdk22/' /etc/profile

[root@dy001 jdk22]# sed -n '$p' /etc/profile

export JAVA_HOME=/usr/local/jdk22/

[root@dy001 jdk22]# source /etc/profile

[root@dy001 jdk22]# $JAVA_HOME

-bash: /usr/local/jdk22/: 是一个目录

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

[root@dy001 jdk22]# sed -n '$p' /etc/profile

PATH=$JAVA_HOME/bin:$PATH

[root@dy001 jdk22]# source /etc/profile

[root@dy001 jdk22]# java -version

  • 9
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值