云原生高级课——高性能web服务器
一、 什么是高性能Web服务器?
简单说,它是一个能够快速、稳定、高效地处理大量并发HTTP/HTTPS请求的软件系统。其高性能主要体现在:
-
高并发:同时处理成千上万甚至百万级的连接。
-
低延迟:单个请求的响应时间极短。
-
高吞吐:单位时间内处理的请求数量或数据量巨大。
-
高可靠:能够7x24小时稳定运行,资源消耗(CPU、内存)可控。
二、 高性能的基石:核心架构模式
这是实现高性能的关键设计,主要分为三类:
1. 多进程模型 (Multi-Process)
-
原理:主进程 (
Master) 监听端口,接受连接,然后fork出子进程 (Worker) 来处理请求。每个进程独立,内存空间隔离。 -
经典模型:Prefork。主进程预先创建好一批空闲子进程,请求到来时直接分配,避免了进程创建的临时开销。
-
优点:稳定性高,一个进程崩溃不影响其他进程;编程简单,可利用多核。
-
缺点:进程较重,创建/销毁开销大;进程间共享数据复杂(需IPC);内存占用高(每个进程独立内存空间)。
-
代表:Apache HTTP Server (mpm_prefork模块) 的默认模式。
2. 多线程模型 (Multi-Threaded)
-
原理:一个主进程下运行多个工作线程来处理请求。所有线程共享进程的内存空间。
-
优点:线程比进程轻量,创建/上下文切换开销小;数据共享容易(通过堆内存)。
-
缺点:编程复杂,需处理锁和线程同步问题,一个线程崩溃(如内存越界)可能导致整个进程崩溃。
-
代表:Apache HTTP Server (mpm_worker/event模块),Microsoft IIS。
3. 事件驱动模型 (Event-Driven)
-
原理:这是现代高性能服务器的核心。一个主线程(或少量线程)通过I/O多路复用(如
epoll/kqueue/IOCP)监听所有连接上的事件(如“可读”、“可写”)。当事件发生时,再调用对应的回调函数进行非阻塞式处理。 -
关键:非阻塞I/O + 事件循环。整个服务器进程不会被慢速的I/O(如读写磁盘、网络)阻塞。
-
优点:极高的并发能力,一个线程即可处理数万连接;资源消耗极低(不需要为每个连接创建线程/进程)。
-
挑战:编程模型复杂(回调地狱),CPU密集型任务会阻塞事件循环。
-
变种:
-
反应器模式:经典的单线程事件循环。
-
多反应器模式:主反应器负责接收连接,然后将连接分发给多个子反应器(运行在不同线程/进程)处理,充分利用多核。Netty的核心设计。
-
-
代表:Nginx,Node.js,Redis。
4. 协程模型 (Coroutine/Fiber)
-
原理:用户态的“轻量级线程”,由程序自身调度,而非操作系统内核。在事件驱动的基础上,提供了“同步编程”的代码风格,避免了回调地狱。
-
工作方式:当一个协程遇到I/O等待时,主动让出CPU,事件循环在I/O就绪后再恢复该协程执行。开发者看起来像是“同步阻塞”的代码,底层却是非阻塞的。
-
优点:兼具事件驱动的高并发和同步编程的简易性。
-
代表:Go (Goroutine是其原生支持),Java (Quasar/Kilim),C++ (libco, Boost.Coroutine),现代Python异步框架。
三、 关键技术点与优化
1. 连接处理优化
-
连接多路复用:HTTP/2, HTTP/3 (QUIC) 在一个TCP连接上支持多路复用多个请求/响应流,极大减少了连接建立开销。
-
TCP优化:调整内核参数(
net.core.somaxconn,net.ipv4.tcp_tw_reuse),使用TCP_DEFER_ACCEPT等选项。
2. 内存与缓存
-
零拷贝:通过
sendfile()等系统调用,数据直接从磁盘文件发送到网卡,避免在内核和用户空间之间复制。 -
内存池:预先申请大块内存并自行管理,避免频繁的
malloc/free系统调用和内存碎片。 -
高效缓存:各级缓存策略(如Nginx的
proxy_cache,fastcgi_cache),将热点数据(静态文件、计算结果)缓存在内存中。
3. CPU与并发
-
CPU亲和性:将工作进程/线程绑定到特定的CPU核心,减少上下文切换和缓存失效。
-
无锁数据结构:在高并发场景下,使用原子操作或无锁队列来减少锁竞争。
4. 高效解析与处理
-
状态机解析:使用高效的状态机来解析HTTP请求行、头部,而非正则表达式。
-
静态文件服务优化:使用
sendfile,mmap内存映射文件。
四、 代表服务器对比
|
特性 |
Nginx |
Apache HTTP Server |
Caddy |
OpenResty / Kong |
|---|---|---|---|---|
|
核心架构 |
事件驱动(多进程+事件循环) |
多进程(Prefork)/多线程/事件混合 |
事件驱动(Go协程) |
基于Nginx,事件驱动 |
|
并发能力 |
极高 |
中等(Prefork)到高(Event) |
高 |
极高 |
|
资源占用 |
低 |
中到高 |
中 |
低 |
|
配置方式 |
声明式配置文件 |
声明式配置文件(.htaccess) |
声明式Caddyfile/JSON |
Nginx配置 + Lua代码 |
|
扩展性 |
模块化(C语言模块) |
模块化极其丰富(.so模块) |
插件化(Go) |
可编程(Lua脚本) |
|
主要场景 |
反向代理、负载均衡、静态文件 |
传统Web应用(如PHP)、模块丰富 |
自动HTTPS、快速部署、微服务网关 |
API网关、Web应用防火墙、高定制逻辑 |
五、 如何构建/选择高性能Web服务?
-
选择合适的核心服务器:
-
网关/反向代理/静态资源:首选 Nginx 或 Caddy(追求自动化)。
-
传统动态应用:可选用Apache的
mpm_event模式。 -
API网关/高定制中间件:OpenResty 或 Kong。
-
-
架构分层:
-
前端用Nginx做负载均衡和静态缓存。
-
中间是应用服务器集群(如Tomcat, Gunicorn, uWSGI)。
-
后端是数据库/缓存集群。
-
-
优化配置:
-
根据CPU核心数设置工作进程/线程数。
-
启用并合理配置缓存。
-
调整操作系统和服务器软件的内核参数。
-
-
异步编程:
-
在后端应用开发中,采用异步非阻塞框架(如Node.js, Tornado, Spring WebFlux, Go net/http)来避免阻塞工作线程,从根本上提升应用层处理能力。
-
-
全链路监控与压测:
-
使用工具(如
wrk,jmeter)进行压力测试,找到瓶颈。 -
通过监控(如Prometheus, APM)分析性能指标。
-
二、Nginx的源码编译
源码安装需要提前准备标准的编译器(GCC)
Nginx安装可以使用yum或源码安装,但是推荐使用源码编译安装
1、yum的版本比较旧
2、编译安装可以更方便自定义相关路径
3、使用源码编译可以自定义相关功能,更方便业务的上的使用
下载软件
[root@Nginx ~]# wget https://nginx.org/download/nginx-1.28.1.tar.gz
解压nginx
[root@Nginx ~]# tar zxf nginx-1.28.1.tar.gz
[root@Nginx ~]# ls
anaconda-ks.cfg nginx-1.28.1 nginx-1.28.1.tar.gz
[root@Nginx ~]# cd nginx-1.28.1/
[root@Nginx nginx-1.28.1]# ls
auto CHANGES.ru conf contrib html man SECURITY.md
CHANGES CODE_OF_CONDUCT.md configure CONTRIBUTING.md LICENSE README.md src
检测环境后编译
# 在做这些之前一定要看httpd(Apache)是否启动,如果启动后续就会报错,并且占用80端口,导致无法运行
[root@Nginx ~]# systemctl status httpd
# 安装依赖
[root@Nginx ~]# dnf install gcc -y
[root@Nginx ~]# dnf install pcre2-devel.x86_64 -y
[root@Nginx ~]# dnf install openssl-devel.x86_64 -y
[root@Nginx ~]# dnf install zlib-devel.x86_64 -y
# 检测环境(可以看error,就可以看出你少哪些依赖)
[root@Nginx nginx-1.28.1]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
[root@Nginx nginx-1.28.1]# make
[root@Nginx nginx-1.28.1]# make install
编译完成之后,会出现有四个主要目录
[root@Nginx nginx-1.28.1]# ls /usr/local/nginx/
conf html logs sbin
# conf:保存nginx所有的配置文件,其中nginx.conf是nginx服务器的最核心最主要的配置文件,其他的.conf则是用来配置nginx相关的功能的,例如fastcgi功能使用的是fastcgi.conf和
fastcgi_params两个文件,配置文件一般都有一个样板配置文件,是以.default为后缀,使用时可将其复制并将default后缀去掉即可。
# html目录中保存了nginx服务器的web文件,但是可以更改为其他目录保存web文件,另外还有一个50x的web文件是默认的错误页面提示页面。
l# ogs:用来保存nginx服务器的访问日志错误日志等日志,logs目录可以放在其他路径,比如/var/logs/nginx里面。
# sbin:保存nginx二进制启动脚本,可以接受不同的参数以实现不同的功能。
二、启动nginx
# 1、直接执行就会找不到命令
[root@Nginx nginx-1.28.1]# cd /usr/local/nginx/sbin/
[root@Nginx sbin]# ./nginx
nginx: [emerg] getpwnam("nginx") failed
# 2、设定环境变量,系统才会识别命令
[root@Nginx sbin]# vim ~/.bash_profile
export PATH=$PATH:/usr/local/nginx/sbin
[root@Nginx sbin]# source ~/.bash_profile
# 3、这个代表nginx正常运行
[root@Nginx sbin]# nginx -V
nginx version: nginx/1.28.1
built by gcc 11.5.0 20240719 (Red Hat 11.5.0-5) (GCC)
built with OpenSSL 3.2.2 4 Jun 2024
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
# 4、直接nginx会报错,因为我们没有这个用户,所以我们需要添加相对应的用户nginx
[root@Nginx sbin]# useradd -s /sbin/nologin -M nginx
[root@Nginx nginx-1.28.1]# nginx
[root@Nginx nginx-1.28.1]# ps aux | grep nginx
root 32911 0.0 0.1 14688 2376 ? Ss 17:15 0:00 nginx: master process nginx
nginx 32912 0.0 0.2 14888 3912 ? S 17:15 0:00 nginx: worker process
root 32938 0.0 0.1 6636 2176 pts/0 S+ 17:15 0:00 grep --color=auto nginx
[root@Nginx sbin]# echo timinglee > /usr/local/nginx/html/index.html
# 5、如果curl失败,就需要查看是否可以ping通,所以查看80端口是否被httpd占用。如果看httpd是active,所以需要关闭之后,在重新查看,而此时关闭了httpd之后那就需要重新从3、检测环境重新安装一次,否则nginx启动不了
[root@Nginx ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; preset: disabled)
Active: active (running) since Thu 2026-01-29 16:32:39 CST; 36min ago
[root@Nginx ~]# systemctl stop httpd.service
# 6、排除一切错误,就可以成功显现了
[root@Nginx nginx-1.28.1]# curl 172.25.254.100
timinglee
编写启动文件
# 1、编写配置文件
[root@Nginx ~]# vim /lib/systemd/system/nginx.service
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@Nginx ~]# systemctl daemon-reload
# 2、验证
[root@Nginx ~]# systemctl status nginx.service
○ nginx.service - The NGINX HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; preset: disabled)
Active: inactive (dead)
# 3、确认80端口没有被占用
[root@Nginx ~]# netstat -tulpn | grep :80
[root@Nginx ~]# systemctl enable --now nginx.service
[root@Nginx ~]# netstat -tulpn | grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 46313/nginx: master
[root@Nginx ~]# reboot
[root@Nginx ~]# systemctl status nginx.service
三、Nginx的平滑升级及回滚
下载高版本的软件
[root@Nginx ~]# wget https://nginx.org/download/nginx-1.29.4.tar.gz
对于新版本的软件进行源码编译并进行平滑升级
# 1、编译nginx隐藏版本
[root@Nginx ~]# tar zxf nginx-1.29.4.tar.gz
[root@Nginx ~]# cd nginx-1.29.4/src/core/
# 2、文件编辑完成后进行源码编译即可
[root@Nginx core]# vim nginx.h
#define nginx_version 1029004
#define NGINX_VERSION ""
#define NGINX_VER "TIMINGLEE/" NGINX_VERSION
[root@Nginx core]# cd ../../
[root@Nginx nginx-1.29.4]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
[root@Nginx nginx-1.29.4]# make
[root@Nginx nginx-1.29.4]# cd objs/
[root@Nginx objs]# ls
autoconf.err nginx ngx_auto_config.h ngx_modules.c src
Makefile nginx.8 ngx_auto_headers.h ngx_modules.o
[root@Nginx objs]# \cp -f /root/nginx-1.29.4/objs/nginx /usr/local/nginx/sbin/nginx
[root@Nginx objs]# ls /usr/local/nginx/logs/
access.log error.log nginx.pid
[root@Nginx objs]# ps aux | grep nginx
root 8830 0.0 0.2 14828 3908 ? Ss 09:47 0:00 nginx: master process ./nginx
nginx 25620 0.0 0.2 15028 4072 ? S 10:33 0:00 nginx: worker process
root 30927 0.0 0.1 6636 2176 pts/0 S+ 10:39 0:00 grep --color=auto nginx
[root@Nginx objs]# kill -USR2 8830
# 3、nginx master进程id
[root@Nginx objs]# ps aux | grep nginx
root 8830 0.0 0.2 14828 3908 ? Ss 09:47 0:00 nginx: master process ./nginx
nginx 25620 0.0 0.2 15028 4072 ? S 10:33 0:00 nginx: worker process
root 31216 0.0 0.4 14716 8192 ? S 10:40 0:00 nginx: master process ./nginx
nginx 31217 0.0 0.2 14916 4156 ? S 10:40 0:00 nginx: worker process
root 31237 0.0 0.1 6636 2176 pts/0 S+ 10:40 0:00 grep --color=auto nginx
[root@Nginx objs]# ls /usr/local/nginx/logs/
access.log error.log nginx.pid nginx.pid.oldbin
# 4、测试效果
[root@Nginx objs]# nginx -V
nginx version: TIMINGLEE/
built by gcc 11.5.0 20240719 (Red Hat 11.5.0-5) (GCC)
built with OpenSSL 3.2.2 4 Jun 2024
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
# 5、回收旧版本子进程
[root@Nginx sbin]# ps aux | grep nginx
root 8830 0.0 0.2 14828 3908 ? Ss 09:47 0:00 nginx: master process ./nginx
nginx 25620 0.0 0.2 15028 4072 ? S 10:33 0:00 nginx: worker process
root 31216 0.0 0.4 14716 8192 ? S 10:40 0:00 nginx: master process ./nginx
nginx 31217 0.0 0.2 14916 4156 ? S 10:40 0:00 nginx: worker process
root 32095 0.0 0.1 6636 2176 pts/0 R+ 10:42 0:00 grep --color=auto nginx
[root@Nginx sbin]# kill -WINCH 8830
[root@Nginx sbin]# ps aux | grep nginx
root 8830 0.0 0.2 14828 3908 ? Ss 09:47 0:00 nginx: master process ./nginx
root 31216 0.0 0.4 14716 8192 ? S 10:40 0:00 nginx: master process ./nginx
nginx 31217 0.0 0.2 14916 4156 ? S 10:40 0:00 nginx: worker process
root 32313 0.0 0.1 6636 2176 pts/0 S+ 10:43 0:00 grep --color=auto nginx
版本回退|版本回滚
[root@Nginx sbin]# cd /usr/local/nginx/sbin/
[root@Nginx sbin]# cp nginx nginx.new -p
[root@Nginx sbin]# \cp nginx.old nginx -pf
[root@Nginx sbin]# ps aux | grep nginx
root 8830 0.0 0.2 14828 3908 ? Ss 09:47 0:00 nginx: master process ./nginx
root 31216 0.0 0.4 14716 8192 ? S 10:40 0:00 nginx: master process ./nginx
nginx 31217 0.0 0.2 14916 4156 ? S 10:40 0:00 nginx: worker process
root 32737 0.0 0.1 6636 2176 pts/0 R+ 10:44 0:00 grep --color=auto nginx
[root@Nginx sbin]# kill -HUP 8830
[root@Nginx sbin]# ps aux | grep nginx
root 8830 0.0 0.2 14828 3908 ? Ss 09:47 0:00 nginx: master process ./nginx
root 31216 0.0 0.4 14716 8192 ? S 10:40 0:00 nginx: master process ./nginx
nginx 31217 0.0 0.2 14916 4156 ? S 10:40 0:00 nginx: worker process
nginx 32937 0.0 0.2 15028 4072 ? S 10:44 0:00 nginx: worker process
root 32951 0.0 0.1 6636 2176 pts/0 S+ 10:44 0:00 grep --color=auto nginx
[root@Nginx sbin]# nginx -V
nginx version: nginx/1.28.1
built by gcc 11.5.0 20240719 (Red Hat 11.5.0-5) (GCC)
built with OpenSSL 3.2.2 4 Jun 2024
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
# 1、回收新版本进程
[root@Nginx sbin]# kill -WINCH 31216
[root@Nginx sbin]# ps aux | grep nginx
root 8830 0.0 0.2 14828 3908 ? Ss 09:47 0:00 nginx: master process ./nginx
root 31216 0.0 0.4 14716 8192 ? S 10:40 0:00 nginx: master process ./nginx
nginx 32937 0.0 0.2 15028 4072 ? S 10:44 0:00 nginx: worker process
root 33506 0.0 0.1 6636 2176 pts/0 S+ 10:46 0:00 grep --color=auto nginx
四、Nginx配置文件的管理及优化参数
[root@Nginx ~]# vim /usr/local/nginx/conf/nginx.conf
user nginx;
[root@Nginx ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@Nginx ~]# nginx -s reload
[root@Nginx ~]# ps aux | grep nginx
root 8830 0.0 0.2 14828 3908 ? Ss 09:47 0:00 nginx: master process ./nginx
root 31216 0.0 0.4 14716 8320 ? S 10:40 0:00 nginx: master process ./nginx
nginx 32937 0.0 0.2 15028 4072 ? S 10:44 0:00 nginx: worker process
nginx 34799 0.0 0.2 15140 4288 ? S 10:50 0:00 nginx: worker process
root 34831 0.0 0.1 6636 2176 pts/0 S+ 10:50 0:00 grep --color=auto nginx
[root@Nginx ~]# vim /usr/local/nginx/conf/nginx.conf
worker_processes 2;
# 1、在vmware中更改硬件cpu核心个数,然后重启
[root@Nginx ~]# vim /usr/local/nginx/conf/nginx.conf
worker_processes auto;
worker_cpu_affinity 0001 0010 0100 1000;
[root@Nginx ~]# nginx -s reload
[root@Nginx ~]# ps aux | grep nginx
root 8830 0.0 0.2 14828 3908 ? Ss 09:47 0:00 nginx: master process ./nginx
root 31216 0.0 0.4 14716 8320 ? S 10:40 0:00 nginx: master process ./nginx
nginx 32937 0.0 0.2 15028 4456 ? S 10:44 0:00 nginx: worker process
nginx 39339 0.0 0.2 14952 4052 ? S 11:02 0:00 nginx: worker process
nginx 39340 0.0 0.2 14952 4308 ? S 11:02 0:00 nginx: worker process
nginx 39341 0.0 0.2 14952 4180 ? S 11:02 0:00 nginx: worker process
nginx 39342 0.0 0.2 14952 4052 ? S 11:02 0:00 nginx: worker process
root 39398 0.0 0.1 6636 2176 pts/1 S+ 11:02 0:00 grep --color=auto nginx
[root@Nginx ~]# ps axo pid,cmd,psr | grep nginx
8830 nginx: master process ./ngi 0
31216 nginx: master process ./ngi 1
32937 nginx: worker process 2
39339 nginx: worker process 0
39340 nginx: worker process 1
39341 nginx: worker process 2
39342 nginx: worker process 3
39448 grep --color=auto nginx 2
[root@Nginx ~]# vim /usr/local/nginx/conf/nginx.conf
events {
worker_connections 1024;
use epoll;
accept_mutex on;
multi_accept on;
}
[root@Nginx ~]# nginx -s reload
[root@Nginx ~]# dnf install httpd-tools -y
[root@Nginx ~]# ab -n 100000 -c5000 http://172.25.254.100/index.html
This is ApacheBench, Version 2.3 <$Revision: 1913912 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 172.25.254.100 (be patient)
socket: Too many open files (24)
[root@Nginx ~]# vim /etc/security/limits.conf
* - nofile 100000
* - noproc 100000
root - nofile 100000
[root@Nginx ~]# sudo -u nginx ulimit -n
100000
[root@Nginx ~]# ulimit -n 10000
# 1、测试:
[root@Nginx ~]# ab -n 100000 -c10000 http://172.25.254.100/index.html
This is ApacheBench, Version 2.3 <$Revision: 1913912 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 172.25.254.100 (be patient)
五、Nginx下构建PC站点
location中的alias
[root@Nginx conf.d]# vim vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
location /passwd {
alias /etc/passwd;
}
location /passwd/ {
alias /mnt/;
}
}
[root@Nginx conf.d]# systemctl restart nginx.service
[root@Nginx conf.d]# echo passwd > /mnt/index.html
# 测试:
[root@Nginx conf.d]# curl lee.timinglee.org/passwd/
passwd
[root@Nginx conf.d]# curl lee.timinglee.org/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
systemd-coredump:x:999:999:systemd Core Dumper:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:998:998:User for polkitd:/:/sbin/nologin
sssd:x:997:996:User for sssd:/:/sbin/nologin
tss:x:59:59:Account used for TPM access:/:/usr/sbin/nologin
clevis:x:996:995:Clevis Decryption Framework unprivileged user:/var/cache/clevis:/usr/sbin/nologin
libstoragemgmt:x:994:994:daemon account for libstoragemgmt:/:/usr/sbin/nologin
setroubleshoot:x:993:993:SELinux troubleshoot server:/var/lib/setroubleshoot:/usr/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/usr/share/empty.sshd:/usr/sbin/nologin
chrony:x:992:992:chrony system user:/var/lib/chrony:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
nginx:x:1000:1000::/home/nginx:/sbin/nologin
location中的root
[root@Nginx ~]# cd /usr/local/nginx/conf/
[root@Nginx conf]# mkdir conf.d
[root@Nginx conf]# vim nginx.conf
include "/usr/local/nginx/conf/conf.d/*.conf";
[root@Nginx conf]# nginx -s reload
[root@Nginx conf]# cd conf.d/
[root@Nginx conf.d]# mkdir -p /webdata/nginx/timinglee.org/lee/html
[root@Nginx conf.d]# echo lee.timinglee.org > /webdata/nginx/timinglee.org/lee/html/index.html
[root@Nginx conf.d]# vim vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
location / {
root /webdata/nginx/timinglee.org/lee/html;
}
}
[root@Nginx conf.d]# systemctl restart nginx.service
# 测试:
[root@Nginx conf.d]# vim /etc/hosts
172.25.254.100 Nginx www.timinglee.org lee.timinglee.org
[root@Nginx conf.d]# curl www.timinglee.org
timinglee
[root@Nginx conf.d]# curl lee.timinglee.org
lee.timinglee.org
# local示例需要访问lee.timinglee.org/lee/目录
[root@Nginx conf.d]# vim vhosts.conf
[root@Nginx conf.d]# systemctl restart nginx.service
[root@Nginx conf.d]# mkdir -p /webdata/nginx/timinglee.org/lee/html/lee
[root@Nginx conf.d]# echo lee > /webdata/nginx/timinglee.org/lee/html/lee/index.html
[root@Nginx conf.d]# curl lee.timinglee.org/lee/
lee
六、KeepAlived长链接优化
设定长链接时间
[root@Nginx ~]# vim /usr/local/nginx/conf/nginx.conf
keepalive_timeout 5;
[root@Nginx ~]# systemctl restart nginx.service
# 测试:
[root@Nginx ~]# dnf install telnet -y
[root@Nginx ~]# telnet www.timinglee.org 80
Trying 172.25.254.100...
Connected to www.timinglee.org.
Escape character is '^]'.
GET / HTTP/1.1 <<<<
Host: www.timinglee.org <<<<
<<<
HTTP/1.1 200 OK
Server: nginx/1.28.1
Date: Sat, 31 Jan 2026 08:27:02 GMT
Content-Type: text/html
Content-Length: 10
Last-Modified: Thu, 29 Jan 2026 09:02:15 GMT
Connection: keep-alive
ETag: "697b2217-a"
Accept-Ranges: bytes
timinglee # 显示的页面出现后根据设定的长链接时间会等待,超过时间后会自动退出
Connection closed by foreign host.
设定长链接次数
[root@Nginx ~]# vim /usr/local/nginx/conf/nginx.conf
keepalive_requests 3;
[root@Nginx ~]# systemctl restart nginx.service
# 1、测试:
[root@Nginx ~]# telnet www.timinglee.org 80
Trying 172.25.254.100...
Connected to www.timinglee.org.
Escape character is '^]'.
GET / HTTP/1.1
Host: www.timinglee.org
HTTP/1.1 200 OK #第一次
Server: nginx/1.28.1
Date: Sat, 31 Jan 2026 08:32:14 GMT
Content-Type: text/html
Content-Length: 10
Last-Modified: Thu, 29 Jan 2026 09:02:15 GMT
Connection: keep-alive
Keep-Alive: timeout=100
ETag: "697b2217-a"
Accept-Ranges: bytes
timinglee
GET / HTTP/1.1
Host: www.timinglee.org
HTTP/1.1 200 OK #第二次
Server: nginx/1.28.1
Date: Sat, 31 Jan 2026 08:32:24 GMT
Content-Type: text/html
Content-Length: 10
Last-Modified: Thu, 29 Jan 2026 09:02:15 GMT
Connection: keep-alive
Keep-Alive: timeout=100
ETag: "697b2217-a"
Accept-Ranges: bytes
timinglee
GET / HTTP/1.1
Host: www.timinglee.org
HTTP/1.1 200 OK #第三次
Server: nginx/1.28.1
Date: Sat, 31 Jan 2026 08:32:35 GMT
Content-Type: text/html
Content-Length: 10
Last-Modified: Thu, 29 Jan 2026 09:02:15 GMT
Connection: close
ETag: "697b2217-a"
Accept-Ranges: bytes
timinglee
Connection closed by foreign host.
七、Location字符匹配详解
语法规则:
location [ = | ~ | ~* | ^~ ] uri { ... }
匹配模式说明:
= 精确匹配:要求请求字符串与uri完全一致(区分大小写),匹配成功立即处理请求并终止后续匹配
^~ 前缀匹配:对uri最左侧部分进行正则表达式匹配(不区分大小写)
~ 正则匹配:使用正则表达式匹配uri(区分大小写)
~* 正则匹配:使用正则表达式匹配uri(不区分大小写)
无符号 普通匹配:匹配以该uri开头的所有请求
\ 转义字符:将正则表达式中的特殊字符(如. * ?等)转义为普通字符
匹配优先级(从高到低):
= 精确匹配
^~ 前缀匹配
~/~* 正则匹配
无符号普通匹配
Location后什么都不带直接指定目录
# 1、先确认是否存在这个域名
[root@Nginx ~]# cd /usr/local/nginx/conf/conf.d
[root@Nginx conf.d]# vim /etc/hosts
172.25.254.100 Nginx www.timinglee.org lee.timinglee.org
[root@Nginx etc]# cd /etc/httpd/conf.d/
[root@Nginx conf.d]# vim vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
location /null {
return 200 "/null-1";
}
}
[root@Nginx conf.d]# systemctl restart nginx.service
# 2、测试:
[root@Nginx conf.d]# curl lee.timinglee.org/null/
/null-1[root@Nginx conf.d]# curl lee.timinglee.org/NULL/
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.28.1</center>
</body>
</html>
[root@Nginx conf.d]# curl lee.timinglee.org/test/null/
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.28.1</center>
</body>
</html>
location 后用 =
[root@Nginx conf.d]# vim vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
location /null {
return 200 "/null-1";
}
location = /null { #精确匹配到此结束
return 200 "null-2";
}
location ~ /null {
return 200 "null-3";
}
}
# 测试:
[root@Nginx conf.d]# curl lee.timinglee.org/null
null-2
location 后用"^~”
[root@Nginx conf.d]# vim vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
location /null {
return 200 "null-1";
}
location = /null {
return 200 "null-2";
}
location ~ /null {
return 200 "null-3";
}
location ^~ /lee {
return 200 "lee";
}
}
[root@Nginx conf.d]# systemctl restart nginx.service
# 测试:
[root@Nginx conf.d]# curl lee.timinglee.org/lee
lee
[root@Nginx conf.d]# curl lee.timinglee.org/test/lee
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.28.1</center>
</body>
</html>
[root@Nginx conf.d]# curl lee.timinglee.org/lee/test
lee
[root@Nginx conf.d]# curl lee.timinglee.org/alee/test
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.28.1</center>
</body>
</html>
[root@Nginx conf.d]# curl lee.timinglee.org/leeabc/test
lee
location 后用"~”
[root@Nginx conf.d]# vim vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
location /null {
return 200 "null-1";
}
location = /null {
return 200 "null-2";
}
location ~ /null {
return 200 "null-3";
}
location ^~ /lee {
return 200 "lee";
}
location ~ /timing {
return 200 "timing";
}
}
[root@Nginx conf.d]# systemctl restart nginx.service
# 测试:
[root@Nginx conf.d]# curl lee.timinglee.org/timing
timing
[root@Nginx conf.d]# curl lee.timinglee.org/timinga
timing
[root@Nginx conf.d]# curl lee.timinglee.org/a/timinga
timing
[root@Nginx conf.d]# curl lee.timinglee.org/a/atiminga
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.28.1</center>
</body>
</html>
[root@Nginx conf.d]# curl lee.timinglee.org/a/Timinga
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.28.1</center>
</body>
</html>
[root@Nginx conf.d]# curl lee.timinglee.org/a/timinga/a/
timing
location 后用"~*”
[root@Nginx conf.d]# vim vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
location /null {
return 200 "null-1";
}
location = /null {
return 200 "null-2";
}
location ~ /null {
return 200 "null-3";
}
location ^~ /lee {
return 200 "lee";
}
location ~ /timing {
return 200 "timing";
}
location ~* /timinglee {
return 200 "timinglee";
}
}
[root@Nginx conf.d]# systemctl restart nginx.service
# 测试:
[root@Nginx conf.d]# curl lee.timinglee.org/Timinglee
timinglee
[root@Nginx conf.d]# curl lee.timinglee.org/timinglee
timinglee
[root@Nginx conf.d]# curl lee.timinglee.org/timinglee/a
timinglee
[root@Nginx conf.d]# curl lee.timinglee.org/a/timinglee/a
timinglee
[root@Nginx conf.d]# curl lee.timinglee.org/a/atiminglee/a
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.28.1</center>
</body>
</html>
[root@Nginx conf.d]# curl lee.timinglee.org/a/timingleea/a
timing[root@Nginx conf.d]# curl lee.timinglee.org/a/Timingleea/a
timinglee
location 后用“\”
[root@Nginx conf.d]# vim vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
location /null {
return 200 "null-1";
}
location = /null {
return 200 "null-2";
}
location ~ /null {
return 200 "null-3";
}
location ^~ /lee {
return 200 "lee";
}
location ~ /timing/ {
return 200 "timing";
}
location ~* /timinglee {
return 200 "timinglee";
}
location ~* \.(img|php|jsp)$ {
return 200 "app";
}
}
[root@Nginx conf.d]# systemctl restart nginx.service
[root@Nginx conf.d]# curl lee.timinglee.org/test.php
app
[root@Nginx conf.d]# curl lee.timinglee.org/test.jsp
app
八、服务访问的用户认证
# 1、做好准备
[root@Nginx ~]# mkdir -p /usr/local/nginx/html/admin
[root@Nginx ~]# echo admin > /usr/local/nginx/html/admin/index.html
[root@Nginx ~]# htpasswd -cmb /usr/local/nginx/conf/.htpasswd admin lee
Adding password for user admin
[root@Nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
location /admin {
root /usr/local/nginx/html;
auth_basic "login passwd";
auth_basic_user_file "/usr/local/nginx/conf/.htpasswd";
}
}
[root@Nginx ~]# systemctl restart nginx.service
# 2、测试:
[root@Nginx ~]# curl lee.timinglee.org/admin/
<html>
<head><title>401 Authorization Required</title></head>
<body>
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.28.1</center>
</body>
</html>
[root@Nginx ~]# curl -uadmin:lee http://lee.timinglee.org/admin/
admin
九、自定义错误页面、日志
自定义错误界面
[root@Nginx ~]# mkdir /usr/local/nginx/errorpage
[root@Nginx ~]# echo "你的页面丢失" > /usr/local/nginx/errorpage/errormessage
[root@Nginx ~]# cat /usr/local/nginx/errorpage/errormessage
你的页面丢失
[root@Nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
error_page 404 405 503 502 /error;
location /lee {
root /usr/local/nginx/html;
}
location /error {
alias /usr/local/nginx/errorpage/errormessage;
}
}
[root@Nginx ~]# systemctl restart nginx.service
[root@Nginx ~]# curl lee.timinglee.org/lee/
你的页面丢失
自定义错误日志
[root@Nginx ~]# mkdir -p /usr/local/nginx/logs/timinglee.org/
[root@Nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
error_page 404 405 503 502 /error;
error_log logs/timinglee.org/lee.error error;
location /lee {
root /usr/local/nginx/html;
}
location /error {
alias /usr/local/nginx/errorpage/errormessage;
}
}
[root@Nginx ~]# systemctl restart nginx.service
# 测试:
[root@Nginx ~]# cd /usr/local/nginx/logs/timinglee.org/
[root@Nginx timinglee.org]# ls
lee.error
[root@Nginx timinglee.org]# cat lee.error
[root@Nginx timinglee.org]# curl lee.timinglee.org/lee/
你的页面丢失了
[root@Nginx timinglee.org]# cat lee.error
2026/02/01 13:41:13 [error] 32779#0: *1 "/usr/local/nginx/html/lee/index.html" is not found (2: No such file or directory), client: 172.25.254.100, server: lee.timinglee.org, request: "GET /lee/ HTTP/1.1", host: "lee.timinglee.org"
十、Nginx中建立下载服务器
[root@Nginx ~]# mkdir -p /usr/local/nginx/download
[root@Nginx ~]# cp /etc/passwd /usr/local/nginx/download/
[root@Nginx ~]# dd if=/dev/zero of=/usr/local/nginx/download/bigfile bs=1M count=100
记录了100+0 的读入
记录了100+0 的写出
104857600字节(105 MB,100 MiB)已复制,0.0666022 s,1.6 GB/s
[root@Nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
error_page 404 405 503 502 /error;
error_log logs/timinglee.org/lee.error error;
location /lee {
root /usr/local/nginx/html;
}
location /error {
alias /usr/local/nginx/errorpage/errormessage;
}
location /download {
root /usr/local/nginx;
}
}
[root@Nginx ~]# systemctl restart nginx.service
启用列表功能
[root@Nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
error_page 404 405 503 502 /error;
error_log logs/timinglee.org/lee.error error;
location /lee {
root /usr/local/nginx/html;
}
location /error {
alias /usr/local/nginx/errorpage/errormessage;
}
location /download {
root /usr/local/nginx;
autoindex on;
}
}
[root@Nginx ~]# nginx -s reload
下载控诉
[root@Nginx ~]# wget http://lee.timinglee.org/download/bigfile
--2026-02-01 13:55:04-- http://lee.timinglee.org/download/bigfile
正在解析主机 lee.timinglee.org (lee.timinglee.org)... 172.25.254.100
正在连接 lee.timinglee.org (lee.timinglee.org)|172.25.254.100|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:104857600 (100M) [application/octet-stream]
正在保存至: “bigfile”
bigfile 100%[=====================================>] 100.00M 667MB/s 用时 0.2s
2026-02-01 13:55:04 (667 MB/s) - 已保存 “bigfile” [104857600/104857600])
[root@Nginx ~]# rm -fr bigfile
[root@Nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
error_page 404 405 503 502 /error;
error_log logs/timinglee.org/lee.error error;
location /lee {
root /usr/local/nginx/html;
}
location /error {
alias /usr/local/nginx/errorpage/errormessage;
}
location /download {
root /usr/local/nginx;
autoindex on;
limit_rate 1024k;
}
}
[root@Nginx ~]# systemctl restart nginx.service
[root@Nginx ~]# wget http://lee.timinglee.org/download/bigfile
--2026-02-01 13:55:41-- http://lee.timinglee.org/download/bigfile
正在解析主机 lee.timinglee.org (lee.timinglee.org)... 172.25.254.100
正在连接 lee.timinglee.org (lee.timinglee.org)|172.25.254.100|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:104857600 (100M) [application/octet-stream]
正在保存至: “bigfile”
显示文件大小优化
[root@Nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
error_page 404 405 503 502 /error;
error_log logs/timinglee.org/lee.error error;
location /lee {
root /usr/local/nginx/html;
}
location /error {
alias /usr/local/nginx/errorpage/errormessage;
}
location /download {
root /usr/local/nginx;
autoindex on;
limit_rate 1024k;
autoindex_exact_size off;
}
}
[root@Nginx ~]# systemctl restart nginx.service
# 1、效果
[root@Nginx ~]# curl lee.timinglee.org/download
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.28.1</center>
</body>
</html>
[root@Nginx ~]# curl lee.timinglee.org/download/
<html>
<head><title>Index of /download/</title></head>
<body>
<h1>Index of /download/</h1><hr><pre><a href="../">../</a>
<a href="bigfile">bigfile</a> 01-Feb-2026 05:42 100M
<a href="passwd">passwd</a> 01-Feb-2026 05:42 1347
</pre><hr></body>
</html>
时间显示调整
[root@Nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
error_page 404 405 503 502 /error;
error_log logs/timinglee.org/lee.error error;
location /lee {
root /usr/local/nginx/html;
}
location /error {
alias /usr/local/nginx/errorpage/errormessage;
}
location /download {
root /usr/local/nginx;
autoindex on;
limit_rate 1024k;
autoindex_exact_size off;
autoindex_localtime on;
}
}
[root@Nginx ~]# systemctl restart nginx.service

