1. web服务基本介绍
1.1 http协议访问流程

1.2 Apache的运行机制
1.2.1 Apache prefork****模型
1.预派生模式,有一个主控制进程,然后生成多个子进程,使用select模型,最大并发1024
2.每个子进程有一个独立的线程响应用户请求。
3.相对比较占用内存,但是比较稳定,可以设置最大和最小进程数。
4.是最古老的一种模式,也是最稳定的模式,适用于访问量不是很大的场景。
优点:稳定。
缺点:每个用户请求需要对应开启一个进程,占用资源较多,并发性差,不适用于高并发场景。

1.2.2 Apache worker****模型
1.一种多进程和多线程混合的模型。
2.有一个控制进程,启动多个子进程。
3.每个子进程里面包含固定的线程。
4.使用线程程来处理请求。
5.当线程不够使用的时候会再启动一个新的子进程,然后在进程里面再启动线程处理请求,
6.由于其使用了线程处理请求,因此可以承受更高的并发。
优点:相比prefork 占用的内存较少,可以同时处理更多的请求。
缺点:使用keepalive的长连接方式,某个线程会一直被占据,即使没有传输数据,也需要一直等待到超 时才会被释放。如果过多的线程,被这样占据,也会导致在高并发场景下的无服务线程可用(该问题在prefork模式下,同样会发生)。

1.2.3 Apache event****模型
1.Apache中最新的模式,2012年发布的apache 2.4.X系列正式支持event 模型,属于事件驱动模型(epoll)。
2.每个进程响应多个请求,在现在版本里的已经是稳定可用的模式。
3.它和worker模式很像,最大的区别在于,它解决了keepalive场景下长期被占用的线程的资源浪费问题 (某些线程因为被keepalive,空挂在哪里等待,中间几乎没有请求过来,甚至等到超时)。4.event MPM中,会有一个专门的线程来管理这些keepalive类型的线程。当有真实请求过来的时候,将请求传递给服务线程,执行完毕后,又允许它释放。这样增强了高并发场景下的请求处理能力。
优点:单线程响应多请求,占据更少的内存,高并发下表现更优秀,会有一个专门的线程来管理keep
alive类型的线程,当有真实请求过来的时候,将请求传递给服务线程,执行完毕后,又允许它释放
缺点:没有线程安全控制

