当前位置: 首页 > news >正文

济宁建设企业网站网站建设找什么工作室

济宁建设企业网站,网站建设找什么工作室,网站用户体验优化,ps做图下载网站目录 一、简介 #xff08;一#xff09;nginx-高性能的web服务端 #xff08;二#xff09;用户访问体验 二、I/O模型 #xff08;一#xff09;概念 #xff08;二#xff09;网络I/O模型 #xff08;三#xff09;阻塞型 I/O 模型 #xff08;四#xf…目录 一、简介 一nginx-高性能的web服务端 二用户访问体验 二、I/O模型 一概念 二网络I/O模型 三阻塞型 I/O 模型 四非阻塞型 I/O 模型 (nonblocking IO) 五多用服用I/O型 六 信号驱动式 I/O 模型 (signal-driven IO 七异步 I/O 模型 (asynchronous IO) 三、nginx 一功能介绍 二基础特性 三服务相关内容 四进程结构 五进程间通信 六模块介绍 七版本信息 四、实验 一、nginx的源码编译安装 二、平滑升级和回滚 三、nginx常用参数 四、nginx服务的启动脚本 五、nginx全局参数优化 六、lnginx配置中的root和alias 七、location的用法以及优先级 1、定义 2、语法规则 3、优先级测试 八、nginx的用户认证 九、自定义 一自定义错误页面 二自定义日志 十、检测文件是否存在 十一、nginx中长连接的管理 十二、nginx下载服务器的设定优化 十三、nginx状态页 十四、nginx数据压缩功能 十五、nginx内键变量 十六、nginx的rewrite模块功能 十七、nginx-rewrite的企业示例及防盗链 十八、nginx的反向代理及动静分离 二、fastcginginx和php共存 一、源码编译php 二、php的配置 三、nginx和php的整合 四、nginx-php缓存 五、php高速缓存 一、简介 一nginx-高性能的web服务端 Nginx是由1994年毕业于俄罗斯国立莫斯科鲍曼科技大学的同学为俄罗斯rambler.ru公司开发的开发 工作最早从2002年开始第一次公开发布时间是2004年10月4日版本号是0.1.0 2019年3月11日F5 与 NGINX达成协议,F5 将收购 NGINX 的所有已发行股票总价值约为 6.7 亿美元。 6.7亿美金约合44.97亿人民币,nginx核心模块代码长度198430包括空格、注释所以一行代码约为 2.2万人民币 官网地址 www.nginx.org Nginx历经十几年的迭代更新https://nginx.org/en/CHANGES 目前功能已经非常完善且运行稳 定另外Nginx的版本分为开发版、稳定版和过期版nginx以功能丰富著称它即可以作为http服务 器也可以作为反向代理服务器或者邮件服务器能够快速的响应静态网页的请求 支持FastCGI/SSL/Virtual Host/URL Rwrite /Gzip / HTTP Basic Auth/http或者TCP的负载均衡(1.9版本以 上且开启stream模块)等功能并且支持第三方的功能扩展。 天猫 淘宝 京东 小米 163 新浪等一线互联网公司都在用Nginx或者进行二次开发 基于Nginx的工作场景 二用户访问体验 用户访问体验统计 互联网存在用户速度体验的1-3-10原则即1秒最优1-3秒较优3~10秒比较慢10秒以上用户无法接 受。用户放弃一个产品的代价很低只是换一个URL而已。 全球最大搜索引擎 Google慢500ms 20% 将放弃访问。 全球最大的电商零售网站亚马逊慢100ms 1% 将放弃交易 有很多研究都表明性能对用户的行为有很大的影响 79%的用户表示不太可能再次打开一个缓慢的网站 47%的用户期望网页能在2秒钟以内加载 40%的用户表示如果加载时间超过三秒钟就会放弃这个网站 页面加载时间延迟一秒可能导致转换损失7%页面浏览量减少11% 8秒定律用户访问一个网站时如果等待网页打开的时间超过8秒会有超过30%的用户放弃等待 影响用户体验的因素 1.客户端 客户端硬件配置 客户端网络速率 客户端与服务端距离 2.服务器 服务端网络速率 服务端硬件配置 服务端架构设计 服务端应用程序工作模式 服务端并发数量服务端响应文件大小及数量 buffer cache 服务端I/O压力1.2.4 服务端 I/O 流程 二、I/O模型 一概念 同步/异步关注的是消息通信机制即调用者在等待一件事情的处理结果时被调用者是否提供完成状 态的通知。 同步synchronous被调用者并不提供事件的处理结果相关的通知消息需要调用者主动询问事 情是否处理完成 异步asynchronous被调用者通过状态、通知或回调机制主动通知调用者被调用者的运行状态 阻塞/非阻塞关注调用者在等待结果返回之前所处的状态 阻塞blocking指IO操作需要彻底完成后才返回到用户空间调用结果返回之前调用者被挂 起干不了别的事情。 非阻塞nonblocking指IO操作被调用后立即返回给用户一个状态值而无需等到IO操作彻底完 成在最终的调用结果返回之前调用者不会被挂起可以去做别的事情。 二网络I/O模型 阻塞型、非阻塞型、复用型、信号驱动型、异步 三阻塞型 I/O 模型 阻塞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/O 模型 (nonblocking IO) 用户线程发起IO请求时立即返回。但并未读取到任何数据用户线程需要不断地发起IO请求直到数据 到达后才真正读取到数据继续执行。即 “轮询”机制存在两个问题如果有大量文件描述符都要等 那么就得一个一个的read。这会带来大量的Context Switchread是系统调用每调用一次就得在用户 态和核心态切换一次。轮询的时间不好把握。这里是要猜多久之后数据才能到。等待时间设的太长 程序响应延迟就过大;设的太短就会造成过于频繁的重试干耗CPU而已是比较浪费CPU的方式一 般很少直接使用这种模型而是在其他IO模型中使用非阻塞IO这一特性。 非阻塞程序向内核发送请I/O求后一直等待内核响应如果内核处理请求的IO操作不能立即返回IO结 果进程将不再等待而且继续处理其他请求但是仍然需要进程隔一段时间就要查看内核I/O是否完 成。 查看上图可知在设置连接为非阻塞时当应用进程系统调用 recvfrom 没有数据返回时内核会立即返 回一个 EWOULDBLOCK 错误而不会一直阻塞到数据准备好。如上图在第四次调用时有一个数据报准 备好了所以这时数据会被复制到 应用进程缓冲区 于是 recvfrom 成功返回数据 当一个应用进程这样循环调用 recvfrom 时称之为轮询 polling 。这么做往往会耗费大量CPU时间实 际使用很少 五多用服用I/O型 上面的模型中,每一个文件描述符对应的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复用因此异步非阻塞使用最多的一种通信方式。 三、nginx 一功能介绍 静态的web资源服务器html图片jscsstxt等静态资源 http/https协议的反向代理 结合FastCGI/uWSGI/SCGI等协议反向代理动态资源请求 tcp/udp协议的请求转发反向代理 imap4/pop3协议的反向代理 二基础特性 模块化设计较好的扩展性 高可靠性 支持热部署不停机更新配置文件升级版本更换日志文件 低内存消耗10000个keep-alive连接模式下的非活动连接仅需2.5M内存 event-driven,aio,mmapsendfile 三服务相关内容 虚拟主机server 支持 keep-alive 和管道连接(利用一个连接做多次请求) 访问日志支持基于日志缓冲提高其性能 url rewirte 路径别名 基于IP及用户的访问控制 支持速率限制及并发数限制 重新配置和在线升级而无须中断客户的工作进程 四进程结构 多进程方式服务器每接收到一个客户端请求就有服务器的主进程生成一个子进程响应客户端直 到用户关闭连接这样的优势是处理速度快子进程之间相互独立但是如果访问过大会导致服务 器资源耗尽而无法提供请求 多线程方式与多进程方式类似但是每收到一个客户端请求会有服务进程派生出一个线程和此客 户端进行交互一个线程的开销远远小于一个进程因此多线程方式在很大程度减轻了web服务器 对系统资源的要求但是多线程也有自己的缺点即当多个线程位于同一个进程内工作的时候可 以相互访问同样的内存地址空间所以他们相互影响一旦主进程挂掉则所有子线程都不能工作 了IIS服务器使用了多线程的方式需要间隔一段时间就重启一次才能稳定。 Nginx是多进程组织模型而且是一个由Master主进程和Worker工作进程组成。 主进程(master process)的功能 对外接口接收外部的操作信号 对内转发根据外部的操作的不同通过信号管理 Worker 监控监控 worker 进程的运行状态worker 进程异常终止后自动重启 worker 进程 读取Nginx 配置文件并验证其有效性和正确性 建立、绑定和关闭socket连接 按照配置生成、管理和结束工作进程 接受外界指令比如重启、升级及退出服务器等指令 不中断服务实现平滑升级重启服务并应用新的配置 开启日志文件获取文件描述符 不中断服务实现平滑升级升级失败进行回滚处理 编译和处理perl脚本 工作进程worker process的功能 所有 Worker 进程都是平等的 实际处理网络请求由 Worker 进程处理 Worker进程数量一般设置为核心数充分利用CPU资源同时避免进程数量过多导致进程竞争 CPU资源 增加上下文切换的损耗 接受处理客户的请求 将请求依次送入各个功能模块进行处理 I/O调用获取响应数据 与后端服务器通信接收后端服务器的处理结果 缓存数据访问缓存索引查询和调用缓存数据 发送请求结果响应客户的请求 接收主程序指令比如重启、升级和退出等 五进程间通信 工作进程是由主进程生成的主进程使用fork()函数在Nginx服务器启动过程中主进程根据配置文件决 定启动工作进程的数量然后建立一张全局的工作表用于存放当前未退出的所有的工作进程主进程生 成工作进程后会将新生成的工作进程加入到工作进程表中并建立一个单向的管道并将其传递给工作进 程该管道与普通的管道不同它是由主进程指向工作进程的单向通道包含了主进程向工作进程发出 的指令、工作进程ID、工作进程在工作进程表中的索引和必要的文件描述符等信息。 主进程与外界通过信号机制进行通信当接收到需要处理的信号时它通过管道向相关的工作进程发送 正确的指令每个工作进程都有能力捕获管道中的可读事件当管道中有可读事件的时候工作进程就 会从管道中读取并解析指令然后采取相应的执行动作这样就完成了主进程与工作进程的交互 六模块介绍 nginx 有多种模块 核心模块是 Nginx 服务器正常运行必不可少的模块提供错误日志记录 、配置文件解析 、事件 驱动机制 、进程管理等核心功能 标准HTTP模块提供 HTTP 协议解析相关的功能比如 端口配置 、 网页编码设置 、 HTTP响应 头设置 等等 可选HTTP模块主要用于扩展标准的 HTTP 功能让 Nginx 能处理一些特殊的服务比如 Flash 多媒体传输 、解析 GeoIP 请求、 网络传输压缩 、 安全协议 SSL 支持等 邮件服务模块主要用于支持 Nginx 的 邮件服务 包括对 POP3 协议、 IMAP 协议和 SMTP协议的 支持 Stream服务模块: 实现反向代理功能,包括TCP协议代理 第三方模块是为了扩展 Nginx 服务器应用完成开发者自定义功能比如 Json 支持、 Lua 支 持等 nginx高度模块化但其模块早期不支持DSO机制;1.9.11 版本支持动态装载和卸载 七版本信息 Nginx版本 Mainline version 主要开发版本,一般为奇数版本号,比如1.19 Stable version 当前最新稳定版,一般为偶数版本,如:1.20 Legacy versions 旧的稳定版,一般为偶数版本,如:1.18 Nginx安装可以使用yum或源码安装但是推荐使用源码编译安装 yum的版本比较旧 编译安装可以更方便自定义相关路径 使用源码编译可以自定义相关功能更方便业务的上的使用 四、实验 一、nginx的源码编译安装 源码安装需要提前准备标准的编译器GCC的全称是GNU Compiler collection其有GNU开发并以 GPL即LGPL许可是自由的类UNIX即苹果电脑Mac OS X操作系统的标准编译器因为GCC原本只能处理C语 言所以原名为GNU C语言编译器后来得到快速发展可以处理C,Fortranpascalobjective C java以及Ada等其他语言此外还需要Automake工具以完成自动创建Makefile的工作Nginx的一些模块 需要依赖第三方库比如: pcre支持rewritezlib支持gzip模块和openssl支持ssl模块 等。 首先下载压缩包并放入虚拟机中而后进行解压 解压 [rootnode1 ~]# tar zxf nginx-1.24.0.tar.gz [rootnode1 ~]# cd nginx-1.24.0/下载gcc编译器和其他依赖的开发包 [rootnode1 ~]# dnf install gcc pcre-devel zlib-devel openssl-devel -y[rootnode1 nginx-1.24.0]#./configure ./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检测 [rootnode1 nginx-1.24.0]# make make clean如果想重新检测课执行还原文件以下位下载之后所生的文件 [rootnode1 nginx-1.24.0]# cd objs/ [rootnode1 objs]# ls autoconf.err nginx ngx_auto_config.h ngx_modules.c src Makefile nginx.8 ngx_auto_headers.h ngx_modules.o [rootnode1 objs]# cd src/ [rootnode1 src]# ls core event http mail misc os stream [rootnode1 src]# cd stream/ [rootnode1 stream]# ls ngx_stream_access_module.o ngx_stream_proxy_module.o ngx_stream_upstream.o ngx_stream_core_module.o ngx_stream_return_module.o ngx_stream_upstream_random_module.o ngx_stream_geo_module.o ngx_stream_script.o ngx_stream_upstream_round_robin.o ngx_stream_handler.o ngx_stream_set_module.o ngx_stream_upstream_zone_module.o ngx_stream_limit_conn_module.o ngx_stream_split_clients_module.o ngx_stream_variables.o ngx_stream_log_module.o ngx_stream_ssl_module.o ngx_stream_write_filter_module.o ngx_stream_map_module.o ngx_stream_upstream_hash_module.o ngx_stream.o ngx_stream_upstream_least_conn_module.o将objs里的内容拷贝到形同指定的文件下 [rootnode1 nginx-1.24.0]# make install 创建nginx用户 [rootnode1 sbin]# useradd -s /sbin/nologin/nginx -M nginx[rootnode1 sbin]# id nginx 用户id1001(nginx) 组id1001(nginx) 组1001(nginx)启动nginx [rootnode1 sbin]# ./nginx 查看进程 [rootnode1 sbin]# ps aux | grep nginx root 1705 0.0 0.0 9864 2052 ? Ss 20:52 0:00 nginx: master process ./nginx 管理进程 nginx 1706 0.0 0.1 14196 5252 ? S 20:52 0:00 nginx: worker process 工作进程 root 1820 0.0 0.0 221680 2304 pts/1 S 21:09 0:00 grep --colorauto nginx端口 [rootnode1 sbin]# netstat -antlupe | grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 0 25493 1705/nginx: master访问 [rootnode1 ~]# curl 172.25.254.200 !DOCTYPE html html head titleWelcome to nginx!/title style html { color-scheme: light dark; } body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } /style /head body h1Welcome to nginx!/h1 pIf you see this page, the nginx web server is successfully installed and working. Further configuration is required./ppFor online documentation and support please refer to a hrefhttp://nginx.org/nginx.org/a.br/ Commercial support is available at a hrefhttp://nginx.com/nginx.com/a./ppemThank you for using nginx./em/p /body /html查看nginx的大小 [rootnode1 sbin]# du -sh nginx 5.4M    nginx关闭nginx [rootnode1 ~]# /usr/local/nginx/sbin/nginx -s stop 查看是否关闭 [rootnode1 ~]# netstat -antlupe | grep nginx因为nginx太大所以建议关闭debug [rootnode1 nginx-1.24.0]# vim auto/cc/gcc # debug #CFLAGS$CFLAGS -g 关闭之后重新检测 [rootnode1 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 [rootnode1 nginx-1.24.0]# make make install 把nginx软件的命令执行路径添加到环境变量中 [rootnode1 nginx-1.24.0]# vim ~/.bash_profile [rootnode1 nginx-1.24.0]# cat ~/.bash_profile # .bash_profile# Get the aliases and functions if [ -f ~/.bashrc ]; then. ~/.bashrc fi# User specific environment and startup programsexport PATH$PATH:/usr/local/nginx/sbin[rootnode1 nginx-1.24.0]# source ~/.bash_profile 再次查看nginx的大小明显比之前小了很多 [rootnode1 nginx-1.24.0]# du -sh /usr/local/nginx/sbin/nginx 1.2M /usr/local/nginx/sbin/nginx 配置文件 [rootnode1 conf]# vim /usr/local/nginx/conf/nginx.conf[rootnode1 sbin]# curl -I 172.25.254.200 HTTP/1.1 200 OK Server: nginx/1.24.0 Date: Thu, 15 Aug 2024 15:41:51 GMT Content-Type: text/html Content-Length: 615 Last-Modified: Thu, 15 Aug 2024 14:15:54 GMT Connection: keep-alive ETag: 66be0d9a-267 Accept-Ranges: bytes[rootnode1 sbin]# 二、平滑升级和回滚 有时候我们需要对Nginx版本进行升级以满足对其功能的需求例如添加新模块需要新功能而此时 Nginx又在跑着业务无法停掉这时我们就可能选择平滑升级 流程 将旧Nginx二进制文件换成新Nginx程序文件注意先备份) 向master进程发送USR2信号 master进程修改pid文件名加上后缀.oldbin,成为nginx.pid.oldbin master进程用新Nginx文件启动新master进程成为旧master的子进程,系统中将有新旧两个Nginx主 进程共同提供Web服务,当前新的请求仍然由旧Nginx的worker进程进行处理,将新生成的master进 程的PID存放至新生成的pid文件nginx.pid 向旧的Nginx服务进程发送WINCH信号使旧的Nginx worker进程平滑停止 向旧master进程发送QUIT信号关闭老master并删除Nginx.pid.oldbin文件 如果发现升级有问题,可以回滚∶向老master发送HUP向新master发送QUIT 添加新版本和模块 隐藏版本信息修改以下的配置文件 [rootnode1 core]# pwd /root/nginx-1.24.0/src/core 配置文件路径 [rootnode1 core]# vim nginx.h 修改的文件#define nginx_version 1024000 #define NGINX_VERSION 1.24.0 可将1.24.0修改你想要的地址 #define NGINX_VER nginx/ NGINX_VERSION解压生成文件 [rootnode1 ~]# tar zxf echo-nginx-module-0.63.tar.gz [rootnode1 ~]# tar zxf nginx-1.26.2.tar.gz [rootnode1 ~]# ls 公共 文档 anaconda-ks.cfg nginx-1.24.0.tar.gz 模板 下载 echo-nginx-module-0.63 nginx-1.26.1.tar.gz 视频 音乐 echo-nginx-module-0.63.tar.gz nginx-1.26.2 图片 桌面 nginx-1.24.0 nginx-1.26.2.tar.gz编译 [rootnode1 nginx-1.26.2]# make 编译完成之后生成的文件 [rootnode1 nginx-1.26.2]# cd objs/ [rootnode1 objs]# ls addon Makefile nginx.8 ngx_auto_headers.h ngx_modules.o autoconf.err nginx ngx_auto_config.h ngx_modules.c src备份 [rootnode1 sbin]# cp nginx nginx.bak [rootnode1 sbin]# ls nginx nginx.bak nginx.old 将新生成的文件进行覆盖 [rootnode1 sbin]# \cp -f /root/nginx-1.26.2/objs/nginx /usr/local/nginx/sbin/nginx [rootnode1 sbin]# kill -USR2 5708 [rootnode1 sbin]# ps aux | grep nginx root 5708 0.0 0.0 10060 2108 ? Ss 13:46 0:00 nginx: master process nginx nginx 5709 0.0 0.1 14384 5180 ? S 13:46 0:00 nginx: worker process nginx 5710 0.0 0.1 14384 5692 ? S 13:46 0:00 nginx: worker process root 5750 0.0 0.1 9764 6528 ? S 13:53 0:00 nginx: master process nginx nginx 5751 0.0 0.1 14228 4888 ? S 13:53 0:00 nginx: worker process root 5753 0.0 0.0 221680 2304 pts/0 S 13:53 0:00 grep --colorauto nginx[rootnode1 sbin]# kill -WINCH 5708 [rootnode1 sbin]# ps aux | grep nginx root 5708 0.0 0.0 10060 2108 ? Ss 13:46 0:00 nginx: master process nginx root 5750 0.0 0.1 9764 6528 ? S 13:53 0:00 nginx: master process nginx nginx 5751 0.0 0.1 14228 4888 ? S 13:53 0:00 nginx: worker process root 5757 0.0 0.0 221680 2304 pts/0 S 13:56 0:00 grep --colorauto nginx [rootnode1 sbin]# curl -I 172.25.254.200 HTTP/1.1 200 OK Server: nginx/1.26.2 Date: Sat, 17 Aug 2024 05:57:22 GMT Content-Type: text/html Content-Length: 615 Last-Modified: Sat, 17 Aug 2024 05:45:49 GMT Connection: keep-alive ETag: 66c0390d-267 Accept-Ranges: bytes 恢复老版本 [rootnode1 sbin]# kill -HUP 5708 [rootnode1 sbin]# ps aux | grep nginx root 5708 0.0 0.0 10060 2108 ? Ss 13:46 0:00 nginx: master process nginx root 5750 0.0 0.1 9764 6528 ? S 13:53 0:00 nginx: master process nginx nginx 5751 0.0 0.1 14228 5272 ? S 13:53 0:00 nginx: worker process nginx 5800 0.0 0.1 14384 5180 ? S 15:17 0:00 nginx: worker process nginx 5801 0.0 0.1 14384 5180 ? S 15:17 0:00 nginx: worker process root 5803 0.0 0.0 221680 2304 pts/0 S 15:17 0:00 grep --colorauto nginx [rootnode1 sbin]# kill -WINCH 5750 [rootnode1 sbin]# ps aux | grep nginx root 5708 0.0 0.0 10060 2108 ? Ss 13:46 0:00 nginx: master process nginx root 5750 0.0 0.1 9764 6528 ? S 13:53 0:00 nginx: master process nginx nginx 5800 0.0 0.1 14384 5180 ? S 15:17 0:00 nginx: worker process nginx 5801 0.0 0.1 14384 5180 ? S 15:17 0:00 nginx: worker process root 5805 0.0 0.0 221680 2304 pts/0 S 15:18 0:00 grep --colorauto nginx [rootnode1 sbin]# curl -I 172.25.254.200 HTTP/1.1 403 Forbidden Server: nginx/1.20.1 Date: Sat, 17 Aug 2024 07:18:46 GMT Content-Type: text/html Content-Length: 153 Connection: keep-alive恢复原来的文件然后杀掉新的进程。则恢复到原来的版本 [rootnode1 sbin]# cp nginx nginx.new [rootnode1 sbin]# \cp -f nginx.old nginx [rootnode1 sbin]# kill -9 5750三、nginx常用参数 -v : show version and exit 显示版本信息 -V : show version and configure options then exit #显示版本和编译参数 [rootNginx ~]# nginx -v [rootnode1 sbin]# nginx -v nginx version: nginx/1.20.1 [rootnode1 sbin]#[rootnode1 sbin]# nginx -V nginx version: nginx/1.20.1 built by gcc 11.3.1 20221121 (Red Hat 11.3.1-4) (GCC) built with OpenSSL 3.0.7 1 Nov 2022 TLS SNI support enabled configure arguments: --prefix/usr/share/nginx --sbin-path/usr/sbin/nginx --modules-path/usr/lib64/nginx/modules --conf-path/etc/nginx/nginx.conf --error-log-path/var/log/nginx/error.log --http-log-path/var/log/nginx/access.log --http-client-body-temp-path/var/lib/nginx/tmp/client_body --http-proxy-temp-path/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path/var/lib/nginx/tmp/scgi --pid-path/run/nginx.pid --lock-path/run/lock/subsys/nginx --usernginx --groupnginx --with-compat --with-debug --with-file-aio --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_degradation_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_moduledynamic --with-http_mp4_module --with-http_perl_moduledynamic --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-http_xslt_moduledynamic --with-maildynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-streamdynamic --with-stream_ssl_module --with-stream_ssl_preread_module --with-threads --with-cc-opt-O2 -fltoauto -ffat-lto-objects -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werrorformat-security -Wp,-D_FORTIFY_SOURCE2 -Wp,-D_GLIBCXX_ASSERTIONS -specs/usr/lib/rpm/redhat/redhat-hardened-cc1 -fstack-protector-strong -specs/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -marchx86-64-v2 -mtunegeneric -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection --with-ld-opt-Wl,-z,relro -Wl,--as-needed -Wl,-z,now -specs/usr/lib/rpm/redhat/redhat-hardened-ld -specs/usr/lib/rpm/redhat/redhat-annobin-cc1 -Wl,-E-h : this help    帮助 [rootnode1 sbin]# nginx -help nginx version: nginx/1.20.1 Usage: nginx [-?hvVtTq] [-s signal] [-p prefix][-e filename] [-c filename] [-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-p prefix : set prefix path (default: /usr/share/nginx/)-e filename : set error log file (default: /var/log/nginx/error.log)-c filename : set configuration file (default: /etc/nginx/nginx.conf)-g directives : set global directives out of configuration file-t : test configuration and exit        #测试配置文件是否异 [rootnode1 sbin]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful-T : test configuration, dump it and exit    #测试并且会将你文件里边的内容全部打印出来 [rootnode1 sbin]# nginx -T nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful # configuration file /etc/nginx/nginx.conf: # For more information on configuration, see: # * Official English Documentation: http://nginx.org/en/docs/ # * Official Russian Documentation: http://nginx.org/ru/docs/user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid;略。。。。。。。。。。。 -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 #设置全局指令,注意和 配置文件不要同时配置,否则冲突 四、nginx服务的启动脚本 [rootnode1 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 #类型是forkin函数 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[rootnode1 sbin]# systemctl daemon-reload [rootnode1 sbin]# systemctl enable --now nginx Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service. [rootnode1 sbin]#五、nginx全局参数优化 [rootnode1 ~]# vim /usr/local/nginx/conf/nginx.confuser 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;events {worker_connections 100000; }[rootnode1 ~]# vim /etc/security/limits.conf 在最后一行写以下参数nginx - nofile 100000[rootnode1 ~]# sudo -u nginx ulimit -a [rootnode1 ~]# 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) 14258 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) 14258 virtual memory (kbytes, -v) unlimited file locks (-x) unlimited [rootnode1 ~]# nginx -s reload压力测试 [rootnode1 ~]# ab -n 10000 -c 5000 http://172.25.254.200/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.200 (be patient) socket: Too many open files (24)以上显示访问太多扛不住还在可控范围内 [rootnode1 ~]# ab -n 10000 -c 500 http://172.25.254.200/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.200 (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.20.1 Server Hostname: 172.25.254.200 Server Port: 80Document Path: /index.html Document Length: 153 bytesConcurrency Level: 500 Time taken for tests: 0.279 seconds Complete requests: 10000 Failed requests: 0 Non-2xx responses: 10000 Total transferred: 3030000 bytes HTML transferred: 1530000 bytes Requests per second: 35811.49 [#/sec] (mean) Time per request: 13.962 [ms] (mean) Time per request: 0.028 [ms] (mean, across all concurrent requests) Transfer rate: 10596.56 [Kbytes/sec] receivedConnection Times (ms)min mean[/-sd] median max Connect: 0 6 2.0 5 13 Processing: 2 8 3.5 7 26 Waiting: 0 6 3.3 5 23 Total: 6 14 3.7 13 29Percentage of the requests served within a certain time (ms)50% 1366% 1575% 1680% 1690% 1795% 1898% 2499% 28100% 29 (longest request)可在日志中进行查看 [rootnode1 ~]# cat /var/log/nginx/access.log - ApacheBench/2.3 - 172.25.254.200 - - [17/Aug/2024:17:20:11 0800] GET /index.html HTTP/1.0 404 153 - ApacheBench/2.3 - 172.25.254.200 - - [17/Aug/2024:17:20:11 0800] GET /index.html HTTP/1.0 404 153 - ApacheBench/2.3 - 172.25.254.200 - - [17/Aug/2024:17:20:11 0800] GET /index.html HTTP/1.0 404 153 - ApacheBench/2.3 - 172.25.254.200 - - [17/Aug/2024:17:20:11 0800] GET /index.html HTTP/1.0 404 153 - ApacheBench/2.3 - 六、lnginx配置中的root和alias [rootnode1 ~]# vim /usr/local/nginx/conf/nginx.conf events {worker_connections 100000;use epoll; }略。。。。。。。。。#gzip on;include /usr/local/nginx/conf.d/*.conf;server {listen 80;server_name localhost;[rootnode1 ~]# mkdir -p /usr/local/nginx/conf.d[rootnode1 ~]# vim /usr/local/nginx/conf.d/vhost.conf server {listen 80;server_name www.laokang.org;root /data/web/html;index index.html; } ~ [rootnode1 ~]# mkdir -p /data/web/html[rootnode1 ~]# echo www.laokang.org /data/web/html/index.html [rootnode1 ~]# nginx -s reload [rootnode1 ~]# curl www.laokang.org www.laokang.org[rootnode1 ~]# vim /usr/local/nginx/conf.d/vhost.confserver {listen 80;server_name www.laokang.org;root /data/web/html;index index.html;location /test1/ {root /data/web; }} ~[rootnode1 ~]# mkdir /data/web/test1 [rootnode1 ~]# echo /data/web/test1 /data/web/test1/index.html [rootnode1 ~]# systemctl restart nginx.service [rootnode1 ~]# curl www.laokang.org/test1/ /data/web/test1[rootnode1 ~]# vim /usr/local/nginx/conf.d/vhost.conf server {listen 80;server_name www.laokang.org;root /data/web/html;index index.html;location /test1/ {root /data/web; }location /test2 { #相当于给test1做了一个软连接alias /data/web/test1; } }[rootnode1 ~]# systemctl restart nginx.service [rootnode1 ~]# curl www.laokang.org/test2/ /data/web/test1七、location的用法以及优先级 1、定义 在一个server中location配置段可存在多个用于实现从uri到文件系统的路径映射 ngnix会根据用户请求的URI来检查定义的所有location按一定的优先级找出一个最佳匹配 而后应用其配置在没有使用正则表达式的时候nginx会先在server中的多个location选取匹配度最 高的一个uri uri是用户请求的字符串即域名后面的web文件路径 然后使用该location模块中的正则url和字符串如果匹配成功就结束搜索并使用此location处理 此请求。 2、语法规则 #语法规则 location [ | ~ | ~* | ^~ ] uri { ... }          #用于标准uri前需要请求字串与uri精确匹配大小敏感,如果匹配成功就停止向下匹配并立 即处理请求 ^~         #用于标准uri前表示包含正则表达式,并且匹配以指定的正则表达式开头 #对uri的最左边部分做匹配检查不区分字符大小写 ~         #用于标准uri前表示包含正则表达式,并且区分大小写 ~*         #用于标准uri前表示包含正则表达式,并且不区分大写 不带符号         #匹配起始于此uri的所有的uri \         #用于标准uri前表示包含正则表达式并且转义字符。可以将 . * ?等转义为普通符号 #匹配优先级从高到低 , ^~, ~/~*, 不带符号 3、优先级测试 [rootnode1 ~]# vim /usr/local/nginx/conf.d/vhost.confserver {listen 80;server_name www.laokang.org;root /data/web/html;index index.html;location /test {root /data/web2; }location /test {root /data/web1; }location ^~ /t {root /data/web3; }location ~* .HTML$ {root /data/web5; }location ~ .html$ {root /data/web4; } } ~ [rootnode1 ~]# systemctl restart nginx.service [rootnode1 ~]# curl www.laokang.org/test/index.html test web5[rootnode1 ~]# vim /usr/local/nginx/conf.d/vhost.conf server {listen 80;server_name www.laokang.org;root /data/web/html;index index.html;location /test {root /data/web2; }location /test {root /data/web1; }location ^~ /t {root /data/web3; } # location ~* .HTML$ { # root /data/web5; #}location ~ .html$ {root /data/web4; } } [rootnode1 ~]# systemctl restart nginx.service [rootnode1 ~]# curl www.laokang.org/test/index.html test web4 [rootnode1 ~]#[rootnode1 ~]# vim /usr/local/nginx/conf.d/vhost.conf server {listen 80;server_name www.laokang.org;root /data/web/html;index index.html;location /test {root /data/web2; }location /test {root /data/web1; }location ^~ /t {root /data/web3; } # location ~* .HTML$ { # root /data/web5; #} # location ~ .html$ { # root /data/web4; #} } [rootnode1 ~]# systemctl restart nginx.service [rootnode1 ~]# curl www.laokang.org/test/index.html test web1[rootnode1 ~]# vim /usr/local/nginx/conf.d/vhost.confserver {listen 80;server_name www.laokang.org;root /data/web/html;index index.html;location /test {root /data/web2; } # location /test { # root /data/web1; #}location ^~ /t {root /data/web3; } # location ~* .HTML$ { # root /data/web5; #} # location ~ .html$ { # root /data/web4; #} } [rootnode1 ~]# systemctl restart nginx.service[2] 已停止 vim /usr/local/nginx/conf.d/vhost.conf [rootnode1 ~]# curl www.laokang.org/test/index.html test web3 八、nginx的用户认证 [rootnode1 ~]# dnf install httpd-tools [rootnode1 ~]# htpasswd -cm /usr/local/nginx/.htpasswd admin New password: Re-type new password: Adding password for user admin [rootnode1 ~]# cat /usr/local/nginx/.htpasswd admin:$apr1$IDZBut6K$uri3VMd37GFwsSZSJfKEJ/ [rootnode1 ~]# htpasswd -m /usr/local/nginx/.htpasswd laokang New password: Re-type new password: Adding password for user laokang [rootnode1 ~]# mkdir /data/web/kang [rootnode1 ~]# echo kang /data/web/kang/index.html [rootnode1 ~]# vim /usr/local/nginx/conf.d/vhost.conf server {listen 80;server_name www.laokang.org;root /data/web/html;index index.html;location /kang {root /data/web;auth_basic login password !!;auth_basic_user_file /usr/local/nginx/.htpasswd; }[rootnode1 ~]# systemctl restart nginx.service [rootnode1 ~]#九、自定义 一自定义错误页面 [rootnode1 ~]# vim /usr/local/nginx/conf.d/vhost.conf server {listen 80;server_name www.laokang.org;root /data/web/html;index index.html;error_page 404 /40x.html;location /kang {root /data/web;auth_basic login password !!;auth_basic_user_file /usr/local/nginx/.htpasswd; }location /40x.html {root /data/web/errorpage; }[rootnode1 ~]# mkdir -p /data/web/errorpage [rootnode1 ~]# echo error page /data/web/errorpage/40x.html [rootnode1 ~]# systemctl restart nginx.service二自定义日志 日志会默认存放在 /var/local/nginx/logs下 有access.log和error.log两个文件 [rootnode1 ~]# ll /usr/local/nginx/logs/ 总用量 2172 -rw-r--r-- 1 root root 2211453  8月 17 21:24 access.log -rw-r--r-- 1 root root    4919  8月 17 21:24 error.log -rw-r--r-- 1 root root       5  8月 17 21:24 nginx.pid   [rootnode1 ~]# vim /usr/local/nginx/conf.d/vhost.conf server {listen 80;server_name www.laokang.org;root /data/web/html;index index.html;error_page 404 /40x.html;error_log /var/log/laokang.org/error.log;access_log /var/log/laokang.org/access.org;[rootnode1 ~]# mkdir -p /var/log/laokang.org [rootnode1 ~]# systemctl restart nginx.service [rootnode1 ~]# curl www.laokang.org www.laokang.org [rootnode1 ~]# cat /var/log/laokang.org/access.org 172.25.254.200 - - [17/Aug/2024:21:35:11 0800] GET / HTTP/1.1 200 16 - curl/7.76.1 [rootnode1 ~]# curl www.laokang.org/aaa error page [rootnode1 ~]# cat /var/log/laokang.org/error.log 2024/08/17 21:35:55 [error] 4192#0: *2 open() /data/web/html/aaa failed (2: No such file or directory), client: 172.25.254.200, server: www.laokang.org, request: GET /aaa HTTP/1.1, host: www.laokang.org [rootnode1 ~]#十、检测文件是否存在 [rootnode1 ~]# vim /usr/local/nginx/conf.d/vhost.conf server {listen 80;server_name www.laokang.org;root /data/web/html;index index.html;error_page 404 /40x.html;error_log /var/log/laokang.org/error.log;access_log /var/log/laokang.org/access.org;try_files $uri $uri.html $uri/index.html /error/default.html;[rootnode1 ~]# systemctl restart nginx.service [rootnode1 ~]# curl www.laokang.org www.laokang.org [rootnode1 ~]# rm -fr /data/web/html/index.html经过检测之后如果没有$uri $uri.html $uri/index.html /error/default.html;这些文件就会报500的错误 [rootnode1 ~]# curl www.laokang.org html headtitle500 Internal Server Error/title/head body centerh1500 Internal Server Error/h1/center hrcenternginx/1.26.2/center /body /html /html[rootnode1 ~]# mkdir /data/web/html/error [rootnode1 ~]# echo error default /data/web/html/error/default.html [rootnode1 ~]# curl www.laokang.org error default 十一、nginx中长连接的管理 测试工具 [rootnode1 ~]# dnf install telnet -y 第一次测试 [rootnode1 ~]# telnet www.laokang.org 80 Trying 172.25.254.200... Connected to www.laokang.org. Escape character is ^]. GET / HTTP/1.1 HOST:www.laokang.orgHTTP/1.1 200 OK Server: nginx/1.26.2 Date: Sat, 17 Aug 2024 14:01:38 GMT Content-Type: text/html Content-Length: 16 Last-Modified: Sat, 17 Aug 2024 14:01:21 GMT Connection: keep-alive ETag: 66c0ad31-10 Accept-Ranges: byteswww.laokang.org GET / HTTP/1.1 HOST:www.laokang.orgHTTP/1.1 200 OK Server: nginx/1.26.2 Date: Sat, 17 Aug 2024 14:02:04 GMT Content-Type: text/html Content-Length: 16 Last-Modified: Sat, 17 Aug 2024 14:01:21 GMT Connection: keep-alive ETag: 66c0ad31-10 Accept-Ranges: byteswww.laokang.org ^ZConnection closed by foreign host. 第一次测试我们没有设置发起长连接的请求次数所以这里是手动停止的[rootnode1 ~]# vim /usr/local/nginx/conf/nginx.conf#tcp_nopush on;#keepalive_timeout 0;keepalive_timeout 65; 设置等待请求的时间为65skeepalive_requests 2; 设置发起长连接的次数位2次 [rootnode1 ~]# systemctl restart nginx.service第二次测试 [rootnode1 ~]# telnet www.laokang.org 80 Trying 172.25.254.200... Connected to www.laokang.org. Escape character is ^]. GET / HTTP/1.1 HOST:www.laokang.orgHTTP/1.1 200 OK Server: nginx/1.26.2 Date: Sat, 17 Aug 2024 14:02:45 GMT Content-Type: text/html Content-Length: 16 Last-Modified: Sat, 17 Aug 2024 14:01:21 GMT Connection: keep-alive ETag: 66c0ad31-10 Accept-Ranges: byteswww.laokang.org GET / HTTP/1.1 HOST:www.laokang.orgHTTP/1.1 200 OK Server: nginx/1.26.2 Date: Sat, 17 Aug 2024 14:03:00 GMT Content-Type: text/html Content-Length: 16 Last-Modified: Sat, 17 Aug 2024 14:01:21 GMT Connection: close ETag: 66c0ad31-10 Accept-Ranges: byteswww.laokang.org Connection closed by foreign host. 测试完两次之后他是自动结束的 十二、nginx下载服务器的设定优化 [rootnode1 ~]# mkdir /data/web/download做一个大小位100兆的文件把他放在/data/web/download/leefile他的数据从/dev/zero里拷出来 [rootnode1 ~]# dd if/dev/zero of/data/web/download/leefile bs1M count100 记录了1000 的读入 记录了1000 的写出 104857600字节105 MB100 MiB已复制0.0468224 s2.2 GB/s [rootnode1 ~]# vim /usr/local/nginx/conf.d/vhost.conflocation /download {root /data/web;autoindex on;autoindex_localtime on;autoindex_exact_size off;limit_rate 1024k;}[rootnode1 ~]# systemctl restart nginx.service [rootnode1 ~]# wget www.laokang.org/leefile --2024-08-17 22:29:35-- http://www.laokang.org/leefile 正在解析主机 www.laokang.org (www.laokang.org)... 172.25.254.200 正在连接 www.laokang.org (www.laokang.org)|172.25.254.200|:80... 已连接。 已发出 HTTP 请求正在等待回应... 200 OK 长度14 [text/html] 正在保存至: “leefile”leefile 100%[] 14 --.-KB/s 用时 0s2024-08-17 22:29:35 (2.31 MB/s) - 已保存 “leefile” [14/14]) 十三、nginx状态页 [rootnode1 ~]# cd /usr/local/nginx/conf.d/ [rootnode1 conf.d]# vim status.conf server {listen 80;server_name status.laokang.org;root /data/web/html;index index.html;location /status {stub_status;#auth_basic login;#auth_basic_user_file /usr/local/nginx/.htpasswd;} }[rootnode1 conf.d]# systemctl restart nginx.service [rootnode1 conf.d]# vim /etc/hosts 172.25.254.200 node1.laokang.org www.laokang.org status.laokang.org[rootnode1 conf.d]# systemctl restart nginx.service十四、nginx数据压缩功能 [rootnode1 conf.d]# vim /usr/local/nginx/conf/nginx.confgzip 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;[rootnode1 conf.d]# systemctl restart nginx.service [rootnode1 conf.d]# echo hello goudan /data/web/html/small.html [rootnode1 conf.d]# du -sh /usr/local/nginx/logs/access.log 2.2M /usr/local/nginx/logs/access.log [rootnode1 conf.d]# cat /usr/local/nginx/logs/access.log /data/web/html/big.html测试查看效果 [rootnode1 conf.d]# curl --head --compressed 172.25.254.200/small.html HTTP/1.1 200 OK Server: nginx/1.26.2 Date: Sat, 17 Aug 2024 14:56:34 GMT Content-Type: text/html Content-Length: 13 Last-Modified: Sat, 17 Aug 2024 14:55:00 GMT Connection: keep-alive ETag: 66c0b9c4-d Accept-Ranges: bytes[rootnode1 conf.d]# curl --head --compressed 172.25.254.200/big.html HTTP/1.1 200 OK Server: nginx/1.26.2 Date: Sat, 17 Aug 2024 14:56:48 GMT Content-Type: text/html Last-Modified: Sat, 17 Aug 2024 14:55:50 GMT Connection: keep-alive Vary: Accept-Encoding ETag: W/66c0b9f6-21d3e4 Content-Encoding: gzip 十五、nginx内键变 [rootnginx conf.d]# vim /usr/local/nginx/conf.d/var.confserver {listen 80;server_name var.laokang.org;root /data/web/html;index index.html;location /var {default_type text/html;set $laokang lee;echo $laokang; # 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; 十六、nginx的rewrite模块功能 [rootnginx test2]# vim /usr/local/nginx/conf.d/var.conf server {listen 80;server_name var.laokang.org;root /data/web/html;index index.html;location /var {default_type text/html;set $timinglee lee;echo $timinglee;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 test2]# curl -b key1lee,key2lee1 -u lee:lee var.laokang.org/var?nameleeid6666 lee 172.25.254.100 namelee ? /data/web/html /var var.laokang.org 52958 lee GET /data/web/html/var /var?namelee http HTTP/1.1 172.25.254.100 var.laokang.org 80 curl/7.76.1 key1lee,key2lee1 lee1#if判定location /test2 {if ( !-e $request_filename ){echo $request_filename is not exist;}}[rootnginx test2]# curl var.laokang.org/test2/index.html test2#breaklocation /break {default_type text/html;set $name lee;echo $name;if ( $http_user_agent curl/7.76.1 ){break;}set $id 666;echo $id;}[rootnginx test2]# curl var.laokang.org/break lee[rootnginx test2]# curl -A firefox var.laokang.org/break lee 666#returnlocation /return {default_type text/html;if ( !-e $request_filename){return 301 http://www.baidu.com;}echo $request_filename is exist;}#检测 [rootnginx test2]# curl -I var.laokang.org/return HTTP/1.1 200 OK Server: nginx/1.26.1 Date: Sun, 18 Aug 2024 11:54:07 GMT Content-Type: text/html Connection: keep-alive Vary: Accept-Encodingrewirte 临时和永久location / {root /data/web/var;index index.html;rewrite / http://www.timinglee.com permanent;#rewrite / http://www.timinglee.com redirect;}#测试 [rootnginx test2]# curl -I var.laokang.org HTTP/1.1 301 Moved Permanently Server: nginx/1.26.1 Date: Sun, 18 Aug 2024 11:55:29 GMT Content-Type: text/html Content-Length: 169 Connection: keep-alive Location: http://www.timinglee.com #break 和last[rootnginx-node1 conf.d]# mkdir /data/web/html/{test1,test2,break,last} -p [rootnginx-node1 conf.d]# echo test1 /data/web/html/test1/index.html [rootnginx-node1 conf.d]# echo test2 /data/web/html/test2/index.html [rootnginx-node1 conf.d]# echo last /data/web/html/last/index.html [rootnginx-node1 conf.d]# echo break /data/web/html/break/index.html [rootnginx test2]# cat /usr/local/nginx/conf.d/vars.conf server {listen 80;server_name var.laokang.org;root /data/web/html;index index.html;location /break {root /data/web/html;rewrite ^/break/(.*) /test1/$1 break;rewrite ^/test1/(.*) /test2/$1 ;}location /last {root /data/web/html;rewrite ^/last/(.*) /test1/$1 last;rewrite ^/test1/(.*) /test2/$1 ;}location /test1 {default_type test/html;return 666 timinglee hahahahahahahahahahahahahah;}location /test2 {root /data/web/httml;} }十七、nginx-rewrite的企业示例及防盗链 访问强制加密 [rootnginx conf.d]# cd /usr/local/nginx/ [rootnginx nginx]# mkdir certs [rootnginx nginx]# openssl req -newkey rsa:2048 -nodes -sha256 -keyout /usr/local/nginx/certs/timinglee.org.key -x509 -days 365 -out /usr/local/nginx/certs/timinglee.org.crt[rootnginx conf.d]# cat ssl.conf server {listen 80;listen 443 ssl;server_name www.laokang.org;root /data/web/html;index index.html;ssl_certificate /usr/local/nginx/certs/timinglee.org.crt;ssl_certificate_key /usr/local/nginx/certs/timinglee.org.key;ssl_session_cache shared:SSL:1m;ssl_session_timeout 5m;location / {if ( $scheme http ){rewrite / https://$host redirect;}}防盗链 先将照片放入默认发布目录 [rootnginx html]# vim /usr/local/nginx/conf.d/ssl.confserver {listen 80;listen 443 ssl;server_name www.laokang.org;root /data/web/html;index index.html;ssl_certificate /usr/local/nginx/certs/timinglee.org.crt;ssl_certificate_key /usr/local/nginx/certs/timinglee.org.key;ssl_session_cache shared:SSL:1m;ssl_session_timeout 5m;# location / {# if ( $scheme http ){# rewrite /(.*) https://$host redirect;# }# }# if ( !-e $request_filename ){# rewrite /(.*) https://$host/index.html redirect;# }location / {valid_referers none blocked server_names *.laokang.org ~/.baidu/.;if ( $invalid_referer ){rewrite ^/ http://www.laokang.org/tao.jpg;}}}另一台主机[rootnode1 ~]# dnf install httpd [rootnode1 ~]# cd /var/www/html/ [rootnode1 html]# vim index.htmlhtmlheadmeta http-equivContent-Type contenttext/html;charsetutf-8title盗链/title /headbodyimg srchttp://www.laokang.org/images/taoshi1.jpg h1 stylecolor:red欢迎大家/h1pa hrefhttp://www.laokang.org狂点陶师傅/a向你敬礼/p/body/html[rootnode1 html]# systemctl restart httpd.service 十八、nginx的反向代理及动静分离 反向代理 node1 [rootnode1 ~]# systemctl enable --now httpd Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service. [rootnode1 ~]# echo 172.25.254.10 /var/www/html/index.html 服务器 代理只能写一个 [rootnginx html]# vim /usr/local/nginx/conf.d/http.conf [rootnginx html]# cat /usr/local/nginx/conf.d/http.conf server {listen 80;server_name www.laokang.org;location / {proxy_pass http://172.25.254.10:80; } [rootnginx html]# curl www.laokang.org 172.25.254.10动静分明 [rootnginx html]# vim /usr/local/nginx/conf.d/http.conf [rootnginx html]# cat /usr/local/nginx/conf.d/http.conf server {listen 80;server_name www.laokang.org;location ~ \.php$ {proxy_pass http://172.25.254.10:80; }location /static {proxy_pass http://172.25.254.20:8080; }}[rootnginx html]# systemctl restart nginx.servicenode1 [rootnode1 ~]# yum install php -y [rootnode1 ~]# systemctl restart httpd.service [rootnode1 ~]# vim /var/www/html/index.php [rootnode1 ~]# cat /var/www/html/index.php ?phpphpinfo(); ? 动静分里测试 二、fastcginginx和php共存 一、源码编译php 首先我们将所需要的模块添加进去 解压模块 tar zxf memc-nginx-module-0.20.tar.gz tar zxf srcache-nginx-module-0.33.tar.gz重新编译nginx ./configure --prefix/usr/local/nginx --add-module/root/echo-nginx-module-0.63 --add-module/root/memc-nginx-module-0.20 --add-module/root/srcache-nginx-module-0.33 --usernginx --groupnginx --with-http_v2_module --with-http_realip_module --with-http_stub_status_module ./configure --prefix/usr/local/nginx --add-module/root/echo-nginx-module-0.63 --add-module/root/memc-nginx-module-0.20 --add-module/root/srcache-nginx-module-0.33 --usernginx --groupnginx --with-http_v2_module --with-http_realip_module --with-http_stub_status_module make make install 下载php并进行编译 解压下载 tar zxf php-8.3.9.tar.gzcd php-8.3.9/ ./configure --prefix/usr/local/php --with-config-file-path/usr/local/php/etc --enable-fpm --with-fpm-usernginx --with-fpm-groupnginx --with-curl --with-iconv --with-mhash --with-zlib --with-openssl --enable-mysqlnd --with-mysqli --with-pdo-mysql --disable-debug --enable-sockets --enable-soap --enable-xml --enable-ftp --enable-gd --enable-exif --enable-mbstring --enable-bcmath --with-fpm-systemd 编译的话因为缺少一些软件所以编译错误提示你需要下载一些软件完成之后才能编译成功 以下是我编译时需要的软件 yum install libpng-devel.x86_64yum install libcurl-devel.x86_64yum install oniguruma-devel 还有一个是软件仓库没有所以需要在官网去下载下载之后进行下载rpm -ivh oniguruma-devel-6.9.6-1.el9.5.x86_64 make make install 二、php的配置 cd /usr/local/php/etc/cp php-fpm.conf.default php-fpm.convim php-fpm.conf 。。。。。。。。略 [global] ; Pid file ; Note: the default prefix is /usr/local/php/var ; Default Value: none pid run/php-fpm.pid 。。。。。。。。略cd php-fpm.d/cp www.conf.default www.confcd /root/php-8.3.9/cp php.ini-production /usr/local/php/etc/php.ini 更改时区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 生成启动文件 cp sapi/fpm/php-fpm.service /lib/systemd/system/ ls /lib/systemd/system vim /lib/systemd/system/php-fpm.service 注释以下内容 # Mounts the /usr, /boot, and /etc directories read-only for processes invoked by this unit. #ProtectSystemfull systemctl start php-fpm.service netstat -antlupe | grep php tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 0 39197 1940/php-fpm: maste 三、nginx和php的整合 [rootnginx ~]#cd /usr/local/php/bin/[rootnginx ~]#vim ~/.bash_profile [rootnginx ~]# cat ~/.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 ~]#source ~/.bash_profile[rootnginx ~]#mkdir /data/php -p[rootnginx ~]#cat /data/php/index.php[rootnginx ~]# vim /data/php/index.php [rootnginx php]# cat /data/web/php/index.php ?phpphpinfo(); ? [rootnginx php]# vim /usr/local/nginx/conf/nginx.conf#gzip on;include /usr/local/nginx/conf.d/*.conf;server {listen 80;server_name localhost;#charset koi8-r;#access_log logs/host.access.log main; [rootnginx php]# vim /usr/local/nginx/conf.d/vhosts.conf[3] 已停止 vim /usr/local/nginx/conf.d/vhosts.conf [rootnginx php]# cat /usr/local/nginx/conf.d/vhosts.confserver {listen 80;server_name php.laokang.org;root /date/web/html;index index.html;location ~ \.php$ {root /data/web/php;fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;include fastcgi.conf;} } [rootnginx php]# nginx -s reload 四、nginx-php缓存 安装memcache模块 [rootNginx ~]# tar zxf memcache-8.2.tgz [rootNginx ~]# cd memcache-8.2/ [rootNginx memcache-8.2]# yum install autoconf [rootNginx memcache-8.2]# phpize [rootNginx memcache-8.2]# ./configure make make install Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-nonzts-20230831/ [rootNginx memcache-8.2]# ls /usr/local/php/lib/php/extensions/no-debug-non-zts20230831/ memcache.so opcache.so复制测试文件到nginx发布目录中 [rootNginx ~]# cd memcache-8.2/ [rootNginx memcache-8.2]# ls autom4te.cache config.log configure.ac example.php Makefile.fragments README build config.m4 config.w32 include Makefile.objects runtests.php config9.m4 config.nice CREDITS libtool memcache.la src config.h config.status docker LICENSE memcache.php tests config.h.in configure Dockerfile Makefile modules [rootNginx memcache-8.2]# cp example.php memcache.php /data/web/php [rootNginx ~]# vim /data/web/php/memcache.phpdefine(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 ~]# vim /usr/local/php/etc/php.ini ;extensionzip extensionmemcache ;zend_extensionopcache [rootNginx ~]# systemctl reload php-fpm [rootnginx php]# php -m | grep mem memcache部署memcached [rootNginx ~]# yum install memcached -y [rootNginx ~]# systemctl enable --now memcached.service [rootNginx ~]# netstat -antlupe | grep memcache tcp 0 0 127.0.0.1:11211 0.0.0.0:* LISTEN 976 1037243 186762/memcached [rootNginx ~]# cat /etc/sysconfig/memcached PORT11211 USERmemcached MAXCONN1024 CACHESIZE64 OPTIONS-l 127.0.0.1,::1 测试 访问 http://php.timinglee.org/example.php 不断刷新 访问 http://php.timinglee.org/memcache.php 查看命中效果 五、php高速缓存 在原来的配置基础上进行操作 [rootnginx php]# vim /usr/local/nginx/conf.d/vhosts.conf \ [5] 已停止 vim /usr/local/nginx/conf.d/vhosts.conf [rootnginx php]# cat /usr/local/nginx/conf.d/vhosts.conf upstream memcache {server 127.0.0.1:11211;keepalive 512; }server {listen 80;server_name php.laokang.org;root /date/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 127.0.0.1:9000;fastcgi_index index.php;include fastcgi.conf;} } [rootnginx php]# ab -n500 -c10 http://php.laokang.org/index.phpConcurrency Level: 10 Time taken for tests: 0.047 seconds Complete requests: 500 Failed requests: 0 Non-2xx responses: 500 Total transferred: 154500 bytes HTML transferred: 78500 bytes Requests per second: 10597.49 [#/sec] (mean) Time per request: 0.944 [ms] (mean) Time per request: 0.094 [ms] (mean, across all concurrent requests) Transfer rate: 3197.87 [Kbytes/sec] received
http://www.hkea.cn/news/14318839/