设定页面风格
[root@Nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
error_page 404 405 503 502 /error;
error_log logs/timinglee.org/lee.error error;
location /lee {
root /usr/local/nginx/html;
}
location /error {
alias /usr/local/nginx/errorpage/errormessage;
}
location /download {
root /usr/local/nginx;
autoindex on;
limit_rate 1024k;
autoindex_exact_size off;
autoindex_localtime on;
autoindex_format html | xml | json | jsonp;
}
}
[root@Nginx ~]# systemctl restart nginx.service

十一、Nginx的文件检测
# 1、准备:
[root@Nginx ~]# echo default > /usr/local/nginx/errorpage/default.html
[root@Nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
listen 80;
server_name lee.timinglee.org;
error_page 404 405 503 502 /error;
error_log logs/timinglee.org/lee.error error;
root /usr/local/nginx/errorpage; # 添加根目录
try_files $uri $uri.html $uri/index.html /default.html; # 添加默认
[root@Nginx ~]# systemctl restart nginx.service
[root@Nginx ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
# 2、测试:
[root@Nginx ~]# curl lee.timinglee.org/test/
default
[root@Nginx ~]# curl -v lee.timinglee.org/aa/
* Trying 172.25.254.100:80...
* Connected to lee.timinglee.org (172.25.254.100) port 80 (#0)
> GET /aa/ HTTP/1.1
> Host: lee.timinglee.org
> User-Agent: curl/7.76.1
> Accept: */
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Server: nginx/1.28.1
< Date: Sun, 01 Feb 2026 06:28:03 GMT
< Content-Type: text/html
< Content-Length: 8
< Last-Modified: Sun, 01 Feb 2026 06:24:46 GMT
< Connection: keep-alive
< ETag: "697ef1ae-8"
< Accept-Ranges: bytes
<
default
* Connection #0 to host lee.timinglee.org left intact
十二、Nginx的状态页
[root@Nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
location /nginx_status {
stub_status;
auth_basic "auth login";
auth_basic_user_file /usr/local/nginx/conf/.htpasswd;
allow 172.25.254.0/24;
deny all;
}
}
[root@Nginx ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@Nginx ~]# systemctl restart nginx.service

十三、Nginx的压缩功能
[root@Nginx ~]# mkdir /usr/local/nginx/timinglee.org/lee/html -p
[root@Nginx ~]# echo hello lee > /usr/local/nginx/timinglee.org/lee/html/index.html
[root@Nginx ~]# cd /usr/local/nginx/timinglee.org/lee/html/
[root@Nginx html]# ls
bigfile index.html
[root@Nginx html]# file bigfile
bigfile: data
[root@Nginx html]# cp /usr/local/nginx/logs/access.log /usr/local/nginx/timinglee.org/lee/html/bigfile.txt
# 确认为txt而不是data
[root@Nginx html]# file /usr/local/nginx/timinglee.org/lee/html/bigfile.txt
/usr/local/nginx/timinglee.org/lee/html/bigfile.txt: ASCII text
[root@Nginx ~]# vim /usr/local/nginx/conf/nginx.conf
gzip on;
gzip_comp_level 4;
gzip_disable "MSIE [1-6]\.";
gzip_min_length 1024k;
gzip_types text/plain application/javascript application/x-javascript text/css
application/xml text/javascript application/x-httpd-php image/gif image/png;
gzip_vary on;
gzip_static on;
[root@Nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
root /usr/local/nginx/timinglee.org/lee/html;
location /nginx_status {
stub_status;
auth_basic "auth login";
auth_basic_user_file /usr/local/nginx/conf/.htpasswd;
allow 172.25.254.0/24;
deny all;
}
}
[root@Nginx ~]# systemctl restart nginx.service
# 测试:
[root@Nginx html]# curl -I lee.timinglee.org
HTTP/1.1 200 OK
Server: nginx/1.28.1
Date: Sun, 01 Feb 2026 07:24:42 GMT
Content-Type: text/html
Content-Length: 10
Last-Modified: Sun, 01 Feb 2026 07:20:04 GMT
Connection: keep-alive
ETag: "697efea4-a"
Accept-Ranges: bytes
[root@Nginx html]# curl --head --compressed lee.timinglee.org/bigfile.txt
HTTP/1.1 200 OK
Server: nginx/1.28.1
Date: Sun, 01 Feb 2026 07:32:10 GMT
Content-Type: text/plain
Last-Modified: Sun, 01 Feb 2026 07:29:53 GMT
Connection: keep-alive
Keep-Alive: timeout=100
Vary: Accept-Encoding
ETag: W/"697f00f1-2ca84bd"
Content-Encoding: gzip
Nginx变量
升级Nginx支持echo
[root@Nginx ~]# systemctl stop nginx.service
[root@Nginx ~]# ps aux | grep nginx
root 8223 0.0 0.1 6636 2176 pts/0 S+ 16:08 0:00 grep --color=auto nginx
# 1、把文件传进去之后ls查看
[root@Nginx ~]# ls
anaconda-ks.cfg echo-nginx-module-0.64.tar.gz nginx-1.28.1.tar.gz nginx-1.29.4.tar.gz
bigfile nginx-1.28.1 nginx-1.29.4
[root@Nginx ~]# tar zxf echo-nginx-module-0.64.tar.gz
[root@Nginx ~]# cd echo-nginx-module-0.64/
[root@Nginx echo-nginx-module-0.64]# ls
config LICENSE README.markdown src t util valgrind.suppress
[root@Nginx ~]# cd nginx-1.28.1/
[root@Nginx nginx-1.28.1]# make clean
rm -rf Makefile objs
# 2、重新./configure
[root@Nginx nginx-1.28.1]# ./config --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module --add-module=/root/echo-nginx-module-0.64
[root@Nginx nginx-1.28.1]# make
[root@Nginx nginx-1.28.1]# rm -rf /usr/local/nginx/sbin/nginx
[root@Nginx nginx-1.28.1]# cp objs/nginx /usr/local/nginx/sbin/ -p
# 3、测试:
[root@Nginx nginx-1.28.1]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
root /usr/local/nginx/timinglee.org/lee/html;
location /vars {
default_type text/html;
echo $remote_addr;
}
}
[root@Nginx nginx-1.28.1]# systemctl restart nginx.service
[root@Nginx nginx-1.28.1]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
理解内建变量
[root@Nginx nginx-1.28.1]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
root /usr/local/nginx/timinglee.org/lee/html;
location /vars {
default_type text/html;
echo $remote_addr;
}
}
# 测试:
[root@Nginx nginx-1.28.1]# curl lee.timinglee.org/vars/
172.25.254.100
[2026-02-02 18:02.16] ~
[pxy2086] ⮞ curl lee.timinglee.org/vars/
172.25.254.1
# 存放了客户端的地址,注意是客户端的公网IP ,$args
[root@Nginx nginx-1.28.1]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
root /usr/local/nginx/timinglee.org/lee/html;
location /vars {
default_type text/html;
echo $remote_addr;
echo $args;
}
[root@Nginx nginx-1.28.1]# systemctl restart nginx.service
# 测试:
[root@Nginx nginx-1.28.1]# curl "http://lee.timinglee.org/vars?key=lee&id=11"
172.25.254.100
key=lee&id=11
# 更多变量
[root@Nginx nginx-1.28.1]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
root /usr/local/nginx/timinglee.org/lee/html;
location /vars {
default_type text/html;
echo $remote_addr;
echo $args;
echo $is_args; # 如果有参数为? 否则为空
echo $document_root;
echo $document_uri; # 保存了当前请求中不包含参数的URI,注意是不包含请求的指令
echo $host;
echo $remote_port;
echo $remote_user;
echo $request_method;
echo $request_filename;
echo $request_uri;
echo $scheme;
echo $server_protocol;
echo $server_addr;
echo $server_name;
echo $server_port;
echo $http_user_agent;
echo $cookie_key2;
echo $http_user_agent;
echo $sent_http_content_type;
}
}
[root@Nginx nginx-1.28.1]# systemctl restart nginx.service
# 测试:
[root@Nginx nginx-1.28.1]# curl -b "key1=hello,key2=timinglee" -A "haha" -ulee:lee "http://lee.timinglee.org/vars?key=lee&id=11"
172.25.254.100
key=lee&id=11
?
/usr/local/nginx/timinglee.org/lee/html
/vars
lee.timinglee.org
38396
lee
GET
/usr/local/nginx/timinglee.org/lee/html/vars
/vars?key=lee&id=11
http
HTTP/1.1
172.25.254.100
lee.timinglee.org
80
haha
timinglee
haha
text/html
自定义变量
[root@Nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
root /usr/local/nginx/timinglee.org/lee/html;
location /vars {
default_type text/html;
echo $remote_addr;
echo $args;
echo $is_args;
echo $document_root;
echo $document_uri;
echo $host;
echo $remote_port;
echo $remote_user;
echo $request_method;
echo $request_filename;
echo $request_uri;
echo $scheme;
echo $server_protocol;
echo $server_addr;
echo $server_name;
echo $server_port;
echo $http_user_agent;
echo $cookie_key2;
echo $http_user_agent;
echo $sent_http_content_type;
set $test lee; #手动设定变量值
echo $test;
set $web_port $server_port; #变量个传递
echo $web_port;
}
}
[root@Nginx ~]# systemctl restart nginx.service
[root@Nginx ~]# curl lee.timinglee.org/vars/
172.25.254.100
/usr/local/nginx/timinglee.org/lee/html
/vars/
lee.timinglee.org
37106
GET
/usr/local/nginx/timinglee.org/lee/html/vars/
/vars/
http
HTTP/1.1
172.25.254.100
lee.timinglee.org
80
curl/7.76.1
curl/7.76.1
text/html
lee
80
十四、网页重写
网页重写中的指令
1.if
# 1、if
[root@Nginx ~]# mkdir /webdir/timinglee.org/lee/html -p
[root@Nginx ~]# echo lee page > /webdir/timinglee.org/lee/html/index.html
[root@Nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
root /webdir/timinglee.org/lee/html;
location /vars {
echo $remote_user;
echo $request_method;
echo $request_filename;
echo $request_uri;
echo $scheme;
}
location / {
if ( $http_user_agent ~* firefox ) {
return 200 "test if messages";
}
}
}
[root@Nginx ~]# systemctl restart nginx.service
[root@Nginx ~]# curl lee.timinglee.org
lee page
[root@Nginx ~]# curl -A "firefox" lee.timinglee.org
test if messages
2.set
# 2、set
[root@Nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
root /webdir/timinglee.org/lee/html;
location /vars {
echo $remote_user;
echo $request_method;
echo $request_filename;
echo $request_uri;
echo $scheme;
}
location / {
set $testname timinglee;
echo $testname;
}
}
[root@Nginx ~]# systemctl restart nginx.service
[root@Nginx ~]# curl lee.timinglee.org
timinglee
3.return
# 3、return
[root@Nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
root /webdir/timinglee.org/lee/html;
location /vars {
echo $remote_user;
echo $request_method;
echo $request_filename;
echo $request_uri;
echo $scheme;
}
location / {
return 200 "hello world";
}
}
[root@Nginx ~]# systemctl restart nginx.service
[root@Nginx ~]# curl lee.timinglee.org
hello world
4.break
# 4、break
[root@Nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
root /webdir/timinglee.org/lee/html;
location /vars {
echo $remote_user;
echo $request_method;
echo $request_filename;
echo $request_uri;
echo $scheme;
}
location / {
set $test1 lee1;
set $test2 lee2;
if ($http_user_agent = firefox){
break;
}
set $test3 lee3;
echo $test1 $test2 $test3;
}
}
[root@Nginx ~]# systemctl restart nginx.service
[root@Nginx ~]# curl lee.timinglee.org
lee1 lee2 lee3
[root@Nginx ~]# curl -A "firefox" lee.timinglee.org
lee1 lee2
flag
redirect
# 1、redirect
[root@Nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
root /webdir/timinglee.org/lee/html;
location /vars {
echo $remote_user;
echo $request_method;
echo $request_filename;
echo $request_uri;
echo $scheme;
}
location / {
rewrite / http://www.baidu.com redirect;
}
}
[root@Nginx ~]# systemctl restart nginx.service
[root@Nginx ~]# curl -I lee.timinglee.org
HTTP/1.1 302 Moved Temporarily #定向方式返回值
Server: nginx/1.28.1
Date: Tue, 03 Feb 2026 03:25:37 GMT
Content-Type: text/html
Content-Length: 145
Connection: keep-alive
Location: http://www.baidu.com #定向效果
permanent
# 2、permanent
[root@Nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
root /webdir/timinglee.org/lee/html;
location /vars {
echo $remote_user;
echo $request_method;
echo $request_filename;
echo $request_uri;
echo $scheme;
}
location / {
rewrite / http://www.baidu.com permanent;
}
}
[root@Nginx ~]# systemctl restart nginx.service
[root@Nginx ~]# curl -I lee.timinglee.org
HTTP/1.1 301 Moved Permanently
Server: nginx/1.28.1
Date: Tue, 03 Feb 2026 03:26:38 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
Location: http://www.baidu.com
break和last
# 3、break和last
[root@Nginx ~]# mkdir /webdir/timinglee.org/lee/html/{break,last,test1,test2}
[root@Nginx ~]# echo break > /webdir/timinglee.org/lee/html/break/index.html
[root@Nginx ~]# echo last > /webdir/timinglee.org/lee/html/last/index.html
[root@Nginx ~]# echo test1 > /webdir/timinglee.org/lee/html/test1/index.html
[root@Nginx ~]# echo test2 > /webdir/timinglee.org/lee/html/test2/index.html
# break
[root@Nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
root /webdir/timinglee.org/lee/html;
location /vars {
echo $remote_user;
echo $request_method;
echo $request_filename;
echo $request_uri;
echo $scheme;
}
location /break {
rewrite /break/(.*) /test1/$1 break;
rewrite /test1 /test2;
}
location /test1 {
return 200 "test1 end page";
}
location /test2 {
return 200 "TEST2 END PAGE";
}
}
[root@Nginx ~]# systemctl restart nginx.service
[root@Nginx ~]# curl -L lee.timinglee.org/break/index.html
test1
# last
[root@Nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
root /webdir/timinglee.org/lee/html;
location /vars {
echo $remote_user;
echo $request_method;
echo $request_filename;
echo $request_uri;
echo $scheme;
}
location /break {
rewrite /break/(.*) /test1/$1 last;
rewrite /test1 /test2;
}
location /test1 {
return 200 "test1 end page";
}
location /test2 {
return 200 "TEST2 END PAGE";
}
}
[root@Nginx ~]# systemctl restart nginx.service
[root@Nginx ~]# curl -L lee.timinglee.org/break/index.html
test1 end page
十五、Nginx利用网页重写实现全站加密
1、制作key
[root@Nginx ~]# mkdir -p /usr/local/nginx/certs
[root@Nginx ~]# openssl req -newkey rsa:2048 -nodes -sha256 -keyout /usr/local/nginx/certs/timinglee.org.key -x509 -days 365 -out /usr/local/nginx/certs/timinglee.org.crt
2、编辑加密配置文件
[root@Nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
listen 443 ssl;
ssl_certificate /usr/local/nginx/certs/timinglee.org.crt;
ssl_certificate_key /usr/local/nginx/certs/timinglee.org.key;
ssl_session_cache shared:sslcache:20m;
ssl_session_timeout 10m;
server_name lee.timinglee.org;
root /webdir/timinglee.org/lee/html;
location / {
if ($scheme = http ){
rewrite /(.*) https://$host/$1 redirect;
}
}
}
[root@Nginx ~]# systemctl restart nginx.service
[root@Nginx ~]# curl -I http://lee.timinglee.org/test1/
HTTP/1.1 302 Moved Temporarily
Server: nginx/1.28.1
Date: Tue, 02 Feb 2026 13:25:28 GMT
Content-Type: text/html
Content-Length: 145
Connection: keep-alive
Location: https://lee.timinglee.org/test1/
十六、防盗链
[root@Nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
root /webdir/timinglee.org/lee/html;
location / {
valid_referers none blocked server_names *.timinglee.org ~/.baidu/.;
if ($invalid_referer){
return 404;
}
}
location /img {
valid_referers none blocked server_names *.timinglee.org ~/.baidu/.;
if ($invalid_referer){
rewrite ^/ http://lee.timinglee.org/daolian/daolian.png;
}
}
}
[root@Nginx ~]# systemctl restart nginx.service
# 测试:(开启另外的服务器)
[root@localhost ~]# vim /var/www/html/index.html
<html>
<head>
<meta http-equiv=Content-Type content="text/html;charset=utf-8">
<title>盗链</title>
</head>
<body>
<img src="http://lee.timinglee.org/img/lee.png" >
<h1 style="color:red">欢迎大家</h1>
<p><a href=http://lee.timinglee.org>狂点老李</a>出门见喜</p>
</body>
</html>
[root@localhost ~]# systemctl restart httpd
#在浏览器中访问看效果
十七、Nginx反向代理
1、简单的代理方法
[root@RS2 ~]# mkdir /var/www/html/web
[root@RS2 ~]# echo 172.25.254.20 web > /var/www/html/web/index.html
[root@Nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
location / {
proxy_pass http://172.25.254.10:80;
}
location /web {
proxy_pass http://172.25.254.20:80;
}
}
[root@Nginx ~]# systemctl restart nginx.service
# 测试:
[root@Nginx ~]# curl 172.25.254.20/web/
172.25.254.20 web
[root@Nginx ~]# curl 172.25.254.10
172.25.254.10
2、proxy_hide_header filed
[2026-02-02 13:55.25] ~
[pxy2086] ⮞ curl -v lee.timinglee.org
* Host lee.timinglee.org:80 was resolved.
* IPv6: (none)
* IPv4: 172.25.254.100
* Trying 172.25.254.100:80...
* Connected to lee.timinglee.org (172.25.254.100) port 80
* using HTTP/1.x
> GET / HTTP/1.1
> Host: lee.timinglee.org
> User-Agent: curl/8.12.1
> Accept: */*
>
* Request completely sent off
< HTTP/1.1 200 OK
< Server: nginx/1.28.1
< Date: Tue, 03 Feb 2026 07:55:27 GMT
< Content-Type: text/html; charset=UTF-8
< Content-Length: 14
< Connection: keep-alive
< Last-Modified: Tue, 03 Feb 2026 07:48:35 GMT
< ETag: "e-649e6aab07d52" # 可以看到ETAG信息
< Accept-Ranges: bytes
<
172.25.254.10
* Connection #0 to host lee.timinglee.org left intact
[root@Nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
location / {
proxy_pass http://172.25.254.10:80;
proxy_hide_header ETag;
}
location /web {
proxy_pass http://172.25.254.20:80;
}
}
[root@Nginx ~]# systemctl restart nginx.service
# 测试:
[2026-02-02 13:55.27] ~
[pxy2086] ⮞ curl -v lee.timinglee.org
* Host lee.timinglee.org:80 was resolved.
* IPv6: (none)
* IPv4: 172.25.254.100
* Trying 172.25.254.100:80...
* Connected to lee.timinglee.org (172.25.254.100) port 80
* using HTTP/1.x
> GET / HTTP/1.1
> Host: lee.timinglee.org
> User-Agent: curl/8.12.1
> Accept: */*
>
* Request completely sent off
< HTTP/1.1 200 OK
< Server: nginx/1.28.1
< Date: Tue, 03 Feb 2026 07:56:49 GMT
< Content-Type: text/html; charset=UTF-8
< Content-Length: 14
< Connection: keep-alive
< Last-Modified: Tue, 03 Feb 2026 07:48:35 GMT
< Accept-Ranges: bytes
<
172.25.254.10
* Connection #0 to host lee.timinglee.org left intact
3、proxy_pass_header
[2026-02-02 13:55.27] ~
[pxy2086] ⮞ curl -v lee.timinglee.org
* Host lee.timinglee.org:80 was resolved.
* IPv6: (none)
* IPv4: 172.25.254.100
* Trying 172.25.254.100:80...
* Connected to lee.timinglee.org (172.25.254.100) port 80
* using HTTP/1.x
> GET / HTTP/1.1
> Host: lee.timinglee.org
> User-Agent: curl/8.12.1
> Accept: */*
>
* Request completely sent off
< HTTP/1.1 200 OK
< Server: nginx/1.28.1 # 默认访问不透传server信息
< Date: Tue, 03 Feb 2026 07:56:49 GMT
< Content-Type: text/html; charset=UTF-8
< Content-Length: 14
< Connection: keep-alive
< Last-Modified: Tue, 03 Feb 2026 07:48:35 GMT
< Accept-Ranges: bytes
<
172.25.254.10
* Connection #0 to host lee.timinglee.org left intact
[root@Nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
location / {
proxy_pass http://172.25.254.10:80;
proxy_pass_header Server;
}
location /web {
proxy_pass http://172.25.254.20:80;
}
}
[root@Nginx ~]# systemctl restart nginx.service
# 测试:
[2026-02-02 13:57.53] ~
[pxy2086] ⮞ curl -v lee.timinglee.org
* Host lee.timinglee.org:80 was resolved.
* IPv6: (none)
* IPv4: 172.25.254.100
* Trying 172.25.254.100:80...
* Connected to lee.timinglee.org (172.25.254.100) port 80
* using HTTP/1.x
> GET / HTTP/1.1
> Host: lee.timinglee.org
> User-Agent: curl/8.12.1
> Accept: */*
>
* Request completely sent off
< HTTP/1.1 200 OK
< Date: Tue, 03 Feb 2026 07:58:40 GMT
< Content-Type: text/html; charset=UTF-8
< Content-Length: 14
< Connection: keep-alive
< Server: Apache/2.4.62 (Red Hat Enterprise Linux)
< Last-Modified: Tue, 03 Feb 2026 07:48:35 GMT
< ETag: "e-649e6aab07d52"
< Accept-Ranges: bytes
<
172.25.254.10
* Connection #0 to host lee.timinglee.org left intact
透传信息
[root@RS1 ~]# vim /etc/httpd/conf/httpd.conf
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" \"%{X-Forwarded-For}i\"" combined
[root@RS1 ~]# systemctl restart httpd
[root@Nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
location / {
proxy_pass http://172.25.254.10:80;
proxy_set_header X-Forwarded-For $remote_addr;
}
location /web {
proxy_pass http://172.25.254.20:80;
}
}
[root@Nginx ~]# systemctl restart nginx.service
# 测试:
[2026-02-02 13:58.40] ~
[pxy2086] ⮞ curl lee.timinglee.org
172.25.254.10
[root@RS1 ~]# cat /etc/httpd/logs/access_log
172.25.254.100 - - [03/Feb/2026:16:04:46 +0800] "GET / HTTP/1.0" 200 14 "-" "curl/8.12.1" "172.25.254.1"
十八、利用反向代理实现动静分离
试验主机环境
# 在10中
[root@RS1 ~]# dnf install php -y
[root@RS1 ~]# systemctl restart httpd
[root@RS1 ~]# vim /var/www/html/index.php
<?php
echo "<h2>172.25.254.10</h2>";
phpinfo();
?>
动静分离的实现
[root@Nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
location / {
proxy_pass http://172.25.254.20:80;
}
location ~* \.(php|js)$ {
proxy_pass http://172.25.254.10:80;
}
}
[root@Nginx ~]# systemctl restart nginx.service