1.3 nginx的源码编译
1.3.1 下载软件
bash
[root@nginx ~]# ip route add default via 192.168.83.2 dev eth0
[root@Nginx ~]# wget https://nginx.org/download/nginx-1.28.1.tar.gz
1.3.2 解压
bash
[root@Nginx ~]# tar zxf 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
1.3.3 检测环境
bash
[root@Nginx ~]# dnf install gcc openssl-devel.x86_64 pcre2-devel.x86_64 zlib-devel -y
[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
1.3.4 编译
bash
[root@Nginx nginx-1.28.1]# make
[root@Nginx nginx-1.28.1]# make install
1.3.5 nginx启动
bash
[root@Nginx sbin]# vim ~/.bash_profile
export PATH=$PATH:/usr/local/nginx/sbin
[root@Nginx sbin]# source ~/.bash_profile
[root@Nginx logs]# useradd -s /sbin/nologin -M nginx
[root@Nginx logs]# nginx
[root@Nginx logs]# ps aux | grep nginx
root 44012 0.0 0.1 14688 2356 ? Ss 17:01 0:00 nginx: master process nginx
nginx 44013 0.0 0.2 14888 3892 ? S 17:01 0:00 nginx: worker process
root 44015 0.0 0.1 6636 2176 pts/0 S+ 17:01 0:00 grep --color=auto nginx
1.3.6 编写启动文件
bash
[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
测试:

1.4 平滑升级与版本回滚
1.4.1 下载另外一个版本nginx
bash
[root@Nginx ~]# wget https://nginx.org/download/nginx-1.29.4.tar.gz
1.4.2 对于新版本的软件进行源码编译并进行平滑升级
bash
[root@Nginx ~]# tar zxf nginx-1.29.4.tar.gz
[root@Nginx ~]# cd nginx-1.29.4/src/core/
[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]# cd /usr/local/nginx/sbin/
[root@Nginx sbin]# ls
nginx
[root@Nginx sbin]# \cp -f /root/nginx-1.29.4/objs/nginx /usr/local/nginx/sbin/nginx
[root@Nginx sbin]# ls /usr/local/nginx/logs/
access.log error.log nginx.pid
[root@nginx sbin]# ps aux | grep nginx
root 823 0.0 0.1 14688 2360 ? Ss 20:40 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 824 0.0 0.1 14888 3896 ? S 20:40 0:00 nginx: worker process
root 1573 0.0 0.4 22476 9344 pts/0 S+ 20:40 0:00 systemctl status nginx.service
root 4898 0.0 0.1 6636 2304 pts/1 S+ 20:50 0:00 grep --color=auto nginx
[root@nginx sbin]# kill -USR2 823
[root@nginx sbin]# ps aux | grep nginx
root 823 0.0 0.1 14688 2872 ? Ss 20:40 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 824 0.0 0.1 14888 3896 ? S 20:40 0:00 nginx: worker process
root 1573 0.0 0.4 22476 9344 pts/0 S+ 20:40 0:00 systemctl status nginx.service
root 4899 0.0 0.3 14716 8320 ? S 20:50 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 4900 0.0 0.1 14916 4284 ? S 20:50 0:00 nginx: worker process
root 4902 0.0 0.1 6636 2304 pts/1 S+ 20:50 0:00 grep --color=auto nginx
[root@nginx sbin]# ls /usr/local/nginx/logs/
access.log error.log nginx.pid nginx.pid.oldbin
测试:

1.4.3 版本回退
bash
[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 1643 0.0 0.1 14688 2744 ? Ss 09:55 0:00 nginx: master process /usr/local/nginx/sbin/nginx
root 4919 0.0 0.4 14716 7936 ? S 10:24 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 4921 0.0 0.2 14916 4156 ? S 10:24 0:00 nginx: worker process
[root@Nginx sbin]# kill -HUP 1643
[root@Nginx sbin]# ps aux | grep nginx
root 1643 0.0 0.1 14688 2744 ? Ss 09:55 0:00 nginx: master process /usr/local/nginx/sbin/nginx
root 4919 0.0 0.4 14716 7936 ? S 10:24 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 4921 0.0 0.2 14916 4156 ? S 10:24 0:00 nginx: worker process
nginx 4963 0.0 0.2 14888 3896 ? S 10:32 0:00 nginx: worker process
root 4965 0.0 0.1 6636 2176 pts/0 S+ 10:32 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.5 网络IO模型
1.5.1 阻塞型I/O模型

阻塞IO模型是最简单的I/O模型,用户线程在内核进行IO操作时被阻塞。
用户线程通过系统调用read发起I/O读操作,由用户空间转到内核空间。内核等到数据包到达后,然 后将接收的数据拷贝到用户空间,完成read操作。
用户需要等待read将数据读取到buffer后,才继续处理接收的数据。整个I/O请求的过程中,用户线
程是被阻塞的,这导致用户在发起IO请求时,不能做任何事情,对CPU的资源利用率不够。
同步阻塞:程序向内核发送I/O请求后一直等待内核响应,如果内核处理请求的IO操作不能立即返回,则进 程将一直等待并不再接受新的请求,并由进程轮询查看I/O是否完成,完成后进程将I/O结果返回给 Client,在IO没有返回期间进程不能接受其他客户的请求,而且是有进程自己去查看I/O是否完成,这种 方式简单,但是比较慢,用的比较少。
优点:程序简单,在阻塞等待数据期间进程/线程挂起,基本不会占用 CPU 资源
缺点:每个连接需要独立的进程/线程单独处理,当并发请求量大时为了维护程序,内存、线程切换开销 较apache 的preforck使用的是这种模式。
1.5.2 非阻塞型I/O模型

用户线程发起IO请求时立即返回。但并未读取到任何数据,用户线程需要不断地发起IO请求,直到数据 到达后,才真正读取到数据,继续执行。即 "轮询"机制存在两个问题:如果有大量文件描述符都要等, 那么就得一个一个的read。这会带来大量的Context Switch(read是系统调用,每调用一次就得在用户 态和核心态切换一次)。轮询的时间不好把握。这里是要猜多久之后数据才能到。等待时间设的太长,程序响应延迟就过大;设的太短,就会造成过于频繁的重试,干耗CPU而已,是比较浪费CPU的方式,一 般很少直接使用这种模型,而是在其他IO模型中使用非阻塞IO这一特性。
非阻塞:程序向内核发送请I/O求后一直等待内核响应,如果内核处理请求的IO操作不能立即返回IO结 果,进程将不再等待,而且继续处理其他请求,但是仍然需要进程隔一段时间就要查看内核I/O是否完成。
1.5.3 信号驱动式I/O模型

信号驱动I/O的意思就是进程现在不用傻等着,也不用去轮询。而是让内核在数据就绪时,发送信号通知 进程。
调用的步骤是,通过系统调用 sigaction ,并注册一个信号处理的回调函数,该调用会立即返回,然后主 程序可以继续向下执行,当有I/O操作准备就绪,即内核数据就绪时,内核会为该进程产生一个 SIGIO信 号,并回调注册的信号回调函数,这样就可以在信号回调函数中系统调用 recvfrom 获取数据,将用户进 程所需要的数据从内核空间拷贝到用户空间。
此模型的优势在于等待数据报到达期间进程不被阻塞。用户主程序可以继续执行,只要等待来自信号处 理函数的通知。
异步阻塞:程序进程向内核发送IO调用后,不用等待内核响应,可以继续接受其他请求,内核收到进程 请求后进行的IO如果不能立即返回,就由内核等待结果,直到IO完成后内核再通知进程。
1.5.4 异步I/O模型

异步I/O 与 信号驱动I/O最大区别在于,信号驱动是内核通知用户进程何时开始一个I/O操作,而异步I/O 是由内核通知用户进程I/O操作何时完成,两者有本质区别,相当于不用去饭店场吃饭,直接点个外卖,把 等待上菜的时间也给省了。
相对于同步I/O,异步I/O不是顺序执行。用户进程进行aio_read系统调用之后,无论内核数据是否准备 好,都会直接返回给用户进程,然后用户态进程可以去做别的事情。等到socket数据准备好了,内核直 接复制数据给进程,然后从内核向进程发送通知。IO两个阶段,进程都是非阻塞的。
信号驱动IO当内核通知触发信号处理程序时,信号处理程序还需要阻塞在从内核空间缓冲区拷贝数据到 用户空间缓冲区这个阶段,而异步IO直接是在第二个阶段完成后,内核直接通知用户线程可以进行后续 操作了。
异步非阻塞:程序进程向内核发送IO调用后,不用等待内核响应,可以继续接受其他请求,内核调用的 IO如果不能立即返回,内核会继续处理其他事物,直到IO完成后将结果通知给内核,内核在将IO完成的 结果返回给进程,期间进程可以接受新的请求,内核也可以处理新的事物,因此相互不影响,可以实现 较大的同时并实现较高的IO复用,因此异步非阻塞使用最多的一种通信方式。
1.5.5 多路复用I/O模型

当用户进程调用了select,那么整个进程会被block,而同时,kernel会"监视"所有select负责的socket, 当任何一个socket中的数据准备好了,select就会返回。这个时候用户进程再调用read操作,将数据从 kernel拷贝到用户进程。
它的基本原理就是select/poll/epoll这个function会不断的轮询所负责的所有socket,当某个socket有数据到达了,就通知用户进程。
1.6 零拷贝
1.6.1 零拷贝介绍

传统的 Linux 系统的标准 I/O 接口(read、write)是基于数据拷贝的,也就是数据都是 copy_to_user或者 copy_from_user,这样做的好处是,通过中间缓存的机制,减少磁盘 I/O 的操作,但是坏处也很明显,大量数据的拷贝,用户态和内核态的频繁切换,会消耗大量的 CPU 资源,严重影响数据传输的性 能。
而零拷贝就是上述问题的一个解决方案,通过尽量避免拷贝操作来缓解 CPU 的压力。零拷贝并没有真正做到"0"拷贝,它更多是一种思想,很多的零拷贝技术都是基于这个思想去做的优化。
1.6.2 MMAP

1.6.3 SENDFILE

1.6.4 DMA 辅助的 SENDFILE

2.nginx架构和进程
2.1 nginx架构

2.2 nginx进程
多进程方式:服务器每接收到一个客户端请求就有服务器的主进程生成一个子进程响应客户端,直
到用户关闭连接,这样的优势是处理速度快,子进程之间相互独立,但是如果访问过大会导致服务
器资源耗尽而无法提供请求
多线程方式:与多进程方式类似,但是每收到一个客户端请求会有服务进程派生出一个线程和此客
户端进行交互,一个线程的开销远远小于一个进程,因此多线程方式在很大程度减轻了web服务器
对系统资源的要求,但是多线程也有自己的缺点,即当多个线程位于同一个进程内工作的时候,可
以相互访问同样的内存地址空间,所以他们相互影响,一旦主进程挂掉则所有子线程都不能工作
了,IIS服务器使用了多线程的方式,需要间隔一段时间就重启一次才能稳定。
Nginx是多进程组织模型,而且是一个由Master主进程和Worker工作进程组成。

3. nginx核心配置
3.1 nginx参数优化
bash
[root@Nginx ~]# vim /usr/local/nginx/conf/nginx.conf
events {
worker_connections 10000;
use epoll;
accept_mutex on;
multi_accept on;
}
[root@Nginx ~]# nginx -s reload
测试并发:
bash
[root@nginx ~]# dnf install httpd-tools -y
[root@nginx ~]# ab -n 100000 -c5000 http://192.168.83.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 192.168.83.100 (be patient)
socket: Too many open files (24)
优化:
bash
[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
测试:

3.2 root与alias
3.2.1 root
bash
[root@Nginx conf]# 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"; #不要写在sever里面
[root@Nginx conf]# nginx -s reload
[root@Nginx conf]# cd conf.d/
[root@Nginx ~]# mkdir -p /webdata/nginx/timinglee.org/lee/html
[root@Nginx ~]# echo lee.timinglee.org > /webdata/nginx/timinglee.org/lee/html/index.html
[root@Nginx conf.d]# vim vhosts.conf
server {
listen 80;
server_name haha.hhh.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

3.2.2 alias
bash
[root@Nginx conf.d]# vim vhosts.conf
server {
listen 80;
server_name haha.hhh.org;
location /passwd {
alias /etc/passwd;
}
location /passwd/ {
alias /mnt/;
}
}
[root@Nginx conf.d]# nginx -s reload
[root@Nginx conf.d]# echo www > /mnt/index.html

3.3 web访问长链接优化
3.3.1 设置长连接时间
bash
[root@Nginx ~]# vim /usr/local/nginx/conf/nginx.conf
keepalive_timeout 5;
[root@Nginx ~]# nginx -s reload

3.3.2 设置长连接次数
bash
[root@Nginx ~]# vim /usr/local/nginx/conf/nginx.conf
keepalive_requests 3;
[root@Nginx ~]# nginx -s reload

3.4 目录访问用户认证
bash
[root@Nginx ~]# htpasswd -cmb /usr/local/nginx/conf/.htpasswd haha hhh
Adding password for user haha
[root@nginx ~]# mkdir /usr/local/nginx/html/haha/
[root@nginx ~]# echo hhh > /usr/local/nginx/html/haha/index.html
[root@Nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name haha.hhh.org;
location /haha {
root /usr/local/nginx/html;
auth_basic "login passwd";
auth_basic_user_file "/usr/local/nginx/conf/.htpasswd";
}
}
[root@Nginx ~]# systemctl restart nginx.service

3.5 自定义错误页面
bash
[root@nginx ~]# mkdir /usr/local/nginx/errorpage
[root@nginx ~]# echo "糟糕,你访问的页面不见了!!!" > /usr/local/nginx/errorpage/errormessage
[root@nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name haha.hhh.org;
error_page 404 405 503 502 /error;
location /haha {
root /usr/local/nginx/html;
}
location /error {
alias /usr/local/nginx/errorpage/errormessage;
}
}

3.6 自定义错误日志
bash
[root@nginx ~]# mkdir -p /usr/local/nginx/logs/hahahhh.org/
[root@nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name haha.hhh.org;
error_page 404 405 503 502 /error;
error_log logs/hahahhh.org/hhh.error error;
location /haha {
root /usr/local/nginx/html;
}
location /error {
alias /usr/local/nginx/errorpage/errormessage;
}
}
[root@nginx ~]# systemctl restart nginx.service

3.7 自定义下载服务器
bash
[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.0448041 s,2.3 GB/s
[root@nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name haha.hhh.org;
error_page 404 405 503 502 /error;
error_log logs/hahahhh.org/hhh.error error;
location /haha {
root /usr/local/nginx/html;
}
location /error {
alias /usr/local/nginx/errorpage/errormessage;
}
location /download{
root /usr/local/nginx;
}
}
[root@nginx ~]# nginx -s reload
访问

3.7.1 启用列表功能
bash
[root@nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name haha.hhh.org;
error_page 404 405 503 502 /error;
error_log logs/hahahhh.org/hhh.error error;
location /haha {
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
效果展示:

3.7.2 下载控速
bash
[root@nginx ~]# wget http://haha.hhh.org/download/bigfile
--2026-02-20 18:56:42-- http://haha.hhh.org/download/bigfile
正在解析主机 haha.hhh.org (haha.hhh.org)... 192.168.83.100
正在连接 haha.hhh.org (haha.hhh.org)|192.168.83.100|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:104857600 (100M) [application/octet-stream]
正在保存至: "bigfile"
bigfile 100%[==============================================>] 100.00M 617MB/s 用时 0.2s
2026-02-20 18:56:42 (617 MB/s) - 已保存 "bigfile" [104857600/104857600])
[root@nginx ~]# rm -rf bigfile
[root@nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name haha.hhh.org;
error_page 404 405 503 502 /error;
error_log logs/hahahhh.org/hhh.error error;
location /haha {
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 ~]# nginx -s reload

3.7.3 显示文件大小
bash
[root@nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name haha.hhh.org;
error_page 404 405 503 502 /error;
error_log logs/hahahhh.org/hhh.error error;
location /haha {
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 ~]# nginx -s reload

3.7.4 时间显示调整
bash
[root@nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name haha.hhh.org;
error_page 404 405 503 502 /error;
error_log logs/hahahhh.org/hhh.error error;
location /haha {
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 ~]# nginx -s reload

3.7.5 设定页面风格
bash
[root@nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name haha.hhh.org;
error_page 404 405 503 502 /error;
error_log logs/hahahhh.org/hhh.error error;
location /haha {
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 ~]# nginx -s reload


3.8 文件检测工具
bash
[root@nginx ~]# echo hhh > /usr/local/nginx/errorpage/default.html
[root@nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name haha.hhh.org;
error_page 404 405 503 502 /error;
error_log logs/hahahhh.org/hhh.error error;
root /usr/local/nginx/errorpage;
try_files $uri $uri.html $uri/index.html /default.html;
location /haha {
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 json;
}
}
[root@nginx ~]# nginx -s reload

4. nginx高级配置
4.1 状态页
bash
[root@nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name haha.hhh.org;
location /nginx_status{
stub_status;
auth_basic "auth login";
auth_basic_user_file /usr/local/nginx/conf/.htpasswd;
allow 192.168.83.0/24;
deny all;
}
}
[root@nginx ~]# nginx -s reload

4.2 数据传输压缩功能
bash
[root@nginx ~]# mkdir /usr/local/nginx/hhh.org/hhh/html -p
[root@nginx ~]# echo hello hhh > /usr/local/nginx/hhh.org/hhh/html/index.html
[root@nginx ~]# cp /usr/local/nginx/logs/access.log /usr/local/nginx/hhh.org/hhh/html/bigfile.txt
[root@nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name haha.hhh.org;
root /usr/local/nginx/hhh.org/hhh/html;
location /nginx_status{
stub_status;
auth_basic "auth login";
auth_basic_user_file /usr/local/nginx/conf/.htpasswd;
allow 192.168.83.0/24;
deny all;
}
}
[root@nginx ~]# nginx -s reload
gzip on;
gzip_comp_level 4;
gzip_disable "MSIE [1-6]\.";
gzip_min_length 1024k;
gzip_buffers 32 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;

4.3 变量使用
4.3.1 升级nginx支持echo
bash
[root@nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
[root@nginx ~]# systemctl stop nginx.service
[root@nginx ~]# ps aux | grep nginx
root 13117 0.0 0.1 6636 2304 pts/0 S+ 17:31 0:00 grep --color=auto nginx
[root@nginx ~]# tar zxf echo-nginx-module-0.64.tar.gz
[root@nginx ~]# cd nginx-1.28.1/
[root@nginx nginx-1.28.1]# make clean
[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
[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
[root@nginx nginx-1.28.1]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name haha.hhh.org;
root /usr/local/nginx/hhh.org/hhh/html;
location /vars {
default_type text/html;
echo $remote_addr;
}
}

4.3.2 理解内建变量
remote_addr;
存放了客户端的地址,注意是客户端的公网IP
args;
变量中存放了URL中的所有参数 ,例如:https://search.jd.com/Search?keyword=手机\&enc=utf-8,则返回结果为:keyword=手机\&enc=utf-8
is_args
如果有参数为?否则为空
document_root;
保存了针对当前资源的请求的系统根目录。
document_uri;
保存了当前请求中不包含参数的URI,注意是不包含请求的指令 ,如:http://lee.timinglee.org/var?\\id=11111会被定义为/var ,返回结果为:/var。
host;
存放了请求的host名称 ,如果nginx服务器使用limit_rate配置了显示网络速率,则会显示,如果没有设置,则显示0。
remote_port;
客户端请求Nginx服务器时随机打开的端口,这是每个客户端自己的端口。
remote_user;
已经经过AuthBasicModule验证的用户名。
request_body_file;
做反向代理时发给后端服务器的本地资源的名称。
request_method;
请求资源的方式,GET/PUT/DELETE等
request_filename;
当前请求的资源文件的磁盘路径,由root或alias指令与URI请求生成的文件绝对路径。
request_uri;
包含请求参数的原始URI,不包含主机名,相当于:document_uri?args。
scheme;
请求的协议,例如:http,https,ftp等
server_protocol;
保存了客户端请求资源使用的协议的版本,例如:HTTP/1.0,HTTP/1.1,HTTP/2.0等
server_addr;
保存了服务器的IP地址
server_name
虚拟主机的主机名
server_port
虚拟主机的端口号
http_user_agent
客户端浏览器的详细信息
http_cookie
客户端的所有cookie信息
cookie_
name为任意请求报文首部字部cookie的key名
$http_
name为任意请求报文首部字段,表示记录请求报文的首部字段,name的对应的首部字段名需要为小写,如果有 横线需要替换为下划线
bash
[root@nginx nginx-1.28.1]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name haha.hhh.org;
root /usr/local/nginx/hhh.org/hhh/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;
}
}

4.3.3 自定义变量
手动设置变量值
bash
[root@nginx nginx-1.28.1]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name haha.hhh.org;
root /usr/local/nginx/hhh.org/hhh/html;
location /vars {
default_type text/html;
set $test hhhh;
echo $test;
}
}
[root@Nginx ~]# nginx -s reload

变量传递
bash
[root@nginx nginx-1.28.1]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name haha.hhh.org;
root /usr/local/nginx/hhh.org/hhh/html;
location /vars {
default_type text/html;
set $test hhhh;
echo $test;
set $web_port $server_port;
echo $web_port;
}
}
[root@Nginx ~]# nginx -s reload

5.Nginx Rewrite****相关功能
5.1 rewrite模块中的指令
rewrite将用户请求的URI基于regex所描述的模式进行检查,匹配到时将其替换为表达式指定的新的URI。
正则表达式的格式:
|-----------|-----------------------|
| . | 匹配除换行符以外的任意字符 |
| \w | 匹配字母或数字或下划线或汉字 |
| \s | 匹配任意的空白符 |
| \d | 匹配数字 |
| \b | 匹配单词的开始或结束 |
| ^ | 匹配字付串的开始 |
| $ | 匹配字付串的结束 |
| * | 匹配重复零次或更多次 |
| + | 匹配重复一次或更多次 |
| ? | 匹配重复零次或一次 |
| (n) | 匹配重复n次 |
| {n, } | 匹配重复n次或更多次 |
| {n,m} | 匹配重复n次到m次 |
| *? | 匹配重复任意次,但尽可能少重复 |
| +? | 匹配重复1次或更多次,但尽可能少重复 |
| ?? | 匹配重复0次或1次,但尽可能少重复 |
| {n,m}? | 匹配重复n到m次,但尽可能少重复 |
| {n, }? | 匹配重复n次以上,但尽可能少重复 |
| \w | 匹配任意不是字母,数字,下划线,汉字的字符 |
| \s | 匹配任意不是空白符的字符 |
| \D | 匹配任意非数字的字符 |
| \B | 匹配不是单词开头或结束的位置 |
| [^x] | 匹配除了x以外的任意字符 |
| [^hhh] | 匹配除hhh这几个字母以外的任意字符 |
5.2 网页从写动作
5.2.1 网页重写指令
if
bash
[root@nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name haha.hhh.org;
root /usr/local/nginx/hhh.org/hhh/html;
location /vars {
echo $remote_user;
echo $request_method;
echo $request_filename;
echo $request_uri;
echo $scheme;
}
location / {
if ( $http_user_agent ~* hhh ) {
return 200 "test if messages";
}
}
}
[root@Nginx ~]# nginx -s reload

set
bash
[root@nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name haha.hhh.org;
root /usr/local/nginx/hhh.org/hhh/html;
location /vars {
echo $remote_user;
echo $request_method;
echo $request_filename;
echo $request_uri;
echo $scheme;
}
location / {
set $testname hhh;
echo $testname;
}
}
[root@Nginx ~]# nginx -s reload

return
bash
[root@nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name haha.hhh.org;
root /usr/local/nginx/hhh.org/hhh/html;
location /vars {
echo $remote_user;
echo $request_method;
echo $request_filename;
echo $request_uri;
echo $scheme;
}
location / {
return 200 "hello world";
}
}
[root@Nginx ~]# nginx -s reload

break
bash
[root@nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name haha.hhh.org;
root /usr/local/nginx/hhh.org/hhh/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 = hhh){
break;
}
set $test3 lee3;
echo $test1 $test2 $test3;
}
}
[root@Nginx ~]# nginx -s reload

5.2.2 flag
redirect
bash
[root@nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name haha.hhh.org;
root /usr/local/nginx/hhh.org/hhh/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 ~]# nginx -s reload

permanent
bash
[root@nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name haha.hhh.org;
root /usr/local/nginx/hhh.org/hhh/html;
location /vars {
echo $remote_user;
echo $request_method;
echo $request_filename;
echo $request_uri;
echo $scheme;
}
location / {
rewrite / http://www.taobao.com permanent;
}
}
[root@Nginx ~]# nginx -s reload

break
bash
[root@nginx ~]# mkdir /webdir/hhh.org/hhh/html/{break,last,hhhh,haha} -p
[root@nginx ~]# echo break > /webdir/hhh.org/hhh/html/break/index.html
[root@nginx ~]# echo last > /webdir/hhh.org/hhh/html/last/index.html
[root@nginx ~]# echo hhh > /webdir/hhh.org/hhh/html/hhhh/index.html
[root@nginx ~]# echo haha > /webdir/hhh.org/hhh/html/haha/index.html
[root@nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name haha.hhh.org;
root /webdir/hhh.org/hhh/html;
location /vars {
echo $remote_user;
echo $request_method;
echo $request_filename;
echo $request_uri;
echo $scheme;
}
location /break {
rewrite /break/(.*) /hhhh/$1 break;
rewrite /hhhh /haha;
}
location /hhhh {
return 200 "hhhh end page";
}
location /haha {
return 200 "HAHA END PAGE";
}
}
[root@nginx ~]# nginx -s reload

last
bash
[root@nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name haha.hhh.org;
root /webdir/hhh.org/hhh/html;
location /vars {
echo $remote_user;
echo $request_method;
echo $request_filename;
echo $request_uri;
echo $scheme;
}
location /break {
rewrite /break/(.*) /hhhh/$1 last;
rewrite /hhhh /haha;
}
location /hhhh {
return 200 "hhhh end page";
}
location /haha {
return 200 "HAHA END PAGE";
}
}
[root@nginx ~]# nginx -s reload

5.3 全站加密
bash
[root@nginx ~]# mkdir /usr/local/nginx/certs
[root@nginx ~]# openssl req -newkey rsa:2048 -nodes -sha256 -keyout /usr/local/nginx/certs/hhh.org.key -x509 -days 365 -out /usr/local/nginx/certs/hhh.org.crt
...+.........+.+........+.......+.....+.+.........+...+..+....+..+.......+...+.....+.........+....+.....+.+......+..+...+...+.+......+..+.+...........+...+.+.....+................+...+...........+......+.+.........+...+......+..+....+......+..+.+..+...+.+......+...+..............+..........+++++++++++++++++++++++++++++++++++++++*...+......+.+...+.....+...+...+..
[root@nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name haha.hhh.org;
ssl_certificate /usr/local/nginx/certs/hhh.org.crt;
ssl_certificate_key /usr/local/nginx/certs/hhh.org.key;
ssl_session_cache shared:sslcache:20m;
ssl_session_timeout 10m;
server_name lee.timinglee.org;
root /webdir/hhh.org/hhh/html;
location / {
if ($scheme = http ){
rewrite /(.*) https://$host/$1 redirect;
}
}
}
[root@nginx ~]# systemctl restart nginx.service

5.4 防盗锁
6 Nginx****反向代理功能
6.1反向代理定义与模式
反向代理:reverse proxy,指的是代理外网用户的请求到内部的指定的服务器,并将数据返回给用户的 一种方式,这是用的比较多的一种方式。


逻辑图
同构代理:用户不需要其他程序的参与,直接通过http协议或者tcp协议访问后端服务器。
异构代理:用户访问的资源时需要经过处理后才能返回的,比如php,Python,等等,这种访问资源需 要经过处理才能被访问。

6.2 反向代理实现与配置参数
6.2.1 实验环境
bash
[root@RS1 ~]# dnf install httpd -y
[root@RS1 ~]# systemctl enable --now httpd
[root@RS1 ~]# echo 172.25.254.10 > /var/www/html/index.html
[root@RS2 ~]# dnf install httpd -y
[root@RS2 ~]# systemctl enable --now httpd
[root@RS2 ~]# echo 172.25.254.20 > /var/www/html/index.html

6.2.2 简单代理
bash
[root@RS2 ~]# mkdir /var/www/html/web
[root@RS2 ~]# echo 192.168.83.20 web > /var/www/html/web/index.html
[root@nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name haha.hhh.org;
location / {
proxy_pass http://192.168.83.10:80;
}
location /web {
proxy_pass http://192.168.83.20:80;
}
}
[root@Nginx ~]# nginx -s reload

6.2.3 proxy_hide_header filed
bash
[root@Nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name haha.hhh.org;
location / {
proxy_pass http://192.168.83.10:80;
proxy_hide_header ETag;
}
location /web {
proxy_pass http://192.168.83.20:80;
}
}
[root@Nginx ~]# nginx -s reload

6.2.4 proxy_pass_header
默认访问不透传server信息,开启并查看透传信息
bash
[root@Nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name haha.hhh.org;
location / {
proxy_pass http://192.168.83.10:80;
proxy_pass_header Server;
}
location /web {
proxy_pass http://192.168.83.20:80;
}
}
[root@Nginx ~]# nginx -s reload

6.2.5 透传信息
bash
[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 haha.hhh.org;
location / {
proxy_pass http://192.168.83.10:80;
proxy_set_header X-Forwarded-For $remote_addr;
}
location /web {
proxy_pass http://192.168.83.20:80;
}
}
[root@Nginx ~]# nginx -s reload


6.3 利用反向代理实现动静分离
bash
[root@RS1 ~]# dnf install php -y
[root@RS1 ~]# systemctl restart httpd
[root@RS1 ~]# vim /var/www/html/index.php
<?php
echo "<h2>192.168.83.10</h2>";
phpinfo();
?>
[root@nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name haha.hhh.org;
location / {
proxy_pass http://192.168.83.20:80;
}
location ~* \.(php|js)$ {
proxy_pass http://192.168.83.10:80;
}
}
[root@Nginx ~]# nginx -s reload


6.4 缓存加速
没有设定前

设定:
bash
[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 haha.hhh.org;
location / {
proxy_pass http://192.168.83.20:80;
}
location ~* \.(php|js)$ {
proxy_pass http://192.168.83.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
设定后面


6.5 反向代理负载均衡
bash
[root@Nginx ~]# mkdir /usr/local/nginx/conf/upstream/
[root@Nginx ~]# vim /usr/local/nginx/conf/nginx.conf
events {
worker_connections 10000;
use epoll;
accept_mutex on;
multi_accept on;
}
http {
include mime.types;
default_type application/octet-stream;
include "/usr/local/nginx/conf/upstream/*.conf";
[root@Nginx ~]# vim /usr/local/nginx/conf/upstream/www.conf
upstream webserver {
server 192.168.83.10:80 weight=1 fail_timeout=15s max_fails=3;
server 192.168.83.20:80 weight=1 fail_timeout=15s max_fails=3;
server 192.168.83.100:8888 backup;
}
server {
listen 80;
server_name www.hhh.org;
location ~ / {
proxy_pass http://webserver;
}
}
[root@Nginx ~]# mkdir /webdir/hhh.org/error/html -p
[root@Nginx ~]# echo error > /webdir/hhh.org/error/html/index.html
[root@Nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 8888;
root /webdir/hhh.org/error/html;
}

6.6 负载均衡算法
ip_hash:
源地址hash调度方法,基于的客户端的remote_addr(源地址IPv4的前24位或整个IPv6地址)做hash计 算,以实现会话保持。
least_conn:
最少连接调度算法,优先将客户端请求调度到当前连接最少的后端服务器,相当于LVS中的WLC。
hash cookie_sessionid :
基于cookie中的sessionid这个key进行hash调度,实现会话绑定。
hash request_uri consistent:
基于用户请求的uri做hash。
hash KEY [consistent]:
#基于指定请求报文中首部字段或者URI等key做hash计算,使用consistent参数,将使用ketama一致性 hash算法,适用于后端是Cache服务器(如varnish)时使用,consistent定义使用一致性hash运算,一致性hash基于取模运算。
bash
[root@Nginx ~]# vim /usr/local/nginx/conf/upstream/loadbalance.conf
upstream webserver {
#ip_hash;
#hash $request_uri consistent;
#least_conn;
hash $cookie_lee;
server 192.168.83.10:80 weight=1 fail_timeout=15s max_fails=3;
server 192.168.83.20:80 weight=1 fail_timeout=15s max_fails=3;
#server 192.168.83.100:8888 backup;
}
server {
listen 80;
server_name www.hhh.org;
location ~ / {
proxy_pass http://webserver;
}
}

6.7 cgi简介
最早的Web服务器只能简单地响应浏览器发来的HTTP请求,并将存储在服务器上的HTML文件返回给浏览器,也就是静态HTML文件,但是后期随着网站功能增多网站开发也越来越复杂,以至于出现动态技术,比如像php(1995年)、Java(1995)、Python(1991)语言开发的网站,nginx/apache服务器并不能直接运行 php、Java这样的文件,apache实现的方式是打补丁,但是nginx缺通过与第三方基于协议实现,即通过某种特定协议将客户端请求转发给第三方服务处理,第三方服务器会新建新的进程处理用户的请求,处理完成后返回数据给Nginx并回收进程,最后nginx在返回给客户端,那这个约定就是通用网关接口(common gateway interface,简称CGI),CGI(协议) 是web服务器和外部应用程序之间的接口标准,是cgi程序和web服务器之间传递信息的标准化接口。

CGI协议虽然解决了语言解析器和 Web Server 之间通讯的问题,但是它的效率很低,因为 Web Server 每收到一个请求都会创建一个CGI进程,PHP解析器都会解析php.ini文件,初始化环境,请求结束的时候再关闭进程,对于每一个创建的CGI进程都会执行这些操作,所以效率很低,而FastCGI是用来提高CGI性能的,FastCGI每次处理完请求之后不会关闭掉进程,而是保留这个进程,使这个进程可以处理多个请求。这样的话每个请求都不用再重新创建一个进程了,大大提升了处理效率。
6.8 php源码编译与安装
6.8.1 下载源码包
bash
[root@Nginx ~]# wget https://www.php.net/distributions/php-8.3.30.tar.gz
6.8.2 解压
bash
[root@Nginx ~]# tar zxf php-8.3.30.tar.gz
[root@Nginx ~]# ls
anaconda-ks.cfg lee.png nginx-1.29.4.tar.gz test.c
daolian.png nginx-1.28.1 php-8.3.30
echo-nginx-module-0.64 nginx-1.28.1.tar.gz php-8.3.30.tar.gz
echo-nginx-module-0.64.tar.gz nginx-1.29.4
6.8.3 源码编译
bash
[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 ~]# 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 ~]# 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 \ #用cgi方式启动程序
--with-fpm-user=nginx \ #指定运行用户身份
--with-fpm-group=nginx \
--with-curl \ #打开curl浏览器支持
--with-iconv \ #启用iconv函数,转换字符编码
--with-mhash \ #mhash加密方式扩展库
--with-zlib \ #支持zlib库,用于压缩http压缩传输
--with-openssl \ #支持ssl加密
--enable-mysqlnd \ #mysql数据库
--with-mysqli \
--with-pdo-mysql \
--disable-debug \ #关闭debug功能
--enable-sockets \ #支持套接字访问
--enable-soap \ #支持soap扩展协议
--enable-xml \ #支持xml
--enable-ftp \ #支持ftp
--enable-gd \ #支持gd库
--enable-exif \ #支持图片元数据
--enable-mbstring \ #支持多字节字符串
--enable-bcmath \ #打开图片大小调整,用到zabbix监控的时候用到了这个模块
--with-fpm-systemd #支持systemctl 管理cgi
[root@Nginx php-8.3.30]# make && make instsall
6.8.4 配置PHP
bash
[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
41 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
989 date.timezone = Asia/Shangha
[root@Nginx ~]# cp /root/php-8.3.30/sapi/fpm/php-fpm.service /lib/systemd/system/
[root@Nginx ~]# 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 ~]# systemctl daemon-reload
[root@Nginx ~]# systemctl enable --now php-fpm

6.9 nginx整合php与php环境变量设定
6.9.1 php环境变量设定
bash
[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

6.9.2 nginx整合php
bash
[root@Nginx ~]# mkdir /webdir/hhh.org/php/html -p
[root@Nginx ~]# vim /webdir/hhh.org/php/html/index.html
php.hhh.org
[root@Nginx ~]# vim /webdir/hhh.org/php/html/index.php
<?php
phpinfo();
?>
[root@Nginx ~]# cd /usr/local/nginx/conf/conf.d/
[root@Nginx conf.d]# vim php.conf
server {
listen 80;
server_name php.hhh.org;
root /webdir/hhh.org/php/html;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
}
[root@Nginx conf.d]# nginx -s reload


6.10 memcache缓存加速****与加速效果测试
6.10.1 安装 memcache
bash
[root@Nginx ~]# dnf install memcached.x86_64 -y
6.10.2 配置 memcache
bash
[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 0.0.0.0:11211 0.0.0.0:* LISTEN 991 543443 154628/memcachd
tcp6 0 0 ::1:11211 :::* LISTEN 991 543444 154628/memcachd
6.10.3 升级php对于memcache的支持
bash
[root@Nginx ~]# tar zxf memcache-8.2.tgz
[root@Nginx ~]# cd memcache-8.2/
[root@Nginx memcache-8.2]# dnf install autoconf -y
[root@Nginx memcache-8.2]# phpize
[root@Nginx memcache-8.2]# ./configure && make && make install
[root@Nginx memcache-8.2]# ls /usr/local/php/lib/php/extensions/no-debug-non-zts-20230831/
memcache.so opcache.so
[root@Nginx memcache-8.2]# vim /usr/local/php/etc/php.ini
939 extension=memcache
[root@Nginx memcache-8.2]# systemctl restart php-fpm.service

6.10.4 性能测试
bash
[root@Nginx memcache-8.2]# vim memcache.php
define('ADMIN_USERNAME','hhh'); // Admin Username
define('ADMIN_PASSWORD','haha'); // Admin Password
$MEMCACHE_SERVERS[] = '192.168.83.100:11211'; // add more as an array
#$MEMCACHE_SERVERS[] = 'mymemcache-server2:11211'; // add more as an array
[root@Nginx memcache-8.2]# cp -p memcache.php /webdir/hhh.org/php/html/
[root@Nginx memcache-8.2]# cp -p example.php /webdir/hhh.org/php/html/

6.11 memcache前置加速
6.11.1 重新编译nginx
bash
[root@Nginx ~]# systemctl stop nginx.service
[root@Nginx ~]# cp /usr/local/nginx/conf/ /mnt/ -r
[root@Nginx ~]# rm -fr /usr/local/nginx/
[root@Nginx ~]# rm -rf nginx-1.29.4 nginx-1.28.1
[root@Nginx ~]# tar zxf nginx-1.28.1.tar.gz
[root@Nginx ~]# tar zxf srcache-nginx-module-0.33.tar.gz
[root@Nginx ~]# tar zxf memc-nginx-module-0.20.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 nginx-1.28.1]# make && make install
[root@Nginx ~]# cd /usr/local/nginx/conf
[root@Nginx conf]# rm -fr nginx.conf
[root@Nginx conf]# cp /mnt/conf/nginx.conf /mnt/conf/conf.d/ . -r
[root@Nginx conf]# systemctl start nginx.service
6.11.2 整合memcache
bash
[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.hhh.org;
root /webdir/hhh.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]# nginx -s reload
#记得添加本地解析

6.12 四层负载均衡代理
6.12.1 实验环境(MySQL)
bash
[root@RS1 ~]# dnf install mariadb-server -y
[root@RS2 ~]# dnf install mariadb-server -y
[root@RS1 ~]# vim /etc/my.cnf.d/mariadb-server.cnf
server-id=10
[root@RS2 ~]# vim /etc/my.cnf.d/mariadb-server.cnf
server-id=20
[root@RS1 ~]# systemctl enable --now mariadb
[root@RS2 ~]# systemctl enable --now mariadb
[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 hhh@'%' IDENTIFIED BY 'hhh';
Query OK, 0 rows affected (0.001 sec)
MariaDB [(none)]> GRANT ALL ON *.* TO hhh@'%';
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 hhh@'%' IDENTIFIED BY 'hhh';
Query OK, 0 rows affected (0.001 sec)
MariaDB [(none)]> GRANT ALL ON *.* TO hhh@'%';
Query OK, 0 rows affected (0.001 sec)
6.12.2 实验环境(dns)
bash
[root@RS1 ~]# 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; };
dnssec-validation no;
[root@RS2 ~]# dnf install bind -y
[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
[root@RS2 ~]# vim /etc/named.rfc1912.zones
zone "hhh.org" IN {
type master;
file "hhh.org.zone";
allow-update { none; };
};
[root@RS1 ~]# cd /var/named/
[root@RS1 named]# cp -p named.localhost hhh.org.zone
[root@RS2 ~]# cd /var/named/
[root@RS2 named]# cp -p named.localhost hhh.org.zone
[root@RS1 named]# vim hhh.org.zone
$TTL 1D
@ IN SOA dns.hhh.org. rname.invalid. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS dns.hhh.org.
dns A 192.168.83.10
[root@RS1 named]# systemctl enable --now named
[root@RS2 named]# vim hhh.org.zone
$TTL 1D
@ IN SOA dns.hhh.org. rname.invalid. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS dns.hhh.org.
dns A 192.168.83.20
[root@RS2 named]# systemctl enable --now named

6.12.3 tcp四层负载
bash
[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 192.168.83.10:3306 max_fails=3 fail_timeout=30s;
server 192.168.83.20:3306 max_fails=3 fail_timeout=30s;
}
server {
listen 192.168.83.100:3306;
proxy_pass mysql_server;
proxy_connect_timeout 30s;
proxy_timeout 300s;
}
}
[root@Nginx conf]# nginx -s reload

6.12.4 udp四层负载
bash
[root@nginx conf]# vim /usr/local/nginx/conf/tcp/mariadb.conf
stream {
upstream mysql_server {
server 192.168.83.10:3306 max_fails=3 fail_timeout=30s;
server 192.168.83.20:3306 max_fails=3 fail_timeout=30s;
}
upstream dns_server{
server 192.168.83.10:53 max_fails=3 fail_timeout=30s;
server 192.168.83.20:53 max_fails=3 fail_timeout=30s;
}
server {
listen 192.168.83.100:3306;
proxy_pass mysql_server;
proxy_connect_timeout 30s;
proxy_timeout 300s;
}
server {
listen 192.168.83.100:53 udp;
proxy_pass dns_server;
proxy_timeout 1s;
proxy_responses 1;
error_log logs/dns.log;
}
}
[root@Nginx ~]# nginx -s reload

6.13 openresty源码编译
bash
[root@Nginx src]#wget https://openresty.org/download/openresty-1.27.1.2.tar.gz
[root@Nginx ~]#dnf -yq install gcc pcre-devel openssl-devel perl zlib-devel
[root@Nginx ~]#useradd -r -s /sbin/nologin nginx
[root@Nginx ~]#tar zxf openresty-1.27.1.2
[root@webserver ~]# cd openresty-1.27.1.2/
[root@Nginx openresty-1.17.8.2]#./configure \
--prefix=/apps/openresty \
--user=nginx --group=nginx \
--with-http_ssl_module \
--with-http_v2_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 openresty-1.17.8.2]#gmake && gmake install
[root@webserver openresty]# vim ~/.bash_profile
export PATH=$PATH:/usr/local/openresty/bin
source ~/.bash_profile
[root@Nginx openresty-1.17.8.2]#openresty -v
nginx version: openresty/1.17.8.2
[root@Nginx openresty-1.17.8.2]#openresty