相关文章:

  • 亿唐微方网站建设网站左悬浮代码
  • 珠海电商网站制作论文格式样板模板
  • 专业的公司网站设计服务抖音关键词搜索排名收费
  • 网站开发文档模板 开源网页设计与网站开发经济可行性
  • 怎么选择一个好的友情链接网站万网是做什么的
  • 搭建源码下载站网站数字今天科技 网站
  • 做网站及APPwordpress编辑器移植
  • 网站投票活动怎么做seo怎么优化网站
  • 网站建设 上海网站建山西格泰网站建设
  • 云南微网站制作哪家好wordpress多个函数文件路径
  • 广州做护肤品的网站免备案cdn
  • 网站建设华科技台州网站公司建站
  • 营销型网站用什么模版合适标智客logo在线设计生成器免费
  • 未备案的网站百度400电话
  • 怎么做地区网站网页设计作业成品代码免费
  • 做偏门网站网店美工主要负责
  • 做网站编辑需要会什么网站顾客评价
  • 网站建设相关网站php网站方案
  • 外贸五金网站建设wordpress短代码插件TD
  • 二级网站免费建网站推广营销策划
  • 关闭 百度云加速 后网站打不开了wordpress 头部
  • 网站内链技巧做网站好
  • 图书馆网站建设教程织梦手机网站怎么做
  • 常见网站建设网站备案后更换主机
  • 免费网站建设软件有哪些营销图片素材
  • 那个网站做足球测网络营销的网站
  • 阿里云租的域名怎么做网站wordpress 安卓适配
  • 程序员怎么做网站赚钱好用的wordpress代码编辑器
  • 做一网站需要多少钱推广seo网站
  • 网站建设360做响应式网站兼容哪几个尺寸