十九、缓存加速
当未启用缓存时进行压测
[Administrator.DESKTOP-VJ307M3] ➤ ab -n 10000 -c 50 lee.timinglee.org/index.php
This is ApacheBench, Version 2.3 <$Revision: 1807734 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking lee.timinglee.org (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests
Server Software: nginx/1.28.1
Server Hostname: lee.timinglee.org
Server Port: 80
Document Path: /index.php
Document Length: 72921 bytes
Concurrency Level: 50
Time taken for tests: 13.678 seconds
Complete requests: 10000
Failed requests: 9963 #失败的
(Connect: 0, Receive: 0, Length: 9963, Exceptions: 0)
Total transferred: 731097819 bytes
HTML transferred: 729237819 bytes
Requests per second: 731.10 [#/sec] (mean)
Time per request: 68.390 [ms] (mean)
Time per request: 1.368 [ms] (mean, across all concurrent requests)
Transfer rate: 52197.72 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 7 4.0 6 26
Processing: 4 61 168.8 44 3405
Waiting: 2 38 129.9 26 3316
Total: 5 68 168.7 51 3405
Percentage of the requests served within a certain time (ms)
50% 51
66% 61
75% 68
80% 71
90% 83
95% 92
98% 105
99% 506
100% 3405 (longest request)
设定缓存加速
[root@Nginx ~]# vim /usr/local/nginx/conf/nginx.conf
proxy_cache_path /usr/local/nginx/proxy_cache levels=1:2:2 keys_zone=proxycache:20m inactive=120s max_size=1g;
server {
listen 80;
server_name lee.timinglee.org;
location / {
proxy_pass http://172.25.254.20:80;
}
location ~* \.(php|js)$ {
proxy_pass http://172.25.254.10:80;
proxy_cache proxycache;
proxy_cache_key $request_uri;
proxy_cache_valid 200 302 301 10m;
proxy_cache_valid any 1m;
}
}
[root@Nginx ~]# systemctl restart nginx.service
[root@Nginx ~]# tree /usr/local/nginx/proxy_cache/
/usr/local/nginx/proxy_cache/
0 directories, 0 files
#测试
[Administrator.DESKTOP-VJ307M3] ➤ ab -n 10000 -c 50 lee.timinglee.org/index.php
This is ApacheBench, Version 2.3 <$Revision: 1807734 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking lee.timinglee.org (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests
Server Software: nginx/1.28.1
Server Hostname: lee.timinglee.org
Server Port: 80
Document Path: /index.php
Document Length: 72925 bytes
Concurrency Level: 50
Time taken for tests: 4.365 seconds
Complete requests: 10000
Failed requests: 0
Total transferred: 731110000 bytes
HTML transferred: 729250000 bytes
Requests per second: 2290.76 [#/sec] (mean)
Time per request: 21.827 [ms] (mean)
Time per request: 0.437 [ms] (mean, across all concurrent requests)
Transfer rate: 163554.31 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 4 1.8 4 11
Processing: 4 18 31.3 15 734
Waiting: 1 9 30.7 5 726
Total: 6 22 31.2 20 734
Percentage of the requests served within a certain time (ms)
50% 20
66% 21
75% 21
80% 22
90% 27
95% 32
98% 41
99% 46
100% 734 (longest request)
[root@Nginx ~]# tree /usr/local/nginx/proxy_cache/
/usr/local/nginx/proxy_cache/
└── 1
└── af
└── 15
└── e251273eb74a8ee3f661a7af00915af1
3 directories, 1 file
二十、反向代理负载均衡
实现负载均衡
[root@Nginx ~]# mkdir /usr/local/nginx/conf/upstream/
[root@Nginx ~]# vim /usr/local/nginx/conf/nginx.conf
http {
include mime.types;
default_type application/octet-stream;
include "/usr/local/nginx/conf/upstream/*.conf"; #子配置目录
[root@Nginx ~]# vim /usr/local/nginx/conf/upstream/loadbalance.conf
upstream webserver {
server 172.25.254.10:80 weight=1 fail_timeout=15s max_fails=3;
server 172.25.254.20:80 weight=1 fail_timeout=15s max_fails=3;
server 172.25.254.100:8888 backup;
}
server {
listen 80;
server_name www.timinglee.org;
location ~ / {
proxy_pass http://webserver;
}
}
[root@Nginx ~]# mkdir /webdir/timinglee.org/error/html -p
[root@Nginx ~]# echo error > /webdir/timinglee.org/error/html/index.html
[root@Nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 8888;
root /webdir/timinglee.org/error/html;
}
[root@Nginx ~]# systemctl restart nginx.service
# 测试:
[root@Nginx ~]# curl www.timinglee.org
172.25.254.10
[root@Nginx ~]# curl www.timinglee.org
172.25.254.20
[root@Nginx ~]# curl www.timinglee.org
172.25.254.10
[root@Nginx ~]# curl www.timinglee.org
172.25.254.20
[root@RS1 ~]# systemctl stop httpd
[root@RS2 ~]# systemctl stop httpd
[root@Nginx ~]# curl www.timinglee.org
error
Nginx负载均衡算法
[root@Nginx ~]# vim /usr/local/nginx/conf/upstream/loadbalance.conf
upstream webserver {
#ip_hash;
#hash $request_uri consistent;
#least_conn;
hash $cookie_lee;
server 172.25.254.10:80 weight=1 fail_timeout=15s max_fails=3;
server 172.25.254.20:80 weight=1 fail_timeout=15s max_fails=3;
#server 172.25.254.100:8888 backup;
}
server {
listen 80;
server_name www.timinglee.org;
location ~ / {
proxy_pass http://webserver;
}
}
[root@Nginx ~]# systemctl restart nginx.service
# 测试:
[root@Nginx ~]# curl -b lee=20 www.timinglee.org
172.25.254.10
[root@Nginx ~]# curl -b lee=20 www.timinglee.org
172.25.254.10
[root@Nginx ~]# curl www.timinglee.org/web1/index.html
web1 - 172.25.254.10
[root@Nginx ~]# curl www.timinglee.org/web2/index.html
web2 - 172.25.254.20
[root@Nginx ~]# curl www.timinglee.org/web3/index.html
web3 - 172.25.254.10
二十一、PHP的源码编译
下载源码包
[root@Nginx ~]# wget https://www.php.net/distributions/php-8.3.30.tar.gz
[root@Nginx ~]# wget https://mirrors.aliyun.com/rockylinux/9.7/devel/x86_64/os/Packages/o/oniguruma-devel-6.9.6-1.el9.6.x86_64.rpm #依赖
解压
[root@Nginx ~]# ls
anaconda-ks.cfg echo-nginx-module-0.64.tar.gz nginx-1.29.4
bigfile nginx-1.28.1 nginx-1.29.4.tar.gz
echo-nginx-module-0.64 nginx-1.28.1.tar.gz php-8.3.30.tar.gz
[root@Nginx ~]# tar zxf php-8.3.30.tar.gz
[root@Nginx ~]# ls
anaconda-ks.cfg echo-nginx-module-0.64.tar.gz nginx-1.29.4 php-8.3.30.tar.gz
bigfile nginx-1.28.1 nginx-1.29.4.tar.gz
echo-nginx-module-0.64 nginx-1.28.1.tar.gz php-8.3.30
[root@Nginx ~]# cd php-8.3.30
源码编译
[root@Nginx ~]# dnf install gcc systemd-devel-252-51.el9.x86_64 libxml2-devel.x86_64 sqlite-devel.x86_64 libcurl-devel.x86_64 libpng-devel.x86_64 oniguruma-devel-6.9.6-1.el9.6.x86_64.rpm -y
[root@Nginx ~]# dnf install -y ./oniguruma-devel-6.9.6-1.el9.6.x86_64.rpm
[root@Nginx ~]# cd php-8.3.30
[root@Nginx php-8.3.30]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-curl --with-iconv --with-mhash --with-zlib --with-openssl --enable-mysqlnd --with-mysqli --with-pdo-mysql --disable-debug --enable-sockets --enable-soap --enable-xml --enable-ftp --enable-gd --enable-exif --enable-mbstring --enable-bcmath --with-fpm-systemd
[root@Nginx php-8.3.30]# make
[root@Nginx php-8.3.30]# make install
配置PHP
[root@Nginx php-8.3.30]# cd /usr/local/php/etc
[root@Nginx etc]# cp -p php-fpm.conf.default php-fpm.conf
[root@Nginx etc]# vim php-fpm.conf
[global]
; Pid file
; Note: the default prefix is /usr/local/php/var
; Default Value: none
pid = run/php-fpm.pid
[root@Nginx etc]# cd php-fpm.d/
[root@Nginx php-fpm.d]# cp www.conf.default www.conf
[root@Nginx php-fpm.d]# vim www.conf
listen = 0.0.0.0:9000
[root@Nginx php-fpm.d]# cp /root/php-8.3.30/php.ini-production /usr/local/php/etc/php.ini
[root@Nginx php-fpm.d]# vim /usr/local/php/etc/php.ini
date.timezone = Asia/shangha
[root@Nginx php-fpm.d]# cp /root/php-8.3.30/sapi/fpm/php-fpm.service /lib/systemd/system/
[root@Nginx php-fpm.d]# vim /lib/systemd/system/php-fpm.service
# Mounts the /usr, /boot, and /etc directories read-only for processes invoked by this unit.
#ProtectSystem=full # 注释掉它
[root@Nginx php-fpm.d]# systemctl daemon-reload
[root@Nginx php-fpm.d]# systemctl enable --now php-fpm.service
Created symlink /etc/systemd/system/multi-user.target.wants/php-fpm.service → /usr/lib/systemd/system/php-fpm.service.
[root@Nginx php-fpm.d]# netstat -antlupe | grep php
tcp 0 0 0.0.0.0:9000 0.0.0.0:* LISTEN 0 338281 203764/php-fpm: mas
为php设定环境变量
[root@Nginx ~]# vim ~/.bash_profile
export PATH=$PATH:/usr/local/nginx/sbin:/usr/local/php/sbin:/usr/local/php/bin
[root@Nginx ~]# source ~/.bash_profile
[root@Nginx ~]# php -m
二十二、Nginx整合PHP
# 在windows和linux下都加上lee.timinglee.org
[root@Nginx conf.d]# vim /etc/hosts
172.25.254.100 Nginx www.timinglee.org lee.timinglee.org php.timinglee.org
[root@Nginx php-fpm.d]# cd /usr/local/nginx/conf/conf.d/
[root@Nginx conf.d]# ls
vhosts.conf
[root@Nginx conf.d]# mkdir /webdir/timinglee.org/php/html -p
[root@Nginx conf.d]# echo php.timinglee.org > /webdir/timinglee.org/php/html/index.html
[root@Nginx conf.d]# vim /webdir/timinglee.org/php/html/index.php
<?php
phpinfo();
?>
[root@Nginx conf.d]# vim php.conf
server {
listen 80;
server_name php.timinglee.org;
root /webdir/timinglee.org/php/html;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
}
[root@Nginx conf.d]# systemctl restart nginx.service
# 测试:
http://php.timinglee.org/
http://php.timinglee.org/index.php


二十三、利用memcache实现php的缓存加速
安装memcache
[root@Nginx ~]# dnf install memcached.x86_64 -y
配置memcache
Created symlink /etc/systemd/system/multi-user.target.wants/memcached.service → /usr/lib/systemd/system/memcached.service.
[root@Nginx ~]# vim /etc/sysconfig/memcached
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS="-l 0.0.0.0,::1"
[root@Nginx ~]# systemctl enable --now memcached.service
# 验证:
[root@Nginx ~]# netstat -antlupe | grep memcache
tcp 0 0 127.0.0.1:11211 0.0.0.0:* LISTEN 991 582567 262424/memcached
tcp6 0 0 ::1:11211 :::* LISTEN 991 582568 262424/memcached
升级php对于memcache的支持
# 查看php支持的插件
[root@Nginx ~]# php -m
# 找到压缩包并解压
[root@Nginx ~]# tar zxf memcache-8.2\(1\).tgz
[root@Nginx ~]# cd memcache-8.2/
[root@Nginx memcache-8.2]# ls
config9.m4 config.w32 docker example.php memcache.php src
config.m4 CREDITS Dockerfile LICENSE README tests
[root@Nginx memcache-8.2]# yum install autoconf
[root@Nginx memcache-8.2]# phpize
[root@Nginx memcache-8.2]# ./configure
[root@Nginx memcache-8.2]# vim /usr/local/php/etc/php.ini
extension=memcache
[root@Nginx memcache-8.2]# systemctl restart php-fpm.service
[root@Nginx memcache-8.2]# php -m
PHP Warning: PHP Startup: Invalid date.timezone value 'Asia/shangha', using 'UTC' instead in Unknown on line 0
[PHP Modules]
bcmath
Core
ctype
curl
date
dom
exif
fileinfo
filter
ftp
gd
hash
iconv
json
libxml
mbstring
memcache # 这里就显示支持了
测试性能
[root@Nginx memcache-8.2]# vim memcache.php
define('ADMIN_USERNAME','admin'); // Admin Username
define('ADMIN_PASSWORD','lee'); // Admin Password
$MEMCACHE_SERVERS[] = '172.25.254.100:11211'; // add more as an array
[root@Nginx memcache-8.2]# cp -p memcache.php /webdir/timinglee.org/php/html/
[root@Nginx memcache-8.2]# cp -p example.php /webdir/timinglee.org/php/html/
# 测试:
[root@Nginx memcache-8.2]# ab -n 1000 -c 300 php.timinglee.org/example.php
This is ApacheBench, Version 2.3 <$Revision: 1913912 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking php.timinglee.org (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests
Server Software: nginx/1.28.1
Server Hostname: php.timinglee.org
Server Port: 80
Document Path: /example.php
Document Length: 116 bytes
Concurrency Level: 300
Time taken for tests: 0.338 seconds
Complete requests: 1000
Failed requests: 0 # 0次失败

二十四、Nginx+memcache实现高速缓存
重新编译Nginx
[root@Nginx ~]# cp /usr/local/nginx/conf/ /mnt/ -r
[root@Nginx ~]# systemctl stop nginx.service
[root@Nginx ~]# rm -rf /usr/local/nginx/
[root@Nginx ~]# rm -rf nginx-1.29.4
[root@Nginx ~]# rm -rf nginx-1.28.1
# 重新解压、编译
[root@Nginx ~]# tar zxf nginx-1.28.1.tar.gz
[root@Nginx ~]# tar zxf memc-nginx-module-0.20.tar.gz
[root@Nginx ~]# tar zxf srcache-nginx-module-0.33.tar.gz
[root@Nginx ~]# cd nginx-1.28.1/
[root@Nginx nginx-1.28.1]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module --add-module=/root/echo-nginx-module-0.64 --add-module=/root/memc-nginx-module-0.20 --add-module=/root/srcache-nginx-module-0.33
[root@Nginx ~]# make
[root@Nginx ~]# make install
[root@Nginx ~]# cd /usr/local/nginx/conf
[root@Nginx conf]# rm -fr nginx.conf
[root@Nginx conf]# cp /mnt/conf/nginx.conf /usr/local/nginx/conf/nginx.conf
[root@Nginx conf]# systemctl start nginx.service
整合memcache
[root@Nginx conf]# mkdir -p /usr/local/nginx/conf/conf.d/
[root@Nginx conf]# vim /usr/local/nginx/conf/conf.d/php.conf
upstream memcache {
server 127.0.0.1:11211;
keepalive 512;
}
server {
listen 80;
server_name php.timinglee.org;
root /webdir/timinglee.org/php/html;
index index.php index.html;
location /memc {
internal;
memc_connect_timeout 100ms;
memc_send_timeout 100ms;
memc_read_timeout 100ms;
set $memc_key $query_string;
set $memc_exptime 300;
memc_pass memcache;
}
location ~ \.php$ {
set $key $uri$args;
srcache_fetch GET /memc $key;
srcache_store PUT /memc $key;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
}
[root@Nginx conf]# systemctl restart nginx.service
# 测试:
[root@Nginx conf]# ab -n 10000 -c500 http://php.timinglee.org/example.php
二十五、Nginx的四层负载均衡代理
实验环境(Mysql)
[root@RS1 ~]# dnf install mariadb-server -y
[root@RS2 ~]# dnf install mariadb-server -y
[root@RS1 ~]# vim /etc/my.cnf.d/mariadb-server.cnf
[mysqld]
server-id=10
[root@RS2 ~]# vim /etc/my.cnf.d/mariadb-server.cnf
[mysqld]
server-id=20
[root@RS1 ~]# systemctl enable --now mariadb
Created symlink /etc/systemd/system/mysql.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/mysqld.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /usr/lib/systemd/system/mariadb.service.
[root@RS2 ~]# systemctl enable --now mariadb
Created symlink /etc/systemd/system/mysql.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/mysqld.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /usr/lib/systemd/system/mariadb.service.
[root@RS1 ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 10.5.27-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> create user lee@'%' identified by 'lee';
Query OK, 0 rows affected (0.001 sec)
MariaDB [(none)]> grant all on *.* to lee@'%';
Query OK, 0 rows affected (0.001 sec)
[root@RS2 ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 10.5.27-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> create user lee@'%' identified by 'lee';
Query OK, 0 rows affected (0.001 sec)
MariaDB [(none)]> grant all on *.* to lee@'%';
Query OK, 0 rows affected (0.001 sec)
实验环境(dns)
[root@RS1 ~]# dnf install bind -y
[root@RS2 ~]# dnf install bind -y
[root@RS1 ~]# vim /etc/named.conf
options {
// listen-on port 53 { 127.0.0.1; };
// listen-on-v6 port 53 { ::1; };
directory "/var/named";
dump-file "/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
memstatistics-file "/var/named/data/named_mem_stats.txt";
secroots-file "/var/named/data/named.secroots";
recursing-file "/var/named/data/named.recursing";
// allow-query { localhost; };
[root@RS2 ~]# vim /etc/named.conf
options {
// listen-on port 53 { 127.0.0.1; };
// listen-on-v6 port 53 { ::1; };
directory "/var/named";
dump-file "/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
memstatistics-file "/var/named/data/named_mem_stats.txt";
secroots-file "/var/named/data/named.secroots";
recursing-file "/var/named/data/named.recursing";
// allow-query { localhost; };
dnssec-validation no;
[root@RS1 ~]# vim /etc/named.rfc1912.zones
zone "timinglee.org" IN {
type master;
file "timinglee.org.zone";
allow-update { none; };
};
[rozone "timinglee.org" IN {
type master;
file "timinglee.org.zone";
allow-update { none; };
};
ot@RS2 ~]# vim /etc/named.rfc1912.zones
zone "timinglee.org" IN {
type master;
file "timinglee.org.zone";
allow-update { none; };
};
[root@RS1 ~]# cd /var/named/
[root@RS1 named]# cp -p named.localhost timinglee.org.zone
[root@RS2 ~]# cd /var/named/
[root@RS2 named]# cp -p named.localhost timinglee.org.zone
[root@RS1 named]# vim timinglee.org.zone
$TTL 1D
@ IN SOA dns.timingle.org. rname.invalid. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS dns.timinglee.org.
dns A 172.25.254.20
[root@RS1 named]# systemctl enable --now named
Created symlink /etc/systemd/system/multi-user.target.wants/named.service → /usr/lib/systemd/system/named.service.
[root@RS2 named]# vim timinglee.org.zone
$TTL 1D
@ IN SOA dns.timingle.org. rname.invalid. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS dns.timinglee.org.
dns A 172.25.254.20
[root@RS2 named]# systemctl enable --now named
Created symlink /etc/systemd/system/multi-user.target.wants/named.service → /usr/lib/systemd/system/named.service.
# 测试:
[root@RS1 named]# dig dns.timinglee.org @172.25.254.10
; <<>> DiG 9.16.23-RH <<>> dns.timinglee.org @172.25.254.10
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 45658
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
; COOKIE: 6a071fa62027bd58010000006983062a1c429755e77f30c4 (good)
;; QUESTION SECTION:
;dns.timinglee.org. IN A
;; ANSWER SECTION:
dns.timinglee.org. 86400 IN A 172.25.254.10
;; Query time: 0 msec
;; SERVER: 172.25.254.10#53(172.25.254.10)
;; WHEN: Wed Feb 04 16:41:14 CST 2026
;; MSG SIZE rcvd: 90
[root@RS1 named]# dig dns.timinglee.org @172.25.254.20
; <<>> DiG 9.16.23-RH <<>> dns.timinglee.org @172.25.254.20
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 4142
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
; COOKIE: 9c49d08b25c9b3cf010000006983064800c4c2411febd8b1 (good)
;; QUESTION SECTION:
;dns.timinglee.org. IN A
;; ANSWER SECTION:
dns.timinglee.org. 86400 IN A 172.25.254.20
;; Query time: 1 msec
;; SERVER: 172.25.254.20#53(172.25.254.20)
;; WHEN: Wed Feb 04 16:41:44 CST 2026
;; MSG SIZE rcvd: 90
tcp四层负载
[root@Nginx conf]# mkdir /usr/local/nginx/conf/tcp -p
[root@Nginx conf]# mkdir /usr/local/nginx/conf/udp -p
[root@Nginx conf]# vim /usr/local/nginx/conf/nginx.conf
include "/usr/local/nginx/conf/tcp/*.conf";
[root@Nginx conf]# vim /usr/local/nginx/conf/tcp/mariadb.conf
stream {
upstream mysql_server {
server 172.25.254.10:3306 max_fails=3 fail_timeout=30s;
server 172.25.254.20:3306 max_fails=3 fail_timeout=30s;
}
server {
listen 172.25.254.100:3306;
proxy_pass mysql_server;
proxy_connect_timeout 30s;
proxy_timeout 300s;
}
}
[root@Nginx conf]# systemctl restart nginx.service
# 测试:
[root@Nginx ~]# mysql -ulee -plee -h172.25.254.100
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 10.5.27-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> SELECT @@server_id;
+-------------+
| @@server_id |
+-------------+
| 10 |
+-------------+
1 row in set (0.001 sec)
MariaDB [(none)]>
[1]+ 已停止 mysql -ulee -plee -h172.25.254.100
[root@Nginx ~]# mysql -ulee -plee -h172.25.254.100
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 10.5.27-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> select @@server_id;
+-------------+
| @@server_id |
+-------------+
| 20 |
+-------------+
1 row in set (0.001 sec)
udp四层负载
[root@Nginx ~]# vim /usr/local/nginx/conf/tcp/mariadb.conf
stream {
upstream mysql_server {
server 172.25.254.10:3306 max_fails=3 fail_timeout=30s;
server 172.25.254.20:3306 max_fails=3 fail_timeout=30s;
}
upstream dns_server{
server 172.25.254.10:53 max_fails=3 fail_timeout=30s;
server 172.25.254.20:53 max_fails=3 fail_timeout=30s;
}
server {
listen 172.25.254.100:3306;
proxy_pass mysql_server;
proxy_connect_timeout 30s;
proxy_timeout 300s;
}
server {
listen 172.25.254.100:53 udp;
proxy_pass dns_server;
proxy_timeout 1s;
proxy_responses 1;
error_log logs/dns.log;
}
}
# 测试:
[root@Nginx ~]# dig dns.timinglee.org @172.25.254.100
; <<>> DiG 9.16.23-RH <<>> dns.timinglee.org @172.25.254.100
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 43079
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
; COOKIE: 49ab440060e728ec0100000069830b62ec0c74ec4c5f47f6 (good)
;; QUESTION SECTION:
;dns.timinglee.org. IN A
;; ANSWER SECTION:
dns.timinglee.org. 86400 IN A 172.25.254.10
;; Query time: 2 msec
;; SERVER: 172.25.254.100#53(172.25.254.100)
;; WHEN: Wed Feb 04 17:03:30 CST 2026
;; MSG SIZE rcvd: 90
[root@Nginx ~]# dig dns.timinglee.org @172.25.254.100
; <<>> DiG 9.16.23-RH <<>> dns.timinglee.org @172.25.254.100
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 7253
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
; COOKIE: 94342097b8472da60100000069830b6afe57116eb7d50acd (good)
;; QUESTION SECTION:
;dns.timinglee.org. IN A
;; ANSWER SECTION:
dns.timinglee.org. 86400 IN A 172.25.254.20
;; Query time: 1 msec
;; SERVER: 172.25.254.100#53(172.25.254.100)
;; WHEN: Wed Feb 04 17:03:38 CST 2026
;; MSG SIZE rcvd: 90
二十六、编译安装openresty
[root@webserver ~]# wget https://openresty.org/download/openresty-1.27.1.2.tar.gz
[root@webserver ~]# dnf -yq install gcc pcre-devel openssl-devel perl zlib-devel
[root@webserver ~]# useradd -r -s /sbin/nologin nginx
[root@webserver ~]# tar zxf openresty-1.27.1.2.tar.gz
[root@webserver ~]# cd openresty-1.27.1.2/
[root@webserver openresty-1.27.1.2]# ./configure --prefix=/usr/local/openresty --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_stub_status_module --with-http_gzip_static_module
[root@webserver openresty-1.27.1.2]# gmake && gmake install
[root@webserver openresty-1.27.1.2]# vim ~/.bash_profile
export PATH=$PATH:/usr/local/openresty/bin
[root@webserver openresty-1.27.1.2]# source ~/.bash_profile
[root@webserver openresty-1.27.1.2]# openresty -v
nginx version: openresty/1.27.1.2
# 这就是80端口被占用了,所以此时需要关闭原本的80端口的httpd服务,就可以成功启动
[root@webserver openresty-1.27.1.2]# openresty
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
[root@webserver openresty-1.27.1.2]# systemctl stop httpd.service
[root@webserver openresty-1.27.1.2]# openresty
[root@webserver openresty-1.27.1.2]# echo hello test > /usr/local/openresty/nginx/html/index.html
[root@webserver openresty-1.27.1.2]# curl 172.25.254.200
hello test
更多推荐
所有评论(0)