网页建站点,网站建设背景需要写些什么,昌吉北京网站建设,企业网站开发价格文章目录 Apache经典的web服务端Apache prefork 模型Apache work 模型#xff08;适应市场#xff09;Apache event 模型 网络I/O网络I/O模型I/O模型网络I/O模型 Nginx架构和安装Nginx源码编译环境准备安装nginx Nginx的平滑升级及版本回滚 Nginx架构和进程Nginx进程结构Ngin… 文章目录 Apache经典的web服务端Apache prefork 模型Apache work 模型适应市场Apache event 模型 网络I/O网络I/O模型I/O模型网络I/O模型 Nginx架构和安装Nginx源码编译环境准备安装nginx Nginx的平滑升级及版本回滚 Nginx架构和进程Nginx进程结构Nginx进程间通信Nginx启动和HTTP建立连接 Nginx命令常用参数Nginx服务的启动脚本Nginx核心配置参数配置文件说明全局配置root和aliaslocation的详细介绍Nginx下的用户认证Nginx自定义错误页面Nginx下的自定义日志Nginx下的文件检测Nginx长链接控制Nginx下载服务器 Nginx高级配置Nginx状态页Nginx的数据压缩功能Nginx中的变量Rewrite相关功能全站加密防盗链 Nginx反向代理及动静分离实现Nginx反向代理的缓存功能Nginx的反向代理负载均衡四层负载均衡 实现FastCGIFastCGI介绍FastCGI配置指令FastCGI实战案例php的动态扩展模块php的缓存模块php高速缓存 nginx二次开发版本 Apache经典的web服务端
Apache prefork 模型 预派生模式有一个主控制进程然后生成多个子进程使用select模型最大并发1024 每个子进程有一个独立的线程响应用户请求 相对比较占用内存但是比较稳定可以设置最大和最小进程数 是最古老的一种模式,也是最稳定的模式适用于访问量不是很大的场景
优点稳定
缺点每个用户请求需要对应开启一个进程,占用资源较多并发性差,不适用于高并发场景 Apache work 模型适应市场 一种多进程和多线程混合的模型 有一个控制进程启动多个子进程 每个子进程里面包含固定的线程 使用线程程来处理请求 当线程不够使用的时候会再启动一个新的子进程,然后在进程里面再启动线程处理请求 由于其使用了线程处理请求因此可以承受更高的并发
优点相比prefork 占用的内存较少可以同时处理更多的请求
缺点使用keepalive的长连接方式某个线程会一直被占据即使没有传输数据也需要一直等待到超时才会被释放。如果过多的线程被这样占据也会导致在高并发场景下的无服务线程可用该问题在prefork模式下同样会发生 Apache event 模型
Apache中最新的模式2012年发布的apache 2.4.X系列正式支持event 模型,属于事件驱动模型(epoll)
每个进程响应多个请求在现在版本里的已经是稳定可用的模式
它和worker模式很像最大的区别在于它解决了keepalive场景下长期被占用的线程的资源浪费问题某些线程因为被keepalive空挂在哪里等待中间几乎没有请求过来甚至等到超时
event MPM中会有一个专门的线程来管理这些keepalive类型的线程
当有真实请求过来的时候将请求传递给服务线程执行完毕后又允许它释放。这样增强了高并发场景下的请求处理能力
优点单线程响应多请求占据更少的内存高并发下表现更优秀会有一个专门的线程来管理keepalive类型的线程当有真实请求过来的时候将请求传递给服务线程执行完毕后又允许它释放
缺点没有线程安全控制 网络I/O 一个进程启动会在内存中开辟相应的内存空间
内存空间用于进程存储数据使用所以直接将Data拷贝到目标应用的内存之中则完成了I/O流程。
网络I/O读取原理 网卡(接收到data --- 内核 --- 应用程序读取应用程序无法直接访问计算机的硬件所以中间会加上一个内核可理解为中间件可通过socket接口去调用内核内核去检查网卡找数据应用程序和内核在运行的过程中相当于两个进程。那么在计算机的内存中都会开辟相应的空间来存储数据内核准备好数据在内存中直接拷贝到应用程序的内存空间则此应用程序成功接收到了相应数据。网络I/O模型
I/O模型
同步/异步
同步/异步关注的是消息通信机制即调用者在等待一件事情的处理结果时被调用者是否提供完成状态的通知。 同步synchronous被调用者并不提供事件的处理结果相关的通知消息需要调用者主动询问事情是否处理完成 异步asynchronous被调用者通过状态、通知或回调机制主动通知调用者被调用者的运行状态 阻塞/非阻塞
阻塞/非阻塞关注调用者在等待结果返回之前所处的状态 阻塞blocking指IO操作需要彻底完成后才返回到用户空间调用结果返回之前调用者被挂起干不了别的事情。 非阻塞nonblocking指IO操作被调用后立即返回给用户一个状态值而无需等到IO操作彻底完成在最终的调用结果返回之前调用者不会被挂起可以去做别的事情。 网络I/O模型
阻塞依次减小阻塞型、非阻塞型、复用型、信号驱动型、异步
阻塞型I/O 模型blocking IO 阻塞IO模型是最简单的I/O模型用户线程在内核进行IO操作时被阻塞 用户线程通过系统调用read发起I/O读操作由用户空间转到内核空间。内核等到数据包到达后然后将接收的数据拷贝到用户空间完成read操作 用户需要等待read将数据读取到buffer后才继续处理接收的数据。整个I/O请求的过程中用户线程是被阻塞的这导致用户在发起IO请求时不能做任何事情对CPU的资源利用率不够
优点程序简单在阻塞等待数据期间进程/线程挂起基本不会占用 CPU 资源
缺点每个连接需要独立的进程/线程单独处理当并发请求量大时为了维护程序内存、线程切换开销
较apache 的preforck使用的是这种模式。
同步阻塞程序向内核发送I/O请求后一直等待内核响应如果内核处理请求的IO操作不能立即返回,则进程将一直等待并不再接受新的请求并由进程轮询查看I/O是否完成完成后进程将I/O结果返回给Client在IO没有返回期间进程不能接受其他客户的请求而且是有进程自己去查看I/O是否完成这种方式简单但是比较慢用的比较少。
非阻塞型I/Ononblocking IO 用户线程发起IO请求时立即返回。但并未读取到任何数据用户线程需要不断地发起IO请求直到数据到达后才真正读取到数据继续执行。即 “轮询”机制存在两个问题如果有大量文件描述符都要等那么就得一个一个的read。这会带来大量的Context Switchread是系统调用每调用一次就得在用户态和核心态切换一次。轮询的时间不好把握。这里是要猜多久之后数据才能到。等待时间设的太长程序响应延迟就过大;设的太短就会造成过于频繁的重试干耗CPU而已是比较浪费CPU的方式一般很少直接使用这种模型而是在其他IO模型中使用非阻塞IO这一特性。
非阻塞程序向内核发送请I/O求后一直等待内核响应如果内核处理请求的IO操作不能立即返回IO结果进程将不再等待而且继续处理其他请求但是仍然需要进程隔一段时间就要查看内核I/O是否完成。
查看上图可知在设置连接为非阻塞时当应用进程系统调用 recvfrom 没有数据返回时内核会立即返回一个 EWOULDBLOCK 错误而不会一直阻塞到数据准备好。如上图在第四次调用时有一个数据报准备好了所以这时数据会被复制到 应用进程缓冲区 于是 recvfrom 成功返回数据
当一个应用进程这样循环调用 recvfrom 时称之为轮询 polling 。这么做往往会耗费大量CPU时间实际使用很少
多路复用I/O型I/O multiplexing
上面的模型中,每一个文件描述符对应的IO是由一个线程监控和处理
多路复用IO指一个线程可以同时实际是交替实现即并发完成监控和处理多个文件描述符对应各自的IO即复用同一个线程
一个线程之所以能实现同时处理多个IO,是因为这个线程调用了内核中的SELECT,POLL或EPOLL等系统调用从而实现多路复用IO I/O multiplexing 主要包括:selectpollepoll三种系统调用select/poll/epoll的好处就在于单个process就可以同时处理多个网络连接的IO。
它的基本原理就是select/poll/epoll这个function会不断的轮询所负责的所有socket当某个socket有数据到达了就通知用户进程。
当用户进程调用了select那么整个进程会被block而同时kernel会“监视”所有select负责的socket当任何一个socket中的数据准备好了select就会返回。这个时候用户进程再调用read操作将数据从kernel拷贝到用户进程。
Apache prefork是此模式的selectworker是poll模式。
IO多路复用IO Multiplexing) 是一种机制程序注册一组socket文件描述符给操作系统表示“我要
监视这些fd是否有IO事件发生有了就告诉程序处理”IO多路复用一般和NIO一起使用的。NIO和IO多路复用是相对独立的。NIO仅仅是指IO API总是能立刻返回不会被Blocking;而IO多路复用仅仅是操作系提供的一种便利的通知机制。操作系统并不会强制这俩必须得一起用可以只用IO多路复用 BIO这时还是当前线程被卡住。IO多路复用和NIO是要配合一起使用才有实际意义
IO多路复用是指内核一旦发现进程指定的一个或者多个IO条件准备读取就通知该进程多个连接共用一个等待机制本模型会阻塞进程但是进程是阻塞在select或者poll这两个系统调用上而不是阻塞在真正的IO操作上用户首先将需要进行IO操作添加到select中同时等待select系统调用返回。当数据到达时IO被激活select函数返回。用户线程正式发起read请求读取数据并继续执行从流程上来看使用select函数进行IO请求和同步阻塞模型没有太大的区别甚至还多了添加监视IO以及调用select函数的额外操作效率更差。并且阻塞了两次但是第一次阻塞在select上时select可以监控多个IO上是否已有IO操作准备就绪即可达到在同一个线程内同时处理多个IO请求的目的。而不像阻塞IO那种一次只能监控一个IO虽然上述方式允许单线程内处理多个IO请求但是每个IO请求的过程还是阻塞的在select函数上阻塞平均时间甚至比同步阻塞IO模型还要长。如果用户线程只是注册自己需要的IO请求然后去做自己的事情等到数据到来时再进行处理则可以提高CPU的利用率IO多路复用是最常使用的IO模型但是其异步程度还不够“彻底”因它使用了会阻塞线程的select系统调用。因此IO多路复用只能称为异步阻塞IO模型而非真正的异步IO
优缺点
优点可以基于一个阻塞对象同时在多个描述符上等待就绪而不是使用多个线程(每个文件描述符一个线程)这样可以大大节省系统资源缺点当连接数较少时效率相比多线程阻塞 I/O 模型效率较低可能延迟更大因为单个连接处理需要 2 次系统调用占用时间会有增加
IO多路复用适用如下场合 当客户端处理多个描述符时一般是交互式输入和网络套接口必须使用I/O复用 当一个客户端同时处理多个套接字时此情况可能的但很少出现 当一个服务器既要处理监听套接字又要处理已连接套接字一般也要用到I/O复用 当一个服务器即要处理TCP又要处理UDP一般要使用I/O复用 当一个服务器要处理多个服务或多个协议一般要使用I/O复用
信号驱动式I/O模型 (signal-driven IO) 信号驱动I/O的意思就是进程现在不用傻等着也不用去轮询。而是让内核在数据就绪时发送信号通知进程。
调用的步骤是通过系统调用 sigaction 并注册一个信号处理的回调函数该调用会立即返回然后主程序可以继续向下执行当有I/O操作准备就绪,即内核数据就绪时内核会为该进程产生一个 SIGIO信号并回调注册的信号回调函数这样就可以在信号回调函数中系统调用 recvfrom 获取数据,将用户进程所需要的数据从内核空间拷贝到用户空间
此模型的优势在于等待数据报到达期间进程不被阻塞。用户主程序可以继续执行只要等待来自信号处理函数的通知。
在信号驱动式 I/O 模型中应用程序使用套接口进行信号驱动 I/O并安装一个信号处理函数进程继续运行并不阻塞
在信号驱动式 I/O 模型中应用程序使用套接口进行信号驱动 I/O并安装一个信号处理函数进程继续运行并不阻塞
当数据准备好时进程会收到一个 SIGIO 信号可以在信号处理函数中调用 I/O 操作函数处理数据。
优点线程并没有在等待数据时被阻塞内核直接返回调用接收信号不影响进程继续处理其他请求因此可以提高资源的利用率
缺点信号 I/O 在大量 IO 操作时可能会因为信号队列溢出导致没法通知
异步阻塞程序进程向内核发送IO调用后不用等待内核响应可以继续接受其他请求内核收到进程请求后进行的IO如果不能立即返回就由内核等待结果直到IO完成后内核再通知进程
异步 I/O 模型(asynchronous IO) 异步I/O 与 信号驱动I/O最大区别在于信号驱动是内核通知用户进程何时开始一个I/O操作而异步I/O是由内核通知用户进程I/O操作何时完成两者有本质区别,相当于不用去饭店场吃饭直接点个外卖把等待上菜的时间也给省了
相对于同步I/O异步I/O不是顺序执行。用户进程进行aio_read系统调用之后无论内核数据是否准备好都会直接返回给用户进程然后用户态进程可以去做别的事情。等到socket数据准备好了内核直接复制数据给进程然后从内核向进程发送通知。IO两个阶段进程都是非阻塞的。
信号驱动IO当内核通知触发信号处理程序时信号处理程序还需要阻塞在从内核空间缓冲区拷贝数据到用户空间缓冲区这个阶段而异步IO直接是在第二个阶段完成后内核直接通知用户线程可以进行后续操作了
优点异步 I/O 能够充分利用 DMA 特性让 I/O 操作与计算重叠
缺点要实现真正的异步 I/O操作系统需要做大量的工作。目前 Windows 下通过 IOCP 实现了真正的异步 I/O在 Linux 系统下Linux 2.6才引入目前 AIO 并不完善因此在 Linux 下实现高并发网络编程时以 IO 复用模型模式多线程任务的架构基本可以满足需求
Linux提供了AIO库函数实现异步但是用的很少。目前有很多开源的异步IO库例如libevent、libev、libuv。
异步非阻塞程序进程向内核发送IO调用后不用等待内核响应可以继续接受其他请求内核调用的IO如果不能立即返回内核会继续处理其他事物直到IO完成后将结果通知给内核内核在将IO完成的结果返回给进程期间进程可以接受新的请求内核也可以处理新的事物因此相互不影响可以实现较大的同时并实现较高的IO复用因此异步非阻塞使用最多的一种通信方式。
五种IO对比
这五种 I/O 模型中越往后阻塞越少理论上效率也是最优前四种属于同步 I/O因为其中真正的 I/O操作(recvfrom)将阻塞进程/线程只有异步 I/O 模型才与 POSIX 定义的异步 I/O 相匹配 Nginx架构和安装
Nginx源码编译
环境准备
从RHEL9.4母盘中克隆出一台新的虚拟机网络默认NAT模式
Nginx eth0 172.25.254.100 nginx-node1.exmaple.com
安装nginx
官网下载链接https://nginx.org/en/download.html
选择稳定版复制链接在主机中用wget命令下载
[rootnginx-node1 ~]# wget https://nginx.org/download/nginx-1.26.2.tar.gz
--2024-08-16 10:12:07-- https://nginx.org/download/nginx-1.26.2.tar.gz
Resolving nginx.org (nginx.org)... 3.125.197.172, 52.58.199.22, 2a05:d014:5c0:2601::6, ...
Connecting to nginx.org (nginx.org)|3.125.197.172|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1244789 (1.2M) [application/octet-stream]
Saving to: ‘nginx-1.26.2.tar.gz’nginx-1.26.2.tar.gz 100%[] 1.19M 563KB/s in 2.2s 2024-08-16 10:12:10 (563 KB/s) - ‘nginx-1.26.2.tar.gz’ saved [1244789/1244789][rootnginx-node1 ~]# ll
total 2308
-rw-------. 1 root root 992 Aug 4 00:54 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Desktop
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Documents
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Downloads
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Music
-rw-r--r-- 1 root root 1112471 Aug 16 09:17 nginx-1.24.0.tar.gz
-rw-r--r-- 1 root root 1244789 Aug 13 00:39 nginx-1.26.2.tar.gz
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Pictures
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Public
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Templates
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Videos# 解压源码包
[rootnginx-node1 ~]# tar zxf nginx-1.24.0.tar.gz
[rootnginx-node1 ~]# ll
total 2308
-rw-------. 1 root root 992 Aug 4 00:54 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Desktop
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Documents
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Downloads
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Music
drwxr-xr-x 8 1001 1001 158 Apr 11 2023 nginx-1.24.0
-rw-r--r-- 1 root root 1112471 Aug 16 09:17 nginx-1.24.0.tar.gz
-rw-r--r-- 1 root root 1244789 Aug 13 00:39 nginx-1.26.2.tar.gz
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Pictures
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Public
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Templates
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Videos[rootnginx-node1 nginx-1.24.0]# ll
total 812
drwxr-xr-x 6 1001 1001 326 Aug 16 10:16 auto
-rw-r--r-- 1 1001 1001 323312 Apr 11 2023 CHANGES
-rw-r--r-- 1 1001 1001 494234 Apr 11 2023 CHANGES.ru
drwxr-xr-x 2 1001 1001 168 Aug 16 10:16 conf
-rwxr-xr-x 1 1001 1001 2611 Apr 11 2023 configure #检测环境
drwxr-xr-x 4 1001 1001 72 Aug 16 10:16 contrib
drwxr-xr-x 2 1001 1001 40 Aug 16 10:16 html
-rw-r--r-- 1 1001 1001 1397 Apr 11 2023 LICENSE
drwxr-xr-x 2 1001 1001 21 Aug 16 10:16 man
-rw-r--r-- 1 1001 1001 49 Apr 11 2023 README
drwxr-xr-x 9 1001 1001 91 Aug 16 10:16 src# 检测当前的环境是否适合当前的选择
[rootnginx-node1 nginx-1.24.0]# ./configure --prefix/usr/local/nginx \--usernginx \--groupnginx \--with-http_ssl_module \--with-http_v2_module \--with-http_realip_module \--with-http_gzip_static_module \--with-http_stub_status_module \--with-pcre \--with-stream \--with-stream_ssl_module
checking for OS Linux 5.14.0-427.13.1.el9_4.x86_64 x86_64
checking for C compiler ... not found./configure: error: C compiler cc is not found# 此时会报错因为没有安装C语言的编译器所以我们需要下载gcc
[rootnginx-node1 nginx-1.24.0]# dnf install gcc -y# 安装完之后再执行检测环境
[rootnginx-node1 nginx-1.24.0]# ./configure --prefix/usr/local/nginx --usernginx --groupnginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_gzip_static_module --with-http_stub_status_module --with-pcre --with-stream --with-stream_ssl_module # 报错继续安装pcre开发包
[rootnginx-node1 nginx-1.24.0]# dnf search pcre
Updating Subscription Management repositories.
Unable to read consumer identityThis system is not registered with an entitlement server. You can use rhc or subscription-manager to register.Last metadata expiration check: 0:32:37 ago on Fri 16 Aug 2024 10:50:42 AM CST.Name Exactly Matched: pcre
pcre.x86_64 : Perl-compatible regular expression library
pcre.i686 : Perl-compatible regular expression libraryName Summary Matched: pcre
pcre-cpp.i686 : C bindings for PCRE
pcre-cpp.x86_64 : C bindings for PCRE
pcre-devel.i686 : Development files for pcre
pcre-devel.x86_64 : Development files for pcre
pcre-utf16.i686 : UTF-16 variant of PCRE
pcre-utf16.x86_64 : UTF-16 variant of PCRE
pcre-utf32.i686 : UTF-32 variant of PCRE
pcre-utf32.x86_64 : UTF-32 variant of PCRE
pcre2-devel.i686 : Development files for pcre2
pcre2-devel.x86_64 : Development files for pcre2
pcre2-syntax.noarch : Documentation for PCRE2 regular expressions
pcre2-utf16.i686 : UTF-16 variant of PCRE2
pcre2-utf16.x86_64 : UTF-16 variant of PCRE2
pcre2-utf32.x86_64 : UTF-32 variant of PCRE2
pcre2-utf32.i686 : UTF-32 variant of PCRE2
postfix-pcre.x86_64 : Postfix PCRE map supportName Matched: pcre
pcre2.x86_64 : Perl-compatible regular expression library
pcre2.i686 : Perl-compatible regular expression library[rootnginx-node1 nginx-1.24.0]# dnf install pcre-devel.x86_64 -y
安装完之后继续执行configure检测环境 [rootnginx-node1 nginx-1.24.0]# dnf search openssl
Updating Subscription Management repositories.
Unable to read consumer identityThis system is not registered with an entitlement server. You can use rhc or subscription-manager to register.Last metadata expiration check: 0:35:35 ago on Fri 16 Aug 2024 10:50:42 AM CST.Name Exactly Matched: openssl
openssl.x86_64 : Utilities from the general purpose cryptography library with TLS: implementationName Summary Matched: openssl
apr-util-openssl.x86_64 : APR utility library OpenSSL crypto support
openssl-devel.i686 : Files for development of applications which will use OpenSSL
openssl-devel.x86_64 : Files for development of applications which will use OpenSSL
openssl-fips-provider.x86_64 : FIPS module for OpenSSL
openssl-fips-provider.i686 : FIPS module for OpenSSL
openssl-perl.x86_64 : Perl scripts provided with OpenSSL
openssl-pkcs11.i686 : A PKCS#11 engine for use with OpenSSL
openssl-pkcs11.x86_64 : A PKCS#11 engine for use with OpenSSL
perl-Crypt-OpenSSL-Bignum.x86_64 : Perl interface to OpenSSL for Bignum
perl-Crypt-OpenSSL-RSA.x86_64 : Perl interface to OpenSSL for RSA
perl-Crypt-OpenSSL-Random.x86_64 : OpenSSL/LibreSSL pseudo-random number generator: access
rsyslog-openssl.x86_64 : TLS protocol support for rsyslog via OpenSSL library
xmlsec1-openssl.x86_64 : OpenSSL crypto plugin for XML Security Library
xmlsec1-openssl.i686 : OpenSSL crypto plugin for XML Security LibraryName Matched: openssl
compat-openssl11.i686 : Utilities from the general purpose cryptography library with: TLS implementation
compat-openssl11.x86_64 : Utilities from the general purpose cryptography library: with TLS implementation
openssl-libs.x86_64 : A general purpose cryptography library with TLS implementation
openssl-libs.i686 : A general purpose cryptography library with TLS implementationSummary Matched: openssl
perl-Net-SSLeay.x86_64 : Perl extension for using OpenSSL
qatengine.x86_64 : Intel QuickAssist Technology (QAT) OpenSSL Engine[rootnginx-node1 nginx-1.24.0]# dnf install openssl-devel.x86_64 -y继续检测 [rootnginx-node1 nginx-1.24.0]# dnf install zlib-devel -y继续检测 # 看到这个界面就是检测全部通过检测通过之后会生成一个Makefile的文件
[rootnginx-node1 nginx-1.24.0]# ls
auto CHANGES.ru configure html Makefile objs src
CHANGES conf contrib LICENSE man README# 将之前的操作还原
[rootnginx-node1 nginx-1.24.0]# make clean
rm -rf Makefile objs
[rootnginx-node1 nginx-1.24.0]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src# 重新检测又会重新生成Makefile和objs
[rootnginx-node1 nginx-1.24.0]# ./configure --prefix/usr/local/nginx --usernginx ---groupnginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module- --with-http_gzip_static_module --with-http_stub_status_module --with-pcre --with-staream --with-stream_ssl_module [rootnginx-node1 nginx-1.24.0]# ls
auto CHANGES.ru configure html Makefile objs src
CHANGES conf contrib LICENSE man README[rootnginx-node1 nginx-1.24.0]# cd objs/
[rootnginx-node1 objs]# ls
autoconf.err Makefile ngx_auto_config.h ngx_auto_headers.h ngx_modules.c src
[rootnginx-node1 objs]# cd ..
[rootnginx-node1 nginx-1.24.0]# ls
auto CHANGES.ru configure html Makefile objs src
CHANGES conf contrib LICENSE man README
[rootnginx-node1 nginx-1.24.0]# make -j2[rootnginx-node1 nginx-1.24.0]# ls
auto CHANGES.ru configure html Makefile objs src
CHANGES conf contrib LICENSE man README
[rootnginx-node1 nginx-1.24.0]# cd objs/
[rootnginx-node1 objs]# ls
autoconf.err nginx ngx_auto_config.h ngx_modules.c src
Makefile nginx.8 ngx_auto_headers.h ngx_modules.o
[rootnginx-node1 objs]# cd src/
[rootnginx-node1 src]# ls
core event http mail misc os stream
[rootnginx-node1 src]# cd stream/
[rootnginx-node1 stream]# ls
ngx_stream_access_module.o ngx_stream_set_module.o
ngx_stream_core_module.o ngx_stream_split_clients_module.o
ngx_stream_geo_module.o ngx_stream_ssl_module.o
ngx_stream_handler.o ngx_stream_upstream_hash_module.o
ngx_stream_limit_conn_module.o ngx_stream_upstream_least_conn_module.o
ngx_stream_log_module.o ngx_stream_upstream.o
ngx_stream_map_module.o ngx_stream_upstream_random_module.o
ngx_stream.o ngx_stream_upstream_round_robin.o
ngx_stream_proxy_module.o ngx_stream_upstream_zone_module.o
ngx_stream_return_module.o ngx_stream_variables.o
ngx_stream_script.o ngx_stream_write_filter_module.o# make install将目录里面的东西拷贝到指定的位置
[rootnginx-node1 nginx-1.24.0]# ls
auto CHANGES.ru configure html Makefile objs src
CHANGES conf contrib LICENSE man README
[rootnginx-node1 nginx-1.24.0]# make install [rootnginx-node1 sbin]# useradd -s /sbin/nologin -M nginx
[rootnginx-node1 sbin]# id nginx
uid1001(nginx) gid1001(nginx) groups1001(nginx)
[rootnginx-node1 sbin]# ll
total 5516
-rwxr-xr-x 1 root root 5646216 Aug 16 11:46 nginx
[rootnginx-node1 sbin]# ./nginx
[rootnginx-node1 sbin]# ps aux | grep nginx
root 48860 0.0 0.0 9864 2052 ? Ss 11:50 0:00 nginx: master process ./nginx
nginx 48861 0.0 0.1 14196 5124 ? S 11:50 0:00 nginx: worker process
root 48865 0.0 0.0 221664 2304 pts/0 S 11:50 0:00 grep --colorauto nginx
[rootnginx-node1 sbin]# netstat -antlupe | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 0 79571 48860/nginx: master [rootnginx-node1 sbin]# du -sh nginx
5.4M nginx
[rootnginx-node1 sbin]# cd
# 关闭nginx
[rootnginx-node1 ~]# /usr/local/nginx/sbin/nginx -s stop
[rootnginx-node1 ~]# netstat -antlupe | grep nginx
# 重启nginx
[rootnginx-node1 ~]# /usr/local/nginx/sbin/nginx -s restart[rootnginx-node1 ~]# ls
anaconda-ks.cfg Documents Music nginx-1.24.0.tar.gz Pictures Templates
Desktop Downloads nginx-1.24.0 nginx-1.26.2.tar.gz Public Videos
[rootnginx-node1 ~]# cd nginx-1.24.0/
[rootnginx-node1 nginx-1.24.0]# ls
auto CHANGES.ru configure html Makefile objs src
CHANGES conf contrib LICENSE man README
[rootnginx-node1 nginx-1.24.0]# rm -rf /usr/local/nginx/
[rootnginx-node1 nginx-1.24.0]# make clean
rm -rf Makefile objs
[rootnginx-node1 nginx-1.24.0]# ls
auto CHANGES.ru configure html man src
CHANGES conf contrib LICENSE README[rootnginx-node1 nginx-1.24.0]# vim auto/cc/gcc
# 注释下面那一行# 重新检测
[rootnginx-node1 nginx-1.24.0]# ./configure --prefix/usr/local/nginx --usernginx --groupnginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_gzip_static_module --with-http_stub_status_module --with-pcre --with-stream --with-stream_ssl_module# 编译安装
[rootnginx-node1 nginx-1.24.0]# make make install# 把nginx软件的命令执行路径添加到环境变量中
[rootnginx-node1 nginx-1.24.0]# cd
[rootnginx-node1 ~]# vim ~/.bash_profile
# .bash_profile# Get the aliases and functions
if [ -f ~/.bashrc ]; then. ~/.bashrc
fi# User specific environment and startup programs
export PATH$PATH:/usr/local/nginx/sbin[rootnginx-node1 ~]# source ~/.bash_profile
[rootnginx-node1 ~]# du -sh /usr/local/nginx/sbin/nginx
1.2M /usr/local/nginx/sbin/nginx
[rootnginx-node1 ~]# nginx
nginx启动后浏览器访问172.25.254.100 [rootnginx-node1 ~]# cd /usr/local/nginx/
[rootnginx-node1 nginx]# ls
client_body_temp fastcgi_temp logs sbin uwsgi_temp
conf html proxy_temp scgi_temp
[rootnginx-node1 nginx]# cd conf/
[rootnginx-node1 conf]# ls
fastcgi.conf koi-win scgi_params
fastcgi.conf.default mime.types scgi_params.default
fastcgi_params mime.types.default uwsgi_params
fastcgi_params.default nginx.conf uwsgi_params.default
koi-utf nginx.conf.default win-utf
[rootnginx-node1 conf]# curl -I 172.25.254.100
HTTP/1.1 200 OK
Server: nginx/1.24.0
Date: Fri, 16 Aug 2024 04:54:11 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Fri, 16 Aug 2024 04:47:28 GMT
Connection: keep-alive
ETag: 66bed9e0-267
Accept-Ranges: bytes
Nginx的平滑升级及版本回滚
先在官网上下载1.26.2版本的nginx
[rootnginx-node1 ~]# ll
total 2308
-rw-------. 1 root root 992 Aug 4 00:54 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Desktop
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Documents
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Downloads
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Music
drwxr-xr-x 9 nginx nginx 186 Aug 16 12:46 nginx-1.24.0
-rw-r--r-- 1 root root 1112471 Aug 16 09:17 nginx-1.24.0.tar.gz
-rw-r--r-- 1 root root 1244789 Aug 13 00:39 nginx-1.26.2.tar.gz
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Pictures
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Public
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Templates
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Videos加载模块
将echo-nginx-module-0.63.tar.gz模块上传到系统中并解压
[rootnginx-node1 ~]# tar zxf echo-nginx-module-0.63.tar.gz
[rootnginx-node1 ~]# ll
total 2364
-rw-------. 1 root root 992 Aug 4 00:54 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Desktop
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Documents
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Downloads
drwxrwxr-x 5 root root 174 Aug 1 2022 echo-nginx-module-0.63
-rw-r--r-- 1 root root 53421 Aug 16 09:17 echo-nginx-module-0.63.tar.gz
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Music
drwxr-xr-x 9 nginx nginx 186 Aug 16 12:46 nginx-1.24.0
-rw-r--r-- 1 root root 1112471 Aug 16 09:17 nginx-1.24.0.tar.gz
-rw-r--r-- 1 root root 1244789 Aug 13 00:39 nginx-1.26.2.tar.gz
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Pictures
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Public
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Templates
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Videos
# 解压nginx-1.26.2.tar.gz安装包
[rootnginx-node1 ~]# tar zxf nginx-1.26.2.tar.gz
[rootnginx-node1 ~]# ll
total 2364
-rw-------. 1 root root 992 Aug 4 00:54 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Desktop
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Documents
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Downloads
drwxrwxr-x 5 root root 174 Aug 1 2022 echo-nginx-module-0.63
-rw-r--r-- 1 root root 53421 Aug 16 09:17 echo-nginx-module-0.63.tar.gz
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Music
drwxr-xr-x 9 nginx nginx 186 Aug 16 12:46 nginx-1.24.0
-rw-r--r-- 1 root root 1112471 Aug 16 09:17 nginx-1.24.0.tar.gz
drwxr-xr-x 8 502 games 158 Aug 13 00:39 nginx-1.26.2
-rw-r--r-- 1 root root 1244789 Aug 13 00:39 nginx-1.26.2.tar.gz
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Pictures
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Public
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Templates
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Videos# 检测环境
[rootnginx-node1 ~]# cd nginx-1.26.2/
[rootnginx-node1 nginx-1.26.2]# ls
auto CHANGES.ru configure html man src
CHANGES conf contrib LICENSE README
# 检测1.26.2版本的时候需要增加一个参数--add-module/root/echo-nginx-module-0.63在编译nginx这个版本的时候会把echo-nginx-module-0.63这个模块在nginx中加进去让Nginx在内部可以具备echo命令的功能。
[rootnginx-node1 nginx-1.26.2]# ./configure --prefix/usr/local/nginx --usernginx --groupnginx --add-module/root/echo-nginx-module-0.63 --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_gzip_static_module --with-http_stub_status_module --with-pcre --with-stream --with-stream_ssl_module [rootnginx-node1 nginx-1.26.2]# make[rootnginx-node1 nginx-1.26.2]# cd objs/
[rootnginx-node1 objs]# ls
addon Makefile nginx.8 ngx_auto_headers.h ngx_modules.o
autoconf.err nginx ngx_auto_config.h ngx_modules.c src
[rootnginx-node1 objs]# cd /usr/local/nginx/sbin/
[rootnginx-node1 sbin]# ls
nginx
[rootnginx-node1 sbin]# cp nginx nginx.24
[rootnginx-node1 sbin]# ls
nginx nginx.old
[rootnginx-node1 sbin]# \cp -f /root/nginx-1.26.2/objs/nginx /usr/local/nginx/sbin/[rootnginx-node1 sbin]# ll
total 7208
-rwxr-xr-x 1 root root 6144176 Aug 16 14:32 nginx
-rwxr-xr-x 1 root root 1229024 Aug 16 14:31 nginx.old[rootnginx-node1 sbin]# ps aux | grep nginx
root 55464 0.0 0.0 9864 2052 ? Ss 14:48 0:00 nginx: master process nginx
nginx 55465 0.0 0.1 14200 5380 ? S 14:48 0:00 nginx: worker process
root 55489 0.0 0.0 221664 2304 pts/0 S 14:50 0:00 grep --colorauto nginx#USR2 平滑升级可执行程序,将存储有旧版本主进程PID的文件重命名为nginx.pid.oldbin并启动新的nginx
#此时两个master的进程都在运行,只是旧的master不在监听,由新的master监听80
#此时Nginx开启一个新的master进程这个master进程会生成新的worker进程这就是升级后的Nginx进程此时老的进程不会自动退出但是当接收到新的请求不作处理而是交给新的进程处理。
[rootnginx-node1 sbin]# kill -USR2 55464
[rootnginx-node1 sbin]# ps aux | grep nginx
root 55464 0.0 0.0 9864 2564 ? Ss 14:48 0:00 nginx: master process nginx
nginx 55465 0.0 0.1 14200 5380 ? S 14:48 0:00 nginx: worker process
root 55490 0.0 0.1 9764 6656 ? S 14:50 0:00 nginx: master process nginx
nginx 55491 0.0 0.1 14228 5132 ? S 14:50 0:00 nginx: worker process
root 55493 0.0 0.0 221664 2304 pts/0 S 14:50 0:00 grep --colorauto nginx# 回收旧版本
[rootnginx-node1 sbin]# kill -WINCH 55464[rootnginx-node1 ~]# curl -I 172.25.254.100
HTTP/1.1 200 OK
Server: nginx/1.26.2 #新版本生效
Date: Fri, 16 Aug 2024 06:56:05 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Fri, 16 Aug 2024 04:47:28 GMT
Connection: keep-alive
ETag: 66bed9e0-267
Accept-Ranges: bytes#回滚
#如果升级的版本发现问题需要回滚,可以重新拉起旧版本的worker
[rootnginx-node1 sbin]# kill -HUP 55464
[rootnginx-node1 sbin]#
[rootnginx-node1 sbin]# ps aux | grep nginx
root 55464 0.0 0.0 9864 2564 ? Ss 14:48 0:00 nginx: master process nginx
root 55490 0.0 0.1 9764 6656 ? S 14:50 0:00 nginx: master process nginx
nginx 55491 0.0 0.1 14228 5388 ? S 14:50 0:00 nginx: worker process
nginx 55497 0.0 0.1 14200 5124 ? S 14:58 0:00 nginx: worker process
root 55499 0.0 0.0 221664 2304 pts/0 S 14:58 0:00 grep --colorauto nginx
[rootnginx-node1 sbin]# kill -WINCH 55490
[rootnginx-node1 sbin]# ps aux | grep nginx
root 55464 0.0 0.0 9864 2564 ? Ss 14:48 0:00 nginx: master process nginx
root 55490 0.0 0.1 9764 6656 ? S 14:50 0:00 nginx: master process nginx
nginx 55497 0.0 0.1 14200 5124 ? S 14:58 0:00 nginx: worker process
root 55501 0.0 0.0 221664 2304 pts/0 S 14:59 0:00 grep --colorauto nginx[rootnginx-node1 ~]# curl -I 172.25.254.100
HTTP/1.1 200 OK
Server: nginx/1.24.0 # 旧版本回滚完成
Date: Fri, 16 Aug 2024 06:59:50 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Fri, 16 Aug 2024 04:47:28 GMT
Connection: keep-alive
ETag: 66bed9e0-267
Accept-Ranges: bytes自定义Nginx版本信息
目录/root/nginx-1.24.0/src/core
文件nginx.h Nginx架构和进程 Nginx进程结构
web请求处理机制 多进程方式服务器每接收到一个客户端请求就有服务器的主进程生成一个子进程响应客户端直到用户关闭连接这样的优势是处理速度快子进程之间相互独立但是如果访问过大会导致服务器资源耗尽而无法提供请求 多线程方式与多进程方式类似但是每收到一个客户端请求会有服务进程派生出一个线程和此客户端进行交互一个线程的开销远远小于一个进程因此多线程方式在很大程度减轻了web服务器对系统资源的要求但是多线程也有自己的缺点即当多个线程位于同一个进程内工作的时候可以相互访问同样的内存地址空间所以他们相互影响一旦主进程挂掉则所有子线程都不能工作了IIS服务器使用了多线程的方式需要间隔一段时间就重启一次才能稳定。
Nginx是多进程组织模型而且是一个由Master主进程和Worker工作进程组成 主进程(master process)的功能 对外接口接收外部的操作信号 对内转发根据外部的操作的不同通过信号管理 Worker 监控监控 worker 进程的运行状态worker 进程异常终止后自动重启 worker 进程 读取Nginx 配置文件并验证其有效性和正确性 建立、绑定和关闭socket连接 按照配置生成、管理和结束工作进程 接受外界指令比如重启、升级及退出服务器等指令 不中断服务实现平滑升级重启服务并应用新的配置 开启日志文件获取文件描述符 不中断服务实现平滑升级升级失败进行回滚处理 编译和处理perl脚本
工作进程worker process的功能 所有 Worker 进程都是平等的 实际处理网络请求由 Worker 进程处理 Worker进程数量一般设置为核心数充分利用CPU资源同时避免进程数量过多导致进程竞争CPU资源 增加上下文切换的损耗 接受处理客户的请求 将请求依次送入各个功能模块进行处理 I/O调用获取响应数据 与后端服务器通信接收后端服务器的处理结果 缓存数据访问缓存索引查询和调用缓存数据 发送请求结果响应客户的请求 接收主程序指令比如重启、升级和退出等 Nginx进程间通信
工作进程是由主进程生成的主进程使用fork()函数在Nginx服务器启动过程中主进程根据配置文件决定启动工作进程的数量然后建立一张全局的工作表用于存放当前未退出的所有的工作进程主进程生成工作进程后会将新生成的工作进程加入到工作进程表中并建立一个单向的管道并将其传递给工作进程该管道与普通的管道不同它是由主进程指向工作进程的单向通道包含了主进程向工作进程发出的指令、工作进程ID、工作进程在工作进程表中的索引和必要的文件描述符等信息。
主进程与外界通过信号机制进行通信当接收到需要处理的信号时它通过管道向相关的工作进程发送正确的指令每个工作进程都有能力捕获管道中的可读事件当管道中有可读事件的时候工作进程就会从管道中读取并解析指令然后采取相应的执行动作这样就完成了主进程与工作进程的交互。
worker进程之间的通信原理基本上和主进程与worker进程之间的通信是一样的只要worker进程之间能够取得彼此的信息建立管道即可通信但是由于worker进程之间是完全隔离的因此一个进程想要知道另外一个进程的状态信息,就只能通过主进程来实现。
为了实现worker进程之间的交互master进程在生成worker进程之后在worker进程表中进行遍历将该新进程的PID以及针对该进程建立的管道句柄传递给worker进程中的其他进程为worker进程之间的通信做准备当worker进程1向worker进程2发送指令的时候首先在master进程给它的其他worker进程工作信息
中找到2的进程PID然后将正确的指令写入指向进程2的管道worker进程2捕获到管道中的事件后解析指令并进行相关操作这样就完成了worker进程之间的通信。
另worker进程可以通过共享内存来通讯的比如upstream中的zone或者limit_req、limit_conn中的zone等。操作系统提供了共享内存机制Nginx启动和HTTP建立连接 Nginx 启动时Master 进程加载配置文件 Master 进程初始化监听的 socket Master 进程fork 出多个 Worker 进程 Worker 进程竞争新的连接获胜方通过三次握手建立 Socket 连接并处理请求
Nginx命令常用参数
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit #显示版本和编译参数
-t : test configuration and exit #测试配置文件是否异
-T : test configuration, dump it and exit #测试并打印
-q : suppress non-error messages during configuration testing #静默模式
-s signal : send signal to a master process: stop, quit, reopen, reload #发送信号,reload信号 会生成新的worker,但master不会重新生成
-p prefix : set prefix path (default: /etc/nginx/) #指定Nginx 目录
-c filename : set configuration file (default: /etc/nginx/nginx.conf) #配置文件路径
-g directives : set global directives out of configuration file #设置全局指令,注意和配置文件不要同时配置,否则冲突[rootnginx-node1 sbin]# nginx -v
nginx version: nginx/1.26.2
[rootnginx-node1 sbin]# nginx -V
nginx version: nginx/1.26.2
built by gcc 11.4.1 20231218 (Red Hat 11.4.1-3) (GCC)
built with OpenSSL 3.0.7 1 Nov 2022
TLS SNI support enabled
configure arguments: --prefix/usr/local/nginx --usernginx --groupngino-nginx-module-0.63 --with-http_ssl_module --with-http_v2_module --with-h-http_gzip_static_module --with-http_stub_status_module --with-pcre --w_ssl_module
[rootnginx-node1 sbin]# 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
[rootnginx-node1 sbin]# nginx -s reload
[rootnginx-node1 sbin]#
[rootnginx-node1 sbin]# ps axf | grep nginx55672 pts/0 S 0:00 | \_ grep --colorauto nginx55464 ? Ss 0:00 nginx: master process nginx55490 ? S 0:00 \_ nginx: master process nginx55669 ? S 0:00 | \_ nginx: worker process55534 ? S 0:00 \_ nginx: worker process
Nginx服务的启动脚本
[rootnginx-node1 sbin]# ll
total 6004
-rwxr-xr-x 1 root root 6144176 Aug 16 22:04 nginx
[rootnginx-node1 sbin]#
[rootnginx-node1 sbin]#
[rootnginx-node1 sbin]# nginx
[rootnginx-node1 sbin]# ps aux | grep nginx
root 55793 0.0 0.0 9896 2064 ? Ss 22:05 0:00 nginx: master process nginx
nginx 55794 0.0 0.1 14240 5136 ? S 22:05 0:00 nginx: worker process
root 55796 0.0 0.0 221664 2304 pts/0 S 22:05 0:00 grep --colorauto nginx
[rootnginx-node1 sbin]# nginx -s stop
[rootnginx-node1 sbin]# ps aux | grep nginx
root 55799 0.0 0.0 221664 2304 pts/0 S 22:05 0:00 grep --colorauto nginx
[rootnginx-node1 sbin]# nginx -v
nginx version: nginx/1.26.2[rootnginx-node1 sbin]# vim /lib/systemd/system/nginx.service
[Unit]
DescriptionThe NGINX HTTP and reverse proxy server #描述
Aftersyslog.target network-online.target remote-fs.target nss-lookup.target #指定哪些服务在启动Nginx的时候被激活
Wantsnetwork-online.target #期望什么服务在启动时被激活[Service]
Typeforking # 类型
PIDFile/usr/local/nginx/logs/nginx.pid #PID的位置
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
PrivateTmptrue[Install]
WantedBymulti-user.target[rootnginx-node1 sbin]# systemctl daemon-reload
[rootnginx-node1 sbin]# nginx -s stop
[rootnginx-node1 sbin]# ps aux | grep nginx
root 55844 0.0 0.0 221664 2304 pts/0 S 22:07 0:00 grep --colorauto nginx
[rootnginx-node1 sbin]# systemctl enable --now nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[rootnginx-node1 sbin]# ps aux | grep nginx
root 55887 0.0 0.0 9892 2068 ? Ss 22:07 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 55888 0.0 0.1 14240 5012 ? S 22:07 0:00 nginx: worker process
root 55890 0.0 0.0 221664 2304 pts/0 S 22:07 0:00 grep --colorauto nginx
Nginx核心配置参数
配置文件说明
nginx 官方帮助文档http://nginx.org/en/docs/
Nginx的配置文件的组成部分 主配置文件nginx.conf 子配置文件: include conf.d/*.conf fastcgi uwsgiscgi 等协议相关的配置文件 mime.types支持的mime类型MIME(Multipurpose Internet Mail Extensions)多用途互联网邮件扩展类型MIME消息能包含文本、图像、音频、视频以及其他应用程序专用的数据是设定某种扩展名的文件用一种应用程序来打开的方式类型当该扩展名文件被访问的时候浏览器会自动使用指定应用程序来打开。多用于指定一些客户端自定义的文件名以及一些媒体文件打开方式。
nginx 配置文件格式说明 配置文件由指令与指令块构成 每条指令以;分号结尾指令与值之间以空格符号分隔 可以将多条指令放在同一行,用分号分隔即可,但可读性差,不推荐 指令块以{ }大括号将多条指令组织在一起,且可以嵌套指令块 include语句允许组合多个配置文件以提升可维护性 使用#符号添加注释提高可读性 使用$符号使用变量 部分指令的参数支持正则表达式 Nginx 主配置文件的配置指令方式 directive value [value2 …]; 注意 (1) 指令必须以分号结尾 (2) 支持使用配置变量 内建变量由Nginx模块引入可直接引用 自定义变量由用户使用set命令定义,格式: set variable_name value; 引用变量$variable_name 主配置文件结构四部分 main block主配置段即全局配置段对http,mail都有效 #事件驱动相关的配置 event { … } #http/https 协议相关配置段 http { … } #默认配置文件不包括下面两个块 #mail 协议相关配置段 mail { … } #stream 服务器相关配置段 stream { … } 默认的nginx.conf配置文件格式说明
#全局配置端对全局生效主要设置nginx的启动用户/组启动的工作进程数量工作模式Nginx的PID路
径日志路径等。
user nginx nginx;
worker_processes 1; #启动工作进程数数量
events { #events #设置快主要影响nginx服务器与用户的网络连接比如是否允许同时接受多
个网络连接使用哪种事件驱动模型 #处理请求每个工作进程可以同时支持的
最大连接数是否开启对多工作进程下的网络连接进行序列化等。
worker_connections 1024; #设置单个nginx工作进程可以接受的最大并发作为web服务器
的时候最大并发数为 #worker_connections *
worker_processes作为反向代理的时候为
#(worker_connections * worker_processes)/2
}
http { #http块是Nginx服务器配置中的重要部分缓存、代理和日志格
式定义等绝大多数功能和第三方模块都 #可以在这设置http块可
以包含多个server块而一个server块中又可以包含多个location块
#server块可以配置文件引入、MIME-Type定义、日志自定义、是
否启用sendfile、连接超时时间和 #单个链接的请求上限等。
include mime.types;
default_type application/octet-stream;
sendfile on; #作为web服务器的时候打开sendfile加快静态文件传输指定是
否使用
#sendfile系统调用来传输文件
#sendfile系统调用在两个文件描述符之间直接传递数据(完全在
内核中操作)
#从而避免了数据在内核缓冲区和用户缓冲区之间的拷贝操作效率
很高被称之为零拷贝
#硬盘 kernel buffer (快速拷贝到kernelsocket
buffer) 协议栈。
keepalive_timeout 65; #长连接超时时间单位是秒
server { #设置一个虚拟机主机可以包含自己的全局快同时也可以包含多
个location模块
#比如本虚拟机监听的端口、本虚拟机的名称和IP配置多个
server 可以使用一个端口比如都使用 #80端口提供web服务
listen 80; #配置server监听的端口
server_name localhost; #本server的名称当访问此名称的时候nginx会调用当前serevr
内部的配置进程匹配。
location / { #location其实是server的一个指令为nginx服务器提供比较
多而且灵活的指令
#都是在location中体现的主要是基于nginx接受到的请求字符
串
#对用户请求的UIL进行匹配并对特定的指令进行处理
#包括地址重定向、数据缓存和应答控制等功能都是在这部分实现
#另外很多第三方模块的配置也是在location模块中配置。
root html; #相当于默认页面的目录名称默认是安装目录的相对路径可以使
用绝对路径配置。
index index.html index.htm; #默认的页面文件名称
}
error_page 500 502 503 504 /50x.html; #错误页面的文件名称
location /50x.html { #location处理对应的不同错误码的页面定
义到/50x.html
#这个跟对应其server中定义的目录下。
root html; #定义默认页面所在的目录
}
}
#和邮件相关的配置
#mail {
# ...
# } mail 协议相关配置段
#tcp代理配置1.9版本以上支持
#stream {
# ...
# } stream 服务器相关配置段
#导入其他路径的配置文件
#include /apps/nginx/conf.d/*.conf
}全局配置 user nginx nginx; #启动Nginx工作进程的用户和组 worker_processes [number | auto]; #启动Nginx工作进程的数量,一般设为和CPU核心数相同 worker_cpu_affinity 00000001 00000010 00000100 00001000 | auto ; #将Nginx工作进程绑定到指定的CPU核心默认Nginx是不进行进程绑定的绑定并不是意味着当前nginx进程独占以一核心CPU但是可以保证此进程不运行在其他核心上这就极大减少了nginx的工作进程在不同的cpu核心上的来回跳转减少了CPU对进程的资源分配与回收以及内存管理等因此可以有效的提升nginx服务器的性能。 CPU MASK: 000000010号CPU 000000101号CPU 100000007号CPU [rootnginx-node1 sbin]# cd
[rootnginx-node1 ~]# vim /usr/local/nginx/conf/nginx.conf
user nginx;
worker_processes auto;
worker_cpu_affinity 0001 0010; # 双核#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;#pid logs/nginx.pid;省略[rootnginx-node1 ~]# nginx -s reload
# 此时就会出现两个核心
[rootnginx-node1 ~]# ps aux | grep nginx
root 55887 0.0 0.0 10024 3476 ? Ss 22:07 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 55943 0.0 0.1 14364 5008 ? S 22:29 0:00 nginx: worker process
nginx 55944 0.0 0.1 14364 5008 ? S 22:29 0:00 nginx: worker process
root 55946 0.0 0.0 221664 2304 pts/0 S 22:29 0:00 grep --colorauto nginx# 修改pam限制
[rootnginx-node1 ~]# vim /etc/security/limits.conf
# 在文件末尾添加
nginx - nofile 100000[rootnginx-node1 ~]# sudo -u nginx ulimit -a
real-time non-blocking time (microseconds, -R) unlimited
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 14261
max locked memory (kbytes, -l) 8192
max memory size (kbytes, -m) unlimited
open files (-n) 100000
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 14261
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited[rootnginx-node1 ~]# vim /usr/local/nginx/conf/nginx.conf
events {worker_connections 100000; #修改会话连接数
}
[rootnginx-node1 ~]# nginx -s reload# 测试
[rootnginx-node1 ~]# dnf install httpd-tools -y
[rootnginx-node1 ~]# ab -n 10000 -c 500 http://172.25.254.100/index.html
This is ApacheBench, Version 2.3 $Revision: 1903618 $
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)
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 requestsServer Software: nginx/1.26.2
Server Hostname: 172.25.254.100
Server Port: 80Document Path: /index.html
Document Length: 615 bytesConcurrency Level: 500
Time taken for tests: 0.496 seconds
Complete requests: 10000
Failed requests: 0
Total transferred: 8480000 bytes
HTML transferred: 6150000 bytes
Requests per second: 20146.79 [#/sec] (mean)
Time per request: 24.818 [ms] (mean)
Time per request: 0.050 [ms] (mean, across all concurrent requests)
Transfer rate: 16684.06 [Kbytes/sec] receivedConnection Times (ms)min mean[/-sd] median max
Connect: 0 10 3.0 10 19
Processing: 3 14 5.1 14 28
Waiting: 0 11 4.4 10 25
Total: 11 24 5.4 23 40Percentage of the requests served within a certain time (ms)50% 2366% 2675% 2880% 2990% 3295% 3498% 3699% 37100% 40 (longest request)
root和alias
[rootnginx-node1 ~]# vim /usr/local/nginx/conf/nginx.conf
events {worker_connections 100000;use epoll;
}省略#gzip on;include /usr/local/nginx/conf.d/*.conf; #包含子配置文件省略[rootnginx-node1 ~]# mkdir -p /usr/local/nginx/conf.d
[rootnginx-node1 ~]# vim /usr/local/nginx/conf.d/vhost.conf
server {listen 80;server_name www.qwert.org;root /data/web/html;index index.html;
}
[rootnginx-node1 ~]# mkdir -p /data/web/html
[rootnginx-node1 ~]# echo www.qwert.org /data/web/html/index.html
[rootnginx-node1 ~]# 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
[rootnginx-node1 ~]# nginx -s reload在Windows解析文件上添加解析
172.25.254.100 www.qwert.org
浏览器访问测试 [rootnginx-node1 ~]# vim /usr/local/nginx/conf.d/vhost.conf
server {listen 80;server_name www.qwert.org;root /data/web/html;index index.html;location /test1/ {root /data/web;}
}[rootnginx-node1 ~]# mkdir -p /data/web/test1
[rootnginx-node1 ~]# echo /data/web/test1 /data/web/test1/index.html
[rootnginx-node1 ~]# 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
[rootnginx-node1 ~]# nginx -s reload
访问测试 [rootnginx-node1 ~]# vim /usr/local/nginx/conf.d/vhost.conf
server {listen 80;server_name www.qwert.org;root /data/web/html;index index.html;location /test1/ {root /data/web;}location /test2 {alias /data/web/test1;}
}
[rootnginx-node1 ~]# nginx -s reload访问测试 location的详细介绍
在一个server中location配置段可存在多个用于实现从uri到文件系统的路径映射ngnix会根据用户请求的URI来检查定义的所有location按一定的优先级找出一个最佳匹配而后应用其配置在没有使用正则表达式的时候nginx会先在server中的多个location选取匹配度最高的一个uri
uri是用户请求的字符串即域名后面的web文件路径
然后使用该location模块中的正则url和字符串如果匹配成功就结束搜索并使用此location处理此请求。
#语法规则
location [ | ~ | ~* | ^~ ] uri { ... } #用于标准uri前需要请求字串与uri精确匹配大小敏感,如果匹配成功就停止向下匹配并立即处理请求^~ #用于标准uri前表示包含正则表达式,并且匹配以指定的正则表达式开头,对uri的最左边部分做匹配检查不区分字符大小写~ #用于标准uri前表示包含正则表达式,并且区分大小写~* #用于标准uri前表示包含正则表达式,并且不区分大写
不带符号 匹配起始于此uri的所有的uri\ #用于标准uri前表示包含正则表达式并且转义字符。可以将 . * ?等转义为普通符号#匹配优先级从高到低
~*|~ 不带符号 ^~ [rootnginx-node1 ~]# vim /usr/local/nginx/conf.d/vhost.conf
server {listen 80;server_name www.qwert.org;root /data/web/html;index index.html;location /test {root /data/web;}location /test {root /data/web;}
}
[rootnginx-node1 ~]# mkdir -p /data/web/test
[rootnginx-node1 ~]# echo test test test /data/web/test/index.html
[rootnginx-node1 ~]# nginx -s reload [rootnginx-node1 ~]# vim /usr/local/nginx/conf.d/vhost.conf
server {listen 80;server_name www.qwert.org;root /data/web/html;index index.html;location /test {root /data/web1;}location /test {root /data/web2;}
}[rootnginx-node1 ~]# mkdir /data/web{1,2}
[rootnginx-node1 ~]# mkdir /data/web{1,2}/test
[rootnginx-node1 ~]# echo web1 test /data/web1/test/index.html
[rootnginx-node1 ~]# echo web2 test /data/web2/test/index.html
[rootnginx-node1 ~]# nginx -s reload [rootnginx-node1 ~]# vim /usr/local/nginx/conf.d/vhost.conf
server {listen 80;server_name www.qwert.org;root /data/web/html;index index.html;location /test {root /data/web1;}location /test {root /data/web2;}location ^~ /t { # 精确匹配以t开头的root /data/web1;}
}
[rootnginx-node1 ~]# mkdir -p /data/web1/{test1,tee}
[rootnginx-node1 ~]# echo test1 /data/web1/test1/index.html
[rootnginx-node1 ~]# echo tee /data/web1/tee/index.html
[rootnginx-node1 ~]# mkdir -p /data/web1/lee
[rootnginx-node1 ~]# echo lee /data/web1/lee/index.html
[rootnginx-node1 ~]#
[rootnginx-node1 ~]# nginx -s reload
[rootnginx-node1 ~]# [rootnginx-node1 ~]# vim /usr/local/nginx/conf.d/vhost.conf
server {listen 80;server_name www.qwert.org;root /data/web/html;index index.html;location ~ \.html$ { #以html结尾root /data/web1;}
}
[rootnginx-node1 ~]# nginx -s reload [rootnginx-node1 ~]# vim /usr/local/nginx/conf.d/vhost.conf
server {listen 80;server_name www.qwert.org;root /data/web/html;index index.html;location /test {root /data/web1;}location /test {root /data/web2;}location ^~ /t {root /data/web1;}location ~* \.HTML$ { #以html结尾不区分大小写root /data/web1;}
}
[rootnginx-node1 ~]# nginx -s reload 优先级测试
[rootnginx-node1 ~]# mkdir -p /data/web{1..5}
[rootnginx-node1 ~]# mkdir -p /data/web{1..5}/test
[rootnginx-node1 ~]# echo web1 /data/web1/test/index.html
[rootnginx-node1 ~]# echo web2 /data/web2/test/index.html
[rootnginx-node1 ~]# echo web3 /data/web3/test/index.html
[rootnginx-node1 ~]# echo web4 /data/web4/test/index.html
[rootnginx-node1 ~]# echo web5 /data/web5/test/index.html
[rootnginx-node1 ~]# vim /usr/local/nginx/conf.d/vhost.conf
server {listen 80;server_name www.qwert.org;root /data/web/html;index index.html;location /test {root /data/web1;}location /test {root /data/web2;}location ^~ /t {root /data/web3;}location ~ .html$ {root /data/web4;}location ~* .HTML$ {root /data/web5;}
}
[rootnginx-node1 ~]# nginx -s reload [rootnginx-node1 ~]# vim /usr/local/nginx/conf.d/vhost.conf
server {listen 80;server_name www.qwert.org;root /data/web/html;index index.html;location /test {root /data/web1;}location /test {root /data/web2;}location ^~ /t {root /data/web3;}#location ~ .html$ {# root /data/web4;#}location ~* .HTML$ {root /data/web5;}
}[rootnginx-node1 ~]# nginx -s reload 取消注释交换web4和web5的位置最后浏览器访问还是web5所以~和~*的优先级是一样的
继续验证
[rootnginx-node1 ~]# vim /usr/local/nginx/conf.d/vhost.conf
server {listen 80;server_name www.qwert.org;root /data/web/html;index index.html;location /test {root /data/web1;}location /test {root /data/web2;}location ^~ /t {root /data/web3;}#location ~* .HTML$ {# root /data/web5;# }#location ~ .html$ {# root /data/web4;# }
}
[rootnginx-node1 ~]# nginx -s reload[rootnginx-node1 ~]# vim /usr/local/nginx/conf.d/vhost.conf
server {listen 80;server_name www.qwert.org;root /data/web/html;index index.html;#location /test {# root /data/web1;# }location /test {root /data/web2;}location ^~ /t {root /data/web3;}#location ~* .HTML$ {# root /data/web5;# }#location ~ .html$ {# root /data/web4;# }}[rootnginx-node1 ~]# nginx -s reload 优先级排序
精确匹配正则匹配一般匹配
Nginx下的用户认证
# 创建认证文件
[rootnginx-node1 ~]# htpasswd -cm /usr/local/nginx/.htpasswd admin
New password:
Re-type new password:
Adding password for user admin
[rootnginx-node1 ~]# cat /usr/local/nginx/.htpasswd
admin:$apr1$L3M5h8XO$aBF8qjVg6z5SHy1uBaqfp0
[rootnginx-node1 ~]# htpasswd -m /usr/local/nginx/.htpasswd lee
New password:
Re-type new password:
Adding password for user lee
[rootnginx-node1 ~]# cat /usr/local/nginx/.htpasswd
admin:$apr1$L3M5h8XO$aBF8qjVg6z5SHy1uBaqfp0
lee:$apr1$QNC.iHFe$ejcSQ9QwIOsUzL4OJYZ/o0[rootnginx-node1 ~]# mkdir /data/web/lee
[rootnginx-node1 ~]# echo lee /data/web/lee/index.html[rootnginx-node1 ~]# vim /usr/local/nginx/conf.d/vhost.conf
server {listen 80;server_name www.qwert.org;root /data/web/html;index index.html;location /lee {root /data/web;auth_basic input your password!;auth_basic_user_file /usr/local/nginx/.htpasswd;}
}
[rootnginx-node1 ~]# nginx -s reload 输入账号密码登录访问到页面 Nginx自定义错误页面
最开始的错误页面是这样现在重新自定义错误页面 [rootnginx-node1 ~]# vim /usr/local/nginx/conf.d/vhost.conf
server {listen 80;server_name www.qwert.org;root /data/web/html;index index.html;error_page 404 /40x.html;location /lee {root /data/web;auth_basic input your password!;auth_basic_user_file /usr/local/nginx/.htpasswd;}location /40x.html {root /data/web/errorpage;}
}
[rootnginx-node1 ~]# mkdir -p /data/web/errorpage
[rootnginx-node1 ~]# echo error page /data/web/errorpage/40x.html
[rootnginx-node1 ~]# nginx -s reload Nginx下的自定义日志
[rootnginx-node1 ~]# vim /usr/local/nginx/conf.d/vhost.conf
server {listen 80;server_name www.qwert.org;root /data/web/html;index index.html;error_page 404 /40x.html;error_log /var/log/qwert.org/error.log;access_log /var/log/qwert.org/access.log;location /lee {root /data/web;auth_basic input your password!;auth_basic_user_file /usr/local/nginx/.htpasswd;}location /40x.html {root /data/web/errorpage;}
}
[rootnginx-node1 ~]# mkdir /var/log/qwert.org
[rootnginx-node1 ~]# nginx -s reload
[rootnginx-node1 ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.25.254.100 nginx-node1.example.com
172.25.254.100 www.qwert.org
[rootnginx-node1 ~]# curl www.qwert.org
www.qwert.org
[rootnginx-node1 ~]# cat /var/log/qwert.org/access.log
172.25.254.100 - - [17/Aug/2024:14:37:52 0800] GET / HTTP/1.1 200 14 - curl/7.76.1
[rootnginx-node1 ~]# curl www.qwert.org/aaa
error page
[rootnginx-node1 ~]# cat /var/log/qwert.org/error.log
2024/08/17 14:38:26 [error] 56859#0: *10258 open() /data/web/html/aaa failed (2: No such file or directory), client: 172.25.254.100, server: www.qwert.org, request: GET /aaa HTTP/1.1, host: www.qwert.org
[rootnginx-node1 ~]# Nginx下的文件检测
[rootnginx-node1 ~]# vim /usr/local/nginx/conf.d/vhost.conf
server {listen 80;server_name www.qwert.org;root /data/web/html;index index.html;error_page 404 /40x.html;error_log /var/log/qwert.org/error.log;access_log /var/log/qwert.org/access.log;try_files $uri $uri.html $uri/index.html /error/default.html;location /lee {root /data/web;auth_basic input your password!;auth_basic_user_file /usr/local/nginx/.htpasswd;}location /40x.html {root /data/web/errorpage;}
}
[rootnginx-node1 ~]# nginx -s reload
[rootnginx-node1 ~]# curl www.qwert.org
www.qwert.org
[rootnginx-node1 ~]# rm -rf /data/web/html/index.html
[rootnginx-node1 ~]# rm -rf /data/web/html/error
[rootnginx-node1 ~]# curl www.qwert.org
html
headtitle500 Internal Server Error/title/head
body
centerh1500 Internal Server Error/h1/center
hrcenternginx/1.26.2/center
/body
/html
[rootnginx-node1 ~]# mkdir -p /data/web/html/error
[rootnginx-node1 ~]# echo error default /data/web/html/error/default.html
[rootnginx-node1 ~]# curl www.qwert.org
error default
Nginx长链接控制
keepalive_timeout timeout [header_timeout];
#设定保持连接超时时长0表示禁止长连接默认为75s
#通常配置在http字段作为站点全局配置keepalive_requests 数字;
#在一次长连接上所允许请求的资源的最大数量
#默认为100次,建议适当调大,比如:500[rootnginx-node1 ~]# dnf install telnet -y
[rootnginx-node1 ~]# vim /usr/local/nginx/conf/nginx.conf省略#keepalive_timeout 0;keepalive_timeout 65;keepalive_requests 2;
省略[rootnginx-node1 ~]# nginx -s reload
[rootnginx-node1 ~]# telnet www.qwert.org 80
Trying 172.25.254.100...
Connected to www.qwert.org.
Escape character is ^].
GET / HTTP/1.1
HOST: www.qwert.orgHTTP/1.1 200 OK
Server: nginx/1.26.2
Date: Sat, 17 Aug 2024 07:12:19 GMT
Content-Type: text/html
Content-Length: 14
Last-Modified: Sat, 17 Aug 2024 07:09:20 GMT
Connection: keep-alive
ETag: 66c04ca0-e
Accept-Ranges: byteswww.qwert.org
GET / HTTP/1.1
HOST: www.qwert.orgHTTP/1.1 200 OK
Server: nginx/1.26.2
Date: Sat, 17 Aug 2024 07:12:34 GMT
Content-Type: text/html
Content-Length: 14
Last-Modified: Sat, 17 Aug 2024 07:09:20 GMT
Connection: close
ETag: 66c04ca0-e
Accept-Ranges: byteswww.qwert.org
Connection closed by foreign host.[rootnginx-node1 ~]# vim /usr/local/nginx/conf/nginx.conf省略#keepalive_timeout 0;keepalive_timeout 65 60;keepalive_requests 2;
省略 Nginx下载服务器
[rootnginx-node1 ~]# mkdir /data/web/download
[rootnginx-node1 ~]#
[rootnginx-node1 ~]# dd if/dev/zero of/data/web/download/leefile bs1M count100
1000 records in
1000 records out
104857600 bytes (105 MB, 100 MiB) copied, 0.032775 s, 3.2 GB/s
[rootnginx-node1 ~]#
[rootnginx-node1 ~]# vim /usr/local/nginx/conf.d/vhost.conf
server {listen 80;server_name www.qwert.org;root /data/web/html;index index.html;error_page 404 /40x.html;error_log /var/log/qwert.org/error.log;access_log /var/log/qwert.org/access.log;try_files $uri $uri.html $uri/index.html /error/default.html;location /lee {root /data/web;auth_basic input your password!;auth_basic_user_file /usr/local/nginx/.htpasswd;}location /40x.html {root /data/web/errorpage;}location /download {root /data/web;autoindex on;}}
[rootnginx-node1 ~]# nginx -s reload[rootnginx-node1 ~]# curl www.qwert.org/download/
html
headtitleIndex of /download//title/head
body
h1Index of /download//h1hrprea href../..//a
a hrefleefileleefile/a 17-Aug-2024 07:49 104857600
/prehr/body
/html 把时间修改为本地时间
[rootnginx-node1 ~]# vim /usr/local/nginx/conf.d/vhost.conf
server {listen 80;server_name www.qwert.org;root /data/web/html;index index.html;error_page 404 /40x.html;error_log /var/log/qwert.org/error.log;access_log /var/log/qwert.org/access.log;try_files $uri $uri.html $uri/index.html /error/default.html;location /lee {root /data/web;auth_basic input your password!;auth_basic_user_file /usr/local/nginx/.htpasswd;}location /40x.html {root /data/web/errorpage;}location /download {root /data/web;autoindex on;autoindex_localtime on;}}
[rootnginx-node1 ~]# nginx -s reload
[rootnginx-node1 ~]# curl www.qwert.org/download/
html
headtitleIndex of /download//title/head
body
h1Index of /download//h1hrprea href../..//a
a hrefleefileleefile/a 17-Aug-2024 15:49 104857600
/prehr/body
/html 修改文件大小的单位
[rootnginx-node1 ~]# vim /usr/local/nginx/conf.d/vhost.conf
server {listen 80;server_name www.qwert.org;root /data/web/html;index index.html;error_page 404 /40x.html;error_log /var/log/qwert.org/error.log;access_log /var/log/qwert.org/access.log;try_files $uri $uri.html $uri/index.html /error/default.html;location /lee {root /data/web;auth_basic input your password!;auth_basic_user_file /usr/local/nginx/.htpasswd;}location /40x.html {root /data/web/errorpage;}location /download {root /data/web;autoindex on;autoindex_localtime on;autoindex_exact_size off;}
}限制下载速度
[rootnginx-node1 ~]# vim /usr/local/nginx/conf.d/vhost.conf
server {listen 80;server_name www.qwert.org;root /data/web/html;index index.html;error_page 404 /40x.html;error_log /var/log/qwert.org/error.log;access_log /var/log/qwert.org/access.log;try_files $uri $uri.html $uri/index.html /error/default.html;location /lee {root /data/web;auth_basic input your password!;auth_basic_user_file /usr/local/nginx/.htpasswd;}location /40x.html {root /data/web/errorpage;}location /download {root /data/web;autoindex on; #自动索引功能autoindex_localtime on; #on表示显示本机时间而非GMT(格林威治)时间,默为为off显示GMT时间autoindex_exact_size off; #计算文件确切大小单位bytes此为默认值,off只显示大概大小单位kb、mb、gblimit_rate 1024k; #限速,默认不限速}
}Nginx高级配置
Nginx状态页 Active connections: 291
server accepts handled requests
16630948 16630948 31070465
上面三个数字分别对应accepts,handled,requests三个值
Reading: 6 Writing: 179 Waiting: 106
Active connections: #当前处于活动状态的客户端连接数
#包括连接等待空闲连接数readingwritingwaitingaccepts #统计总值Nginx自启动后已经接受的客户端请求连接的总数。handled: #统计总值Nginx自启动后已经处理完成的客户端请求连接总数,通常等于accepts除非有因worker_connections限制等被拒绝的连接requests: #统计总值Nginx自启动后客户端发来的总的请求数Reading: #当前状态正在读取客户端请求报文首部的连接的连接数,数值越大,说明排队现象严重,性能不足Writing: #当前状态正在向客户端发送响应报文过程中的连接数,数值越大,说明访问量很大Waiting: #当前状态正在等待客户端发出请求的空闲连接数
开启 keep-alive的情况下,这个值等于active –(readingwriting)[rootnginx-node1 ~]# cd /usr/local/nginx/conf.d/
[rootnginx-node1 conf.d]# vim status.conf
server {listen 80;server_name status.qwert.org;root /data/web/html;index index.html;location /status {stub_status;}
}
[rootnginx-node1 conf.d]# nginx -s reload在Windows本地解析文件中添加域名
172.25.254.100 www.qwert.org status.qwert.org
在浏览器访问测试 指定用户访问状态页
[rootnginx-node1 conf.d]# vim status.conf
server {listen 80;server_name status.qwert.org;root /data/web/html;index index.html;location /status {stub_status;#auth_basic login;#auth_basic_user_file /usr/local/nginx/.htpasswd;allow 172.25.254.1; #Windows主机IP地址deny all;}
}
[rootnginx-node1 conf.d]# nginx -s reload
[rootnginx-node1 conf.d]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.25.254.100 nginx-node1.example.com
172.25.254.100 www.qwert.org
172.25.254.100 status.qwert.org[rootnginx-node1 conf.d]# curl status.qwert.org/status
html
headtitle403 Forbidden/title/head
body
centerh1403 Forbidden/h1/center
hrcenternginx/1.26.2/center
/body
/html Nginx的数据压缩功能
Nginx支持对指定类型的文件进行压缩然后再传输给客户端而且压缩还可以设置压缩比例压缩后的文件大小将比源文件显著变小样有助于降低出口带宽的利用率降低企业的IT支出不过会占用相应的CPU资源。
Nginx对文件的压缩功能是依赖于模块 ngx_http_gzip_module,默认是内置模块
配置指令如下
#启用或禁用gzip压缩默认关闭
gzip on | off;#压缩比由低到高从1到9默认为1值越高压缩后文件越小但是消耗cpu比较高。基本设定未4或者5
gzip_comp_level 4;#禁用IE6 gzip功能早期的IE6之前的版本不支持压缩
gzip_disable MSIE [1-6]\.;#gzip压缩的最小文件小于设置值的文件将不会压缩
gzip_min_length 1k;#启用压缩功能时协议的最小版本默认HTTP/1.1
gzip_http_version 1.0 | 1.1;#指定Nginx服务需要向服务器申请的缓存空间的个数和大小,平台不同,默认:32 4k或者16 8k;
gzip_buffers number size;#指明仅对哪些类型的资源执行压缩操作;默认为gzip_types text/html不用显示指定否则出错
gzip_types mime-type ...;#如果启用压缩是否在响应报文首部插入“Vary: Accept-Encoding”,一般建议打开
gzip_vary on | off;#预压缩即直接从磁盘找到对应文件的gz后缀的式的压缩文件返回给用户无需消耗服务器CPU
#注意: 来自于ngx_http_gzip_static_module模块
gzip_static on | off;[rootnginx-node1 conf.d]# vim /usr/local/nginx/conf/nginx.conf省略#keepalive_timeout 0;keepalive_timeout 65 60;keepalive_requests 2;gzip on; # 打开压缩功能gzip_comp_level 5;gzip_min_length 1k;gzip_http_version 1.1;gzip_vary on;gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/gif image/png;省略[rootnginx-node1 conf.d]# 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
[rootnginx-node1 conf.d]# nginx -s reload[rootnginx-node1 ~]# echo hello hello /data/web/html/small.html[rootnginx-node1 ~]# du -sh /usr/local/nginx/logs/access.log
1.1M /usr/local/nginx/logs/access.log
[rootnginx-node1 ~]# cat /usr/local/nginx/logs/access.log /data/web/html/big.html[rootnginx-node1 ~]# curl --head --compressed 172.25.254.100/small.html
HTTP/1.1 200 OK
Server: nginx/1.26.2
Date: Sat, 17 Aug 2024 08:40:39 GMT
Content-Type: text/html
Content-Length: 12
Last-Modified: Sat, 17 Aug 2024 08:37:51 GMT
Connection: keep-alive
Keep-Alive: timeout60
ETag: 66c0615f-c
Accept-Ranges: bytes[rootnginx-node1 ~]# curl --head --compressed 172.25.254.100/big.html
HTTP/1.1 200 OK
Server: nginx/1.26.2
Date: Sat, 17 Aug 2024 08:40:46 GMT
Content-Type: text/html
Last-Modified: Sat, 17 Aug 2024 08:39:14 GMT
Connection: keep-alive
Keep-Alive: timeout60
Vary: Accept-Encoding
ETag: W/66c061b2-106ac8
Content-Encoding: gzip
Nginx中的变量
常用内置变量
$remote_addr;
#存放了客户端的地址注意是客户端的公网IP$args;
#变量中存放了URL中的所有参数
#例如:https://search.jd.com/Search?keyword手机encutf-8
#返回结果为: keyword手机encutf-8$is_args
#如果有参数为? 否则为空$document_root;
#保存了针对当前资源的请求的系统根目录,例如:/webdata/nginx/timinglee.org/lee。$document_uri;
#保存了当前请求中不包含参数的URI注意是不包含请求的指令
#比如:http://lee.timinglee.org/var?\id11111会被定义为/var
#返回结果为:/var$host;
#存放了请求的host名称limit_rate 10240;
echo $limit_rate;
#如果nginx服务器使用limit_rate配置了显示网络速率则会显示如果没有设置 则显示0$remote_port;
#客户端请求Nginx服务器时随机打开的端口这是每个客户端自己的端口$remote_user;
#已经经过Auth Basic Module验证的用户名$request_body_file;
#做反向代理时发给后端服务器的本地资源的名称$request_method;
#请求资源的方式GET/PUT/DELETE等$request_filename;
#当前请求的资源文件的磁盘路径由root或alias指令与URI请求生成的文件绝对路径
#如:webdata/nginx/timinglee.org/lee/var/index.html$request_uri;
#包含请求参数的原始URI不包含主机名相当于:$document_uri?$args,
#例如/main/index.do?id20190221partnersearch$scheme;
#请求的协议例如:httphttps,ftp等$server_protocol;
#保存了客户端请求资源使用的协议的版本例如:HTTP/1.0HTTP/1.1HTTP/2.0等$server_addr;
#保存了服务器的IP地址$server_name;
#虚拟主机的主机名$server_port;
#虚拟主机的端口号$http_user_agent;
#客户端浏览器的详细信息$http_cookie;
#客户端的所有cookie信息$cookie_name
#name为任意请求报文首部字部cookie的key名$http_name
#name为任意请求报文首部字段,表示记录请求报文的首部字段ame的对应的首部字段名需要为小写如果有横线需要替换为下划线[rootnginx-node1 conf.d]# vim var.conf
server {listen 80;server_name var.timinglee.org.org;root /data/web/html;index index.html;location /var {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 $http_cookie;echo $cookie_key2;}
}
[rootnginx-node1 conf.d]# vim /etc/hosts
[rootnginx-node1 conf.d]# curl -b key1lee,key2lee1 -u lee:lee var.timinglee.org/var?nameleeid6666
172.25.254.100
namelee
?
/data/web/html
/var
var.timinglee.org
36818
lee
GET
/data/web/html/var
/var?namelee
http
HTTP/1.1
172.25.254.100
var.timinglee.org
80
curl/7.76.1
key1lee,key2lee1
lee1[rootnginx-node1 conf.d]# vim zdyvar.conf
server {listen 80;server_name var.timinglee.org;root /data/web/html;index index.html;location /var {default_type text/html;set $timinglee lee;echo $timinglee;}
}
[rootnginx-node1 conf.d]# nginx -s reload
[rootnginx-node1 conf.d]# curl -b key1lee,key2lee1 -u lee:lee var.timinglee.org/var?nameleeid6666
leeRewrite相关功能 Nginx服务器利用 ngx_http_rewrite_module 模块解析和处理rewrite请求 此功能依靠 PCRE(perl compatible regular expression)因此编译之前要安装PCRE库 rewrite是nginx服务器的重要功能之一用于实现URL的重写URL的重写是非常有用的功能 比如它可以在我们改变网站结构之后不需要客户端修改原来的书签也无需其他网站修改我们的链接就可以设置为访问 另外还可以在一定程度上提高网站的安全性。
server {listen 80;server_name var.qwert.org;root /data/web/html;index index.html;location /var {default_type text/html;set $timinglee lee;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 $http_cookie;echo $cookie_key2;echo $timinglee;}location /test2 {if ( !-e $request_filename ){echo $request_filename is not exist;}}
}
[rootnginx-node1 conf.d]# nginx -s reload
[rootnginx-node1 conf.d]# curl var.qwert.org/rest2
[rootnginx-node1 conf.d]# curl var.qwert.org/test2
/data/web/html/test2 is not exist[rootnginx-node1 conf.d]# vim var.conf
server {listen 80;server_name var.qwert.org;root /data/web/html;index index.html;location /var {default_type text/html;set $timinglee lee;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 $http_cookie;echo $cookie_key2;echo $timinglee;}location /test2 {if ( !-e $request_filename ){echo $request_filename is not exist;}}location /break {default_type text/html;set $name lee;echo $name;if ( $http_user_agent curl/7.76.1 ){break;}set $id 666;echo $id;}location /return {default_type text/html;if ( !-e $request_filename){return 301 http://www.baidu.com;}echo $request_filename is exist;}location / {root /data/web/var;index index.html;rewrite / http://www.timinglee.com permanent;#rewrite / http://www.timinglee.com redirect;}
}
[rootnginx-node1 conf.d]# curl var.qwert.org/break
lee[rootnginx-node1 conf.d]# curl -A firefox var.qwert.org/break
lee
666
[rootnginx-node1 conf.d]# curl -I var.qwert.org/return
HTTP/1.1 301 Moved Permanently
Server: nginx/1.26.2
Date: Sun, 18 Aug 2024 11:08:42 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
Keep-Alive: timeout60
Location: http://www.baidu.com[rootnginx-node1 conf.d]# mkdir -p /data/web/html/return
[rootnginx-node1 conf.d]# curl -I var.qwert.org/return
HTTP/1.1 200 OK
Server: nginx/1.26.2
Date: Sun, 18 Aug 2024 11:08:59 GMT
Content-Type: text/html
Connection: keep-alive
Keep-Alive: timeout60
Vary: Accept-Encoding
[rootnginx-node1 conf.d]# curl -I var.qwert.org
HTTP/1.1 301 Moved Permanently
Server: nginx/1.26.2
Date: Sun, 18 Aug 2024 11:09:30 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
Keep-Alive: timeout60
Location: http://www.timinglee.com# break和last
[rootnginx-node1 conf.d]# vim var.conf
server {listen 80;server_name var.qwert.org;root /data/web/html;index index.html;location /break {root /data/web/html;rewrite ^/break/(.*) /test1/$1 break;rewrite ^/test1/(.*) /test2/$2;}location /last {root /data/web/html;rewrite ^/last/(.*) /test1/$1 last;rewrite ^/test1/(.*) /test2/$2;}location /test1 {default_type text/html;return 666 qwert hahahahahaahhaahahaa;}location /test2 {root /data/web/html;}
}
[rootnginx-node1 conf.d]# nginx -s reload
[C:\~]$ curl var.qwert.org/break/index.html% Total % Received % Xferd Average Speed Time Time Time CurrentDload Upload Total Spent Left Speed
100 6 100 6 0 0 687 0 --:--:-- --:--:-- --:--:-- 1000
test1[C:\~]$ curl var.qwert.org/last/index.html% Total % Received % Xferd Average Speed Time Time Time CurrentDload Upload Total Spent Left Speed
100 26 100 26 0 0 5106 0 --:--:-- --:--:-- --:--:-- 8666
qwert hahahahahaahhaahahaa
全站加密
[rootnginx-node1 conf.d]# cd /usr/local/nginx/
[rootnginx-node1 nginx]# mkdir certs
[rootnginx-node1 nginx]# openssl req -newkey rsa:2048 -nodes -sha256 -keyout /usr/local/nginx/certs/qwert.org.key -x509 -days 365 -out /usr/local/nginx/certs/qwert.org.crt
[rootnginx-node1 nginx]# vim /usr/local/nginx/conf.d/vhost.conf
server {listen 80;listen 443 ssl;server_name www.qwert.org;root /data/web/html;index index.html;ssl_certificate /usr/local/nginx/certs/qwert.org.crt;ssl_certificate_key /usr/local/nginx/certs/qwert.org.key;ssl_session_cache shared:SSL:1m;ssl_session_timeout 5m;location / {if ( $scheme http ){rewrite /(.*) https://$host/$1 redirect;}if ( !-e $request_filename ){rewrite /(.*) https://$host/index.html redirect;}}
}
[rootnginx-node1 nginx]# nginx -s reload防盗链
防盗链基于客户端携带的referer实现referer是记录打开一个页面之前记录是从哪个页面跳转过来的标记信息如果别人只链接了自己网站图片或某个单独的资源而不是打开了网站的整个页面这就是盗链referer就是之前的那个网站域名正常的referer信息有以下几种
none #请求报文首部没有referer首部比如用户直接在浏览器输入域名访问web网站就没有referer信息。blocked #请求报文有referer首部但无有效值比如为空。server_names #referer首部中包含本主机名及即nginx 监听的server_name。arbitrary_string #自定义指定字符串但可使用*作通配符。regular expression #被指定的正则表达式模式匹配到的字符串,要使用~开头[rootnginx-node1 ~]# mkdir -p /data/web/html/images
[rootnginx-node1 ~]# ll /data/web/html/images/
total 776
-rw-r--r-- 1 root root 791331 Aug 18 19:48 lee.jpg
[rootnginx-node1 ~]# ll /data/web/html
-rw-r--r-- 1 root root 598519 Aug 18 19:48 daolian.jpg
[rootnginx-node1 ~]# vim /usr/local/nginx/conf.d/vhost.conf
server {listen 80;listen 443 ssl;server_name www.qwert.org;root /data/web/html;index index.html;ssl_certificate /usr/local/nginx/certs/qwert.org.crt;ssl_certificate_key /usr/local/nginx/certs/qwert.org.key;ssl_session_cache shared:SSL:1m;ssl_session_timeout 5m;location /images {valid_referers none blocked server_names *.qwert.org ~/.baidu/.;if ( $invalid_referer ){rewrite ^/ http://www.qwert.org/daolian.jpg;}}
}
[rootnginx-node1 nginx]# nginx -s reload# web10主机要下载httpd服务
[rootweb10 html]# cd /var/www/html/
[rootweb10 html]# vim index.html
htmlheadmeta http-equivContent-Type contenttext/html;charsetutf-8title盗链/title
/headbodyimg srchttp://www.qwert.org/images/lee.png h1 stylecolor:red欢迎大家/h1pa hrefhttp://www.qwert.orghahahahahhaa/a出门见喜/p/body/html
[rootweb10 html]# systemctl restart httpd继续点击hahahahahhaa链接 Nginx反向代理及动静分离实现
客户在需要访问后端真实的资源时首先被定义到Nginx上Nginx根据我们客户的请求把资源分别定义到不同的位置通过location来把资源定义到不同位置。 反向代理reverse proxy指的是代理外网用户的请求到内部的指定的服务器并将数据返回给用户的一种方式这是用的比较多的一种方式。
Nginx 除了可以在企业提供高性能的web服务之外另外还可以将 nginx 本身不具备的请求通过某种预定义的协议转发至其它服务器处理不同的协议就是Nginx服务器与其他服务器进行通信的一种规范主要在不同的场景使用以下模块实现不同的功能
ngx_http_proxy_module: #将客户端的请求以http协议转发至指定服务器进行处理ngx_http_upstream_module #用于定义为proxy_pass,fastcgi_pass,uwsgi_pass等指令引用的后端服务器分组ngx_stream_proxy_module: #将客户端的请求以tcp协议转发至指定服务器处理
ngx_http_fastcgi_module: #将客户端对php的请求以fastcgi协议转发至指定服务器处理ngx_http_uwsgi_module: #将客户端对Python的请求以uwsgi协议转发至指定服务器处理逻辑调用关系 访问逻辑图 同构代理用户不需要其他程序的参与直接通过http协议或者tcp协议访问后端服务器。静态
异构代理用户访问的资源时需要经过处理后才能返回的比如phppython等等这种访问资源需要经过处理才能被访问 配置参数
参数说明proxy_pass用来设置将客户端请求转发给的后端服务器的主机,可以是主机名(将转发至后端服务做为主机头首部)、IP地址端口的方式,也可以代理到预先设置的主机群组需要模块ngx_http_upstream_module支持proxy_hide_header field用于nginx作为反向代理的时候,在返回给客户端http响应时,隐藏后端服务器相应头部的信息,可以设置在http,server或location块proxy_pass_header field透传默认nginx在响应报文中不传递后端服务器的首部字段Date, Server, X-Pad, X-Accel等参数如果要传递的话则要使用 proxy_pass_header field声明将后端服务器返回的值传递给客户端field 首部字段大小不敏感proxy_pass_request_headers on | off是否将客户端的请求头部转发给后端服务器可以设置在http,server或location块默认即为开启proxy_set_header可更改或添加客户端的请求头部信息内容并转发至后端服务器比如在后端服务器想要获取客户端的真实IP的时候就要更改每一个报文的头部proxy_connect_timeout time配置nginx服务器与后端服务器尝试建立连接的超时时间默认为60秒proxy_read_timeout time配置nginx服务器向后端服务器或服务器组发起read请求后等待的超时时间默认60sproxy_send_timeout time配置nginx项后端服务器或服务器组发起write请求后等待的超时 时间默认60sproxy_http_version 1.0用于设置nginx提供代理服务的HTTP协议的版本默认http 1.0proxy_ignore_client_abort off当客户端网络中断请求时nginx服务器中断其对后端服务器的请求。即如果此项设置为on开启则服务器会忽略客户端中断并一直等着代理服务执行返回如果设置为off则客户端中断后Nginx也会中断客户端请求并立即记录499日志默认为off。
搭建环境
一台nginx两台后端server
两台后端server下载httpd
[rootweb10 html]# echo 172.25.254.10 /var/www/html/index.html
[rootweb10 html]# systemctl restart httpd[rootweb20 html]# echo 172.25.254.20 /var/www/html/index.html
[rootweb20 html]# systemctl restart httpd[rootnginx-node1 ~]# curl 172.25.254.10
172.25.254.10
[rootnginx-node1 ~]# curl 172.25.254.20
172.25.254.20[rootweb20 html]# vim /etc/httpd/conf/httpd.conf
Listen 8080 #监听端口修改为8080[rootweb20 html]# mkdir -p /var/www/html/static
[rootweb20 html]# echo static 172.25.254.20 /var/www/html/static/index.html
[rootweb20 html]# systemctl restart httpd[rootweb10 html]# dnf install php -y
[rootweb10 html]# vim /var/www/html/index.php
?phpphpinfo();
?
[rootweb10 html]# systemctl restart httpd[rootnginx-node1 ~]# cd /usr/local/nginx/conf.d/
[rootnginx-node1 conf.d]# vim vhost.conf server {listen 80;server_name www.qwert.org;location ~ \.php$ {proxy_pass http://172.25.254.10:80;}location /static {proxy_pass http://172.25.254.20:8080;}
}
[rootnginx-node1 conf.d]# nginx -s reloadNginx反向代理的缓存功能
缓存功能默认关闭状态需要先手动配置才能启用
[rootnginx-node1 conf.d]# vim /usr/local/nginx/conf/nginx.conf
#gzip on;proxy_cache_path /usr/local/nginx/proxy_cache levels1:2:2 keys_zoneproxycache:20m inactive120 max_size1g; #添加在nginx.conf http配置段[rootnginx-node1 conf.d]# vim /usr/local/nginx/conf.d/vhost.conf
server {listen 80;server_name www.qwert.org;location ~ \.php$ {proxy_pass http://172.25.254.10:80;}location /static {proxy_pass http://172.25.254.20:8080;proxy_cache proxycache; #指明调用的缓存缓存名称为proxycacheproxy_cache_key $request_uri; ##缓存中用于“键”的内容默认值proxy_cache_key $scheme$proxy_host$request_uriproxy_cache_valid 200 302 301 10m; #定义对特定响应码的响应内容的缓存时长定义在http{...}中proxy_cache_valid any 1m; ##必须指定哪些响应码的缓存}
}
[rootnginx-node1 conf.d]# nginx -s reload[rootweb10 html]# ab -n1000 -c100 http://www.qwert.org/static/index.htmlThis is ApacheBench, Version 2.3 $Revision: 1903618 $
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/Benchmarking www.qwert.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 requestsServer Software: nginx/1.26.2
Server Hostname: www.qwert.org
Server Port: 80Document Path: /static/index.html
Document Length: 21 bytesConcurrency Level: 100
Time taken for tests: 0.070 seconds
Complete requests: 1000
Failed requests: 0
Total transferred: 272000 bytes
HTML transferred: 21000 bytes
Requests per second: 14302.88 [#/sec] (mean)
Time per request: 6.992 [ms] (mean)
Time per request: 0.070 [ms] (mean, across all concurrent requests)
Transfer rate: 3799.20 [Kbytes/sec] receivedConnection Times (ms)min mean[/-sd] median max
Connect: 0 1 0.7 1 4
Processing: 2 5 1.6 5 30
Waiting: 1 4 1.4 4 30
Total: 4 6 1.5 6 31Percentage of the requests served within a certain time (ms)50% 666% 675% 680% 790% 895% 898% 899% 9100% 31 (longest request)
[rootnginx-node1 conf.d]# cd /usr/local/nginx/
[rootnginx-node1 nginx]# ls
certs conf.d logs sbin
client_body_temp fastcgi_temp proxy_cache scgi_temp
conf html proxy_temp uwsgi_temp
[rootnginx-node1 nginx]# tree proxy_cache/
proxy_cache/0 directories, 0 files
[rootnginx-node1 nginx]# tree proxy_cache/
proxy_cache/
└── e└── 50└── 99└── 319432ef3663735a9d3cb4e0c1d9950e3 directories, 1 file
Nginx的反向代理负载均衡
Nginx可以将客户端的请求转发至单台后端服务器但是无法转发至特定的一组的服务器而且不能对后端服务器提供相应的服务器状态监测Nginx 可以基ngx_http_upstream_module模块提供服务器分组转发、权重分配、状态监测、调度算法等高级功能 [rootnginx-node1 nginx]# cd /usr/local/nginx/conf.d
[rootnginx-node1 conf.d]# vim vhost.conf
upstream webcluster {server 172.25.254.10:80 fail_timeout15s max_fails3;server 172.25.254.20:8080 fail_timeout15s max_fails3;server 172.25.254.100:80 backup;
}server {listen 80;server_name www.qwert.org;location / {proxy_pass http://webcluster;}
}
[rootnginx-node1 conf.d]# nginx -s reload测试轮询访问 [rootnginx-node1 conf.d]# vim vhost.conf
upstream webcluster {ip_hash;server 172.25.254.10:80 fail_timeout15s max_fails3;server 172.25.254.20:8080 fail_timeout15s max_fails3;#server 172.25.254.100:80 backup;
}server {listen 80;server_name www.qwert.org;location / {proxy_pass http://webcluster;}
}
[rootnginx-node1 conf.d]# nginx -s reload[rootweb10 html]# curl www.qwert.org
172.25.254.10
[rootweb10 html]# curl www.qwert.org
172.25.254.10
[rootweb10 html]# curl www.qwert.org
172.25.254.10
[rootweb10 html]# curl www.qwert.org
172.25.254.10[rootnginx-node1 conf.d]# vim vhost.conf
upstream webcluster {#ip_hash;hash $request_uri consistent;server 172.25.254.10:80 fail_timeout15s max_fails3;server 172.25.254.20:8080 fail_timeout15s max_fails3;#server 172.25.254.100:80 backup;
}server {listen 80;server_name www.qwert.org;location / {proxy_pass http://webcluster;}
}
[rootnginx-node1 conf.d]# nginx -s reload[rootweb10 html]# mkdir -p /var/www/html/static
[rootweb10 html]# echo 172.25.254.10 static /var/www/html/static/index.html[rootnginx-node1 conf.d]# vim vhost.conf
upstream webcluster {#ip_hash;#hash $request_uri consistent;hash $cookie_lee;server 172.25.254.10:80 fail_timeout15s max_fails3;server 172.25.254.20:8080 fail_timeout15s max_fails3;#server 172.25.254.100:80 backup;
}server {listen 80;server_name www.qwert.org;location / {proxy_pass http://webcluster;}
}
[rootnginx-node1 conf.d]# nginx -s reload因为hash一致性所以要与2^32取模 [rootnginx-node1 conf.d]# vim vhost.conf
upstream webcluster {#ip_hash;#hash $request_uri consistent;hash $cookie_lee;server 172.25.254.10:80 fail_timeout15s max_fails3 down;server 172.25.254.20:8080 fail_timeout15s max_fails3;#server 172.25.254.100:80 backup;
}server {listen 80;server_name www.qwert.org;location / {proxy_pass http://webcluster;}
}
[rootnginx-node1 conf.d]# nginx -s reload一直调度在web20上不会报错是因为hash环 四层负载均衡 #在两台后端server上配置dns
[rootweb10 html]# dnf install bind -y
[rootweb10 html]# 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; };recursion yes;dnssec-validation no;managed-keys-directory /var/named/dynamic;geoip-directory /usr/share/GeoIP;pid-file /run/named/named.pid;session-keyfile /run/named/session.key;/* https://fedoraproject.org/wiki/Changes/CryptoPolicy */include /etc/crypto-policies/back-ends/bind.config;
};[rootweb10 html]# vim /etc/named.rfc1912.zones省略
zone qwert.org IN {type master; file qwert.org.zone;allow-update { none; };
};
省略[rootweb10 html]# cd /var/named/
[rootweb10 named]# cp named.localhost qwert.org.zone -p
[rootweb10 named]# vim qwert.org.zone
$TTL 1DIN SOA ns.qwert.org. root.qwert.org. (0 ; serial1D ; refresh1H ; retry1W ; expire3H ) ; minimumNS ns.qwert.org.
ns A 172.25.254.10
www A 172.25.254.10[rootweb10 named]# systemctl start named
[rootweb10 named]# dig www.qwert.org 172.25.254.10; DiG 9.16.23-RH www.qwert.org 172.25.254.10
;; global options: cmd
;; Got answer:
;; -HEADER- opcode: QUERY, status: NOERROR, id: 61543
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
; COOKIE: ac76c2625c58a2b50100000066c21c673b6be54bda182c28 (good)
;; QUESTION SECTION:
;www.qwert.org. IN A;; ANSWER SECTION:
www.qwert.org. 86400 IN A 172.25.254.10;; Query time: 1 msec
;; SERVER: 172.25.254.10#53(172.25.254.10)
;; WHEN: Mon Aug 19 00:08:07 CST 2024
;; MSG SIZE rcvd: 86[rootweb10 named]# scp -p /etc/named.{conf,rfc1912.zones} root172.25.254.20:/etc/
[rootweb10 named]# scp -p /var/named/qwert.org.zone root172.25.254.20:/var/named/qwert.org.zone# 在20上修改一下配置文件
[rootweb20 html]# vim /var/named/qwert.org.zone
$TTL 1DIN SOA ns.qwert.org. root.qwert.org. (0 ; serial1D ; refresh1H ; retry1W ; expire3H ) ; minimumNS ns.qwert.org.
ns A 172.25.254.20
www A 172.25.254.20[rootweb20 named]# chgrp named qwert.org.zone
[rootweb20 named]# systemctl restart named
[rootweb20 named]# dig www.qwert.org 172.25.254.20; DiG 9.16.23-RH www.qwert.org 172.25.254.20
;; global options: cmd
;; Got answer:
;; -HEADER- opcode: QUERY, status: NOERROR, id: 25510
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
; COOKIE: 69374383c53f97260100000066c21e3117a5d87d2d22927d (good)
;; QUESTION SECTION:
;www.qwert.org. IN A;; ANSWER SECTION:
www.qwert.org. 86400 IN A 172.25.254.20;; Query time: 0 msec
;; SERVER: 172.25.254.20#53(172.25.254.20)
;; WHEN: Mon Aug 19 00:15:45 CST 2024
;; MSG SIZE rcvd: 86# 在nginx主机上编辑配置文件
[rootnginx-node1 conf.d]# vim /usr/local/nginx/conf/nginx.conf
events {worker_connections 100000;use epoll;
}
include /usr/local/nginx/tcpconf.d/*.conf;[rootnginx-node1 conf.d]# mkdir -p /usr/local/nginx/tcpconf.d/[rootnginx-node1 conf.d]# vim dns.conf
[rootnginx-node1 conf.d]# ls
dns.conf status.conf var.conf vhost.conf
[rootnginx-node1 conf.d]# mv dns.conf /usr/local/nginx/tcpconf.d/
[rootnginx-node1 conf.d]# ls
status.conf var.conf vhost.conf
[rootnginx-node1 conf.d]# cd /usr/local/nginx/tcpconf.d/
[rootnginx-node1 tcpconf.d]# ls
dns.conf
stream {upstream dns {server 172.25.254.10:53 fail_timeout15s max_fails3;server 172.25.254.20:53 fail_timeout15s max_fails3;}server {listen 53 udp reuseport;proxy_timeout 20s;proxy_pass dns;}
}
[rootnginx-node1 tcpconf.d]# 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
[rootnginx-node1 tcpconf.d]# nginx -s reload# 轮询调度解析
[rootnginx-node1 tcpconf.d]# dig www.qwert.org 172.25.254.100; DiG 9.16.23-RH www.qwert.org 172.25.254.100
;; global options: cmd
;; Got answer:
;; -HEADER- opcode: QUERY, status: NOERROR, id: 32097
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
; COOKIE: eb608cc0a3fc4e070100000066c22377c8d445162b401dd2 (good)
;; QUESTION SECTION:
;www.qwert.org. IN A;; ANSWER SECTION:
www.qwert.org. 86400 IN A 172.25.254.10;; Query time: 0 msec
;; SERVER: 172.25.254.100#53(172.25.254.100)
;; WHEN: Mon Aug 19 00:38:15 CST 2024
;; MSG SIZE rcvd: 86[rootnginx-node1 tcpconf.d]# dig www.qwert.org 172.25.254.100; DiG 9.16.23-RH www.qwert.org 172.25.254.100
;; global options: cmd
;; Got answer:
;; -HEADER- opcode: QUERY, status: NOERROR, id: 21978
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
; COOKIE: 29e380655c929b090100000066c223786b53bc42aa6617ab (good)
;; QUESTION SECTION:
;www.qwert.org. IN A;; ANSWER SECTION:
www.qwert.org. 86400 IN A 172.25.254.20;; Query time: 1 msec
;; SERVER: 172.25.254.100#53(172.25.254.100)
;; WHEN: Mon Aug 19 00:38:16 CST 2024
;; MSG SIZE rcvd: 86#数据库在两台后端server上安装mariadb-server
[rootweb10 named]# dnf install mariadb-server -y
[rootweb10 named]# vim /etc/my.cnf.d/mariadb-server.cnf
[mysqld]
server-id10
datadir/var/lib/mysql
socket/var/lib/mysql/mysql.sock
log-error/var/log/mariadb/mariadb.log
pid-file/run/mariadb/mariadb.pid[rootweb10 named]# systemctl start mariadb.service
[rootweb20 named]# vim /etc/my.cnf.d/mariadb-server.cnf
[mysqld]
server-id20
datadir/var/lib/mysql
socket/var/lib/mysql/mysql.sock
log-error/var/log/mariadb/mariadb.log
pid-file/run/mariadb/mariadb.pid[rootweb20 named]# systemctl start mariadb.service
[rootweb10 named]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 10.5.22-MariaDB MariaDB ServerCopyright (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 qwert% identified by qwert;
Query OK, 0 rows affected (0.001 sec)MariaDB [(none)] GRANT ALL ON *.* to qwert%;
Query OK, 0 rows affected (0.001 sec)[rootweb20 named]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 10.5.22-MariaDB MariaDB ServerCopyright (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 qwert% identified by qwert;
Query OK, 0 rows affected (0.001 sec)MariaDB [(none)] GRANT ALL ON *.* to qwert%;
Query OK, 0 rows affected (0.001 sec)MariaDB [(none)] [rootnginx-node1 tcpconf.d]# vim dns.conf
stream {upstream dns {server 172.25.254.10:53 fail_timeout15s max_fails3;server 172.25.254.20:53 fail_timeout15s max_fails3;}upstream mysql {server 172.25.254.10:3306 fail_timeout15s max_fails3;server 172.25.254.20:3306 fail_timeout15s max_fails3;}server {listen 3306;proxy_timeout 60s;proxy_pass mysql;}server {listen 53 udp reuseport;proxy_timeout 20s;proxy_pass dns;}
}
[rootnginx-node1 tcpconf.d]# nginx -s reload
[rootnginx-node1 tcpconf.d]# netstat -antlupe | grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 0 209412 55887/nginx: master
[rootnginx-node1 tcpconf.d]# dnf install mariadb -y#测试
[rootnginx-node1 tcpconf.d]# mysql -u qwert -p -h 172.25.254.100
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 10.5.22-MariaDB MariaDB ServerCopyright (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.003 sec)MariaDB [(none)] exit
Bye
[rootnginx-node1 tcpconf.d]# mysql -u qwert -p -h 172.25.254.100
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 10.5.22-MariaDB MariaDB ServerCopyright (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)MariaDB [(none)] exit
Bye实现FastCGI
FastCGI介绍
**CGI**通用网关接口Common Gateway Interface/CGI描述了客户端和服务器程序之间传输数据的一种标准可以让一个客户端从网页浏览器向执行在网络服务器上的程序请求数据。CGI 独立于任何语言的CGI 程序可以用任何脚本语言或者是完全独立编程语言实现只要这个语言可以在这个系统上运行。 用户通过浏览器访问服务器, 发送了一个请求, 请求的url如上服务器接收数据, 对接收的数据进行解析nginx对于一些登录数据不知道如何处理, nginx将数据发送给了cgi程序服务器端会创建一个cgi进程CGI进程执行 加载配置, 如果有需求加载配置文件获取数据连接其他服务器: 比如数据库逻辑处理得到结果 将结果发送给服务器线程结束生命退出 服务器将cgi处理结果发送给客户端弊端在服务器端CGI进程会被频繁的创建销毁服务器开销大, 效率低
为什么会有FastCGI
CGI协议虽然解决了语言解析器和 Web Server 之间通讯的问题但是它的效率很低因为 Web Server每收到一个请求都会创建一个CGI进程PHP解析器都会解析php.ini文件初始化环境请求结束的时候再关闭进程对于每一个创建的CGI进程都会执行这些操作所以效率很低而FastCGI是用来提高CGI性能的FastCGI每次处理完请求之后不会关闭掉进程而是保留这个进程使这个进程可以处理多个请求。这样的话每个请求都不用再重新创建一个进程了大大提升了处理效率。
什么是PHP-FPM
PHP-FPM(FastCGI Process Manager FastCGI进程管理器)是一个实现了Fastcgi的程序并且提供进程管理的功能。 进程包括master进程和worker进程。master进程只有一个负责监听端口接受来自web server的请求 worker进程一般会有多个每个进程中会嵌入一个PHP解析器进行PHP代码的处理。
FastCGI配置指令
Nginx基于模块ngx_http_fastcgi_module实现通过fastcgi协议将指定的客户端请求转发至php-fpm处理其配置指令如下
#转发请求到后端服务器address为后端的fastcgi server的地址可用位置location, if in location
fastcgi_pass address:port;#fastcgi默认的主页资源示例fastcgi_index index.php;
fastcgi_index name;#设置传递给FastCGI服务器的参数值可以是文本变量或组合可用于将Nginx的内置变量赋值给自定义key
fastcgi_param parameter value [if_not_empty];fastcgi_param REMOTE_ADDR $remote_addr; #客户端源IP
fastcgi_param REMOTE_PORT $remote_port; #客户端源端口
fastcgi_param SERVER_ADDR $server_addr; #请求的服务器IP地址
fastcgi_param SERVER_PORT $server_port; #请求的服务器端口
fastcgi_param SERVER_NAME $server_name; #请求的server nameNginx默认配置示例location ~ \.php$ {root /scripts;fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; #默认脚本路径#fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi_params; #此文件默认系统已提供,存放的相对路径为prefix/conf
}FastCGI实战案例
源码编译php
# 解决php依赖问题
[rootnginx-node1 ~]# ls
anaconda-ks.cfg nginx-1.26.2.tar.gz
Desktop oniguruma-devel-6.9.6-1.el9.5.x86_64.rpm
Documents package.xml
Downloads php-8.3.9
echo-nginx-module-0.63 php-8.3.9.tar.gz
echo-nginx-module-0.63.tar.gz Pictures
memcache-8.2 Public
memcache-8.2.tgz srcache-nginx-module-0.33
memc-nginx-module-0.20 srcache-nginx-module-0.33.tar.gz
memc-nginx-module-0.20.tar.gz Templates
Music Videos
nginx-1.26.2
[rootnginx-node1 ~]# yum install -y bzip2 systemd-devel libxml2-devel sqlite-devel libpng-devel libcurl-devel oniguruma-devel# 解压源码并安装
[rootnginx-node1 ~]# cd nginx-1.26.2/
[rootnginx-node1 nginx-1.26.2]# ./configure \
--prefix/usr/local/php \ #安装路径
--with-config-file-path/usr/local/php/etc \ #指定配置路径
--enable-fpm \ #用cgi方式启动程序
--with-fpm-usernginx \ #指定运行用户身份
--with-fpm-groupnginx \
--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 管理cgiphp相关配置
[rootnginx-node1 ~]# cd /usr/local/php/etc
[rootnginx-node1 ~]# cd /usr/local/php/etc/
[rootnginx-node1 etc]# cp php-fpm.conf.default php-fpm.conf
[rootnginx-node1 etc]# vim php-fpm.conf
#去掉注释
pid run/php-fpm.pid #指定pid文件存放位置[rootnginx-node1 etc]# cd php-fpm.d/
[rootnginx-node1 php-fpm.d]# cp www.conf.default www.conf#生成主配置文件
[rootnginx-node1 php-fpm.d]# cd /root/php-8.3.9/
[rootnginx-node1 php-8.3.9]# cp php.ini-production /usr/local/php/etc/php.ini
[rootnginx-node1 ~]# vim /usr/local/php/etc/php.ini
[Date]
; Defines the default timezone used by the date functions
; https://php.net/date.timezone
date.timezone Asia/Shanghai #修改时区#生成启动文件
[rootnginx-node1 ~]# cd /root/php-8.3.9/
[rootnginx-node1 php-8.3.9]# cp sapi/fpm/php-fpm.service /lib/systemd/system/
# Mounts the /usr, /boot, and /etc directories read-only for processes invoked by
this unit.
#ProtectSystemfull #注释该内容
[rootnginx-node1 php-8.3.9]# systemctl start php-fpm.service准备php测试页面
[rootnginx-node1 ~]# mkdir -p /data/web/php
[rootnginx-node1 ~]# cat /data/web/php/index.php #php测试页面
?php
phpinfo();
?Nginx配置转发
[rootnginx-node1 ~]# vim /usr/local/nginx/conf.d/vhost.conf
server {listen 80;server_name www.qwert.org;root /data/web/html;index index.html;location ~ \.php$ {root /data/web/php;fastcgi_pass 172.25.254.100:9000;fastcgi_index index.php;include fastcgi.conf;}
}
[rootnginx-node1 ~]# nginx -s reload
[rootnginx-node1 ~]# systemctl restart php-fpm.service访问验证php测试页面 添加php环境变量
[rootnginx-node1 ~]# vim ~/.bash_profile
# .bash_profile# Get the aliases and functions
if [ -f ~/.bashrc ]; then. ~/.bashrc
fi# User specific environment and startup programs
export PATH$PATH:/usr/local/nginx/sbin:/usr/local/php/bin:/usr/local/php/sbin[rootnginx-node1 ~]# source ~/.bash_profilephp的动态扩展模块php的缓存模块 安装memcache模块
[rootnginx-node1 ~]# tar zxf memcache-8.2.tgz
[rootnginx-node1 ~]# cd memcache-8.2/
[rootnginx-node1 memcache-8.2]# yum install autoconf -y
[rootnginx-node1 memcache-8.2]# phpize
[rootnginx-node1 memcache-8.2]# ./configure make make install
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20230831/
[rootnginx-node1 memcache-8.2]# ls /usr/local/php/lib/php/extensions/no-debug-non-zts-
20230831/
memcache.so opcache.so复制测试文件到nginx发布目录中
[rootnginx-node1 memcache-8.2]# cp example.php memcache.php /data/web/php/
[rootnginx-node1 memcache-8.2]# vim /data/web/php/memcache.php
$VERSION$Id$;define(ADMIN_USERNAME,admin); // Admin Username
define(ADMIN_PASSWORD,lee); // Admin Password
define(DATE_FORMAT,Y/m/d H:i:s);
define(GRAPH_SIZE,200);
define(MAX_ITEM_DUMP,50);$MEMCACHE_SERVERS[] 127.0.0.1:11211; // add more as an array
#$MEMCACHE_SERVERS[] mymemcache-server2:11211; // add more as an array配置php加载memcache模块
[rootnginx-node1 ~]# vim /usr/local/php/etc/php.ini
;extensionzip
extensionmemcache
;zend_extensionopcache
[rootnginx-node1 ~]# systemctl reload php-fpm
[rootnginx-node1 no-debug-non-zts-20230831]# php -m | grep mem
memcache部署memcached
[rootnginx-node1 ~]# yum install memcached -y
[rootnginx-node1 ~]# systemctl enable --now memcached.service
[rootnginx-node1 ~]# netstat -antlupe | grep memcache
tcp 0 0 127.0.0.1:11211 0.0.0.0:* LISTEN 980 180281 191583/memcached
tcp6 0 0 ::1:11211 :::* LISTEN 980 180282 191583/memcached [rootnginx-node1 ~]# cat /etc/sysconfig/memcached
PORT11211
USERmemcached
MAXCONN1024
CACHESIZE64
OPTIONS-l 127.0.0.1,::1#性能对比
[rootnginx-node1 ~]# ab -n500 -c10 http://www.qwert.org/index.php
Concurrency Level: 10
Time taken for tests: 0.514 seconds
Complete requests: 500
Failed requests: 44
(Connect: 0, Receive: 0, Length: 44, Exceptions: 0)[rootnginx-node1 ~]# ab -n500 -c10 http://www.qwert.org/index.php
Concurrency Level: 10
Time taken for tests: 0.452 seconds
Complete requests: 500
Failed requests: 0测试 php高速缓存 部署方法
在我们安装的nginx中默认不支持memc和srcache功能需要借助第三方模块来让nginx支持此功能所以nginx需要重新编译
[rootnginx-node1 ~]# tar zxf srcache-nginx-module-0.33.tar.gz
[rootnginx-node1 ~]# vim /usr/local/nginx/conf.d/vhost.conf
upstream memcache {server 127.0.0.1:11211;keepalive 512;
}server {listen 80;server_name www.qwert.org;root /data/web/html;index 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$ {root /data/web/php;set $key $uri$args;srcache_fetch GET /memc $key;srcache_store PUT /memc $key;fastcgi_pass 172.25.254.100:9000;fastcgi_index index.php;include fastcgi.conf;}
}
[rootnginx-node1 ~]# nginx -s reload#测试结果
[rootnginx-node1 ~]# ab -n500 -c10 http://www.qwert.org/index.php
Concurrency Level: 10
Time taken for tests: 0.255 seconds
Complete requests: 500
Failed requests: 0nginx二次开发版本
[rootnginx-node1 ~]# killall -9 nginx
[rootnginx-node1 ~]# ps aux | grep nginx
nginx 198165 0.0 0.9 265356 17824 ? S 18:01 0:00 php-fpm: pool www
nginx 198166 0.0 0.9 265356 16288 ? S 18:01 0:00 php-fpm: pool www
root 204714 0.0 0.1 221664 2432 pts/3 S 20:52 0:00 grep --colorauto nginx[rootnginx-node1 ~]# dnf -yq install gcc pcre-devel openssl-devel perl
[rootnginx-node1 ~]# useradd -r -s /sbin/nologin nginx
[rootnginx-node1 ~]# cd /usr/local/src
[rootnginx-node1 src]# wget https://openresty.org/download/openresty-1.25.3.1.tar.gz
[rootnginx-node1 src]# tar zxf openresty-1.25.3.1.tar.gz
[rootnginx-node1 src]# cd openresty-1.25.3.1/
[rootnginx-node1 openresty-1.25.3.1]# ./configure \
--prefix/usr/local/openresty \
--with-stream_realip_module \
--with-http_realip_module \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre \
--with-http_sub_module \
--without-http_memcached_module \
--with-stream \
--with-stream_ssl_module [rootnginx-node1 openresty-1.25.3.1]# gmake -j2 gmake install
[rootnginx-node1 openresty-1.25.3.1]# cd /usr/local/openresty/
[rootnginx-node1 openresty]# ls
bin COPYRIGHT luajit lualib nginx pod resty.index site
[rootnginx-node1 openresty]# cd bin/
[rootnginx-node1 bin]# ls
md2pod.pl nginx-xml2pod openresty opm resty restydoc restydoc-index
[rootnginx-node1 bin]# ll
total 168
-rwxr-xr-x 1 root root 19185 Aug 19 21:12 md2pod.pl
-rwxr-xr-x 1 root root 15994 Aug 19 21:12 nginx-xml2pod
lrwxrwxrwx 1 root root 37 Aug 19 21:12 openresty - /usr/local/openresty/nginx/sbin/nginx
-rwxr-xr-x 1 root root 63650 Aug 19 21:12 opm
-rwxr-xr-x 1 root root 36881 Aug 19 21:12 resty
-rwxr-xr-x 1 root root 14957 Aug 19 21:12 restydoc
-rwxr-xr-x 1 root root 8873 Aug 19 21:12 restydoc-index
[rootnginx-node1 bin]# vim ~/.bash_profile
# .bash_profile# Get the aliases and functions
if [ -f ~/.bashrc ]; then. ~/.bashrc
fi# User specific environment and startup programs
export PATH$PATH:/usr/local/nginx/sbin:/usr/local/php/bin:/usr/local/php/sbin:/usr/
local/openresty/bin
[rootnginx-node1 bin]# source ~/.bash_profile
[rootnginx-node1 bin]# openresty
[rootnginx-node1 bin]# netstat -antlulpe | grep 80
tcp 0 0 127.0.0.1:11211 0.0.0.0:* LISTEN 980 180281 191583/memcached
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 0 227829 215315/nginx: maste
tcp6 0 0 ::1:11211 :::* LISTEN 980 180282 191583/memcached