web服务nginx

目录

nginx的介绍

运行原理

 特性

功能

应用场景

架构图

nginx的安装

1,实验环境

2,编译安装

在网上下载好压缩包,通过xftp把下载好的压缩包传输给linux

解压

安装需要编译的软件 gcc,pcre-devel,zlib-devel,openssl-devel

切换到nginx的目录下,并创建一个用户

选择参数安装,并安装到/usr/local/nginx下

​编辑

make

启动nginx

查看

因为启动nginx的时候要切换到/usr/local/nginx/sbin下,所以可以编译参数,使其可以在直接在任何路径下直接nginx就可以启动

此时就可以在家目录下直接nginx启动

可以用-v查看版本,-V查看具体的信息

nginx的平滑升级和回滚

1,通过xftp把更高级的版本传输到Linux上,还有其相关的模块

​编辑

2,解压

选择参数,在之前的基础上加多了一个

make,直接make就可以了,这次不需要再make install

把之前版本nginx给复制,以防新版本出问题,可以回滚

复制新版本的nginx过去

可以用-t来查看文件有没有问题

查看nginx的进程

使用kill -USR2

此时就会多出一个nginx的进程,但这个进程并没有被使用,还是1.24这个版本生效

回收旧版本

此时旧版本的nginx就会被停掉,新版本会生效

可以使用curl -I来查看

回滚

先在把nginx复制出来,再把1.24版本的复制回去,

在使用kill命令,

此时就h会变回原来的1.24版本

nginx的启动文件

nginx的命令参数

效果

核心配置

1,全局配置

在/usr/local/nginx/conf/nginx.conf

http配置块

1,location的用法

在nginx的配置文件里

创建路径

在该路径下编写参数

创建路径,并写入内容

重启nginx程序

测试

如果没有本地解析可以访问IP或者在/etc/hosts做解析

root和alias

root

1.在配置文件里

创建路径,并写入内容,重启nginx

此时可以在window里的浏览器做测试,也可以在本虚拟机里测试

1.在window里要做解析

测试

alias 别名

1.在配置文件里添加参数,并重启nginx

在浏览器测试

location的语法

配置的优先级

1,在目录匹配( = 不能目录匹配)

2,在文件匹配

实验

1,在配置文件里添加参数,

2,创建目录和写入文件内容,重启nginx

可以在浏览器测试

nginx的用户认证

1,创建用户和密码

编辑文件,并重启

创建文件路径,并写入内容

在浏览器测试

错误页面和日志自定义

1,错误页面

编辑配置文件,并重启服务

创建文件路径,并写入内容

在浏览器测试

此时在访问一个没有路径的时候就会自动跳转到自己定义的页面

2,日志自定义

1,创建文件路径

2,编辑配置文件,并重启

测试:可以在浏览器随便访问,使其产生日志

查看

文件检测和长链接

1,检侧文件

创建访问页面

编辑server,并重启

在浏览器测试

此时如果访问的文件不存在就会跳转到自定义的路径,提示文件不存在

2,长链接

1,在nginx的主配置文件里添加keepalive_requests参数,启动长链接功能

测试前如果虚拟机里没有telnet,可以 使用yum先安装

测试

nginx作为下载服物器

1,编辑配置文件location,并重启

创建文件路径

在浏览器测试

nginx的状态页

1,编辑配置文件,并重启服务

在浏览器测试

这个一般都是和用户认证一起做的,

则配置文件的内容为   配置完成后要重启

测试,此时就会需要账户和密码才能登录并访问到

nginx的压缩功能

1,编辑nginx的主配置文件

重启服务

创建文件进行测试,分别在文件里做一个小于1k的和大于1k的

可以用du -sh 来查看文件的大小

测试

此时两个文件一个小于1k的不被压缩,而大于1k的被压缩

nginx的变量使用

配置文件

测试

配置文件

测试

配置文件

测试

配置文件

测试

自定义变量

在配置文件里,添加配置,并重启nginx

测试

也可以结合已有的变量来进行自己定义变量

配置文件

测试

if指令

配置文件,使在服务不存在的文件时会直接 返回定义的内容,如果存在会直接显示该文件里的内容

测试

break指令

配置文件

测试,此时是没有加breakz指令的

加上break指令后

测试,此时break后的就会被打断

可以结合if指令来做判断

配置

return指令

配置

测试,此时服务没有文件就会被定向到百度去

正则表达式

rewrite指令

永久重定向

配置

测试

临时重定向

break和last

配置场景  创建文件和内容,

配置,并重启

而当在内部加了break后,会直接打断location的执行,即使后面的location同也会被打断

配置

测试

而加上last只会打断在这个location语句块last后面的执行

测试

自动跳转https

1,创建目录,制作证书

编辑配置server和location

在浏览器测试,输入http的格式他会自动跳到https

测试之前要在window里做解析

在C:\Windows\System32\drivers\etc添加一个为aaa.timinglee.org的域名

nginx防盗链

新建一台虚拟机

安装httpd

在默认访问路径下添加页面

在nginx的主机里,添加路径和页面

使用xftp来给虚拟机添加图片

添加配置,并重启

此时在浏览器测试

然后点击之后就会跳转到www.timinglee.org这台主机的页面

如果配置了防盗链

配置

此时,就会阻止别人盗链

点击后也会出现404

反向代理

1,多添加一台虚拟机

安装httpd,启动服务

l两台主机添加内容

2,配置

在110主机上

测试

反向代理的缓存功能

1,在主配置文件里添加参数

编辑配置文件

进行压测测试

反向代理的负载均衡

修改配置文件,并重启服务

测试

可以修改算法(默认是轮询)添加

如下示例

nginx实现tcp负载均衡

1,在后端主机上安装bing,两台都装

编辑主配置文件,两台都改

​编辑创建文件,两台都要改,并启动

查看

2,在nginx主机上

在主配置文件添加一个在http块外的子配置文件

创建文件

编辑配置文件,并重启

可以检查端口是否开启

此时就可以实现tcp的负载均衡

nginx的数据库的负载均衡

1,在后端安装mysql,两台都装

编辑配置文件。让后面测试时可以直观的看出来

启动服务

配置远程访问授权,两台都做

在nginx主机上,编辑配置文件

查看端口i

测试

先安装mysql的客户端来使主机能使用MySQL命令

实现 FastCGI

特点

程重新编译nginx

删除之前的nginx,(如果需要之前的配置可以使用平滑升级或回滚来保留)

重新编译nginx

通过xftp来给虚拟机传输压缩包

解压

编译

启动nginx服务

安装php

通过xftp来给虚拟机传输PHP的相关压缩包

安装php前先把相关的捆绑包先安装

此时编译PHP所需要的环境的捆绑包已经安装好了

编译PHP

解压

配置优化

去掉注释

修改时区

注释掉该内容

刷新,并启动php服务

可以查看开启没有

可以添加php的环境变量

php配置方法

创建文件,并编写

在nginx主配置文件里

​编辑创建文件目录

编辑配置文件,并重启

在浏览器测试

php的缓存

安装memcache模块

解压后安装

加载memcache模块

部署memcache

启动

查看是否加载了memcache模块

复制测试文件到nginx的发布目

修改内容

模拟取出数据,不断刷新

此时需要账户和密码

进行压测

PHP高速缓存

编辑配置文件,并重启

压测

nginx二次版本开发

特点

要先停掉原有的nginx,在进行安装

编译安装 openresty

先安装需要的捆绑包,并创建用户

通过xftp传输给虚拟机openresty压缩包

解压

编译

配置环境变量

启动服务

在浏览器访问测试


nginx的介绍

Nginx(发音为“Engine X”)是一款轻量级和高性能的 Web 服务器、反向代理服务器、电子邮件(IMAP/POP3/SMTP)代理服务器,是带有 BSD-like 协议的开源产品。

另外,关于 BSD-like 协议,它为开发者和企业提供了极大的灵活性,在确保尊重原作者工作的同时,允许代码的自由流通和使用,无论是在开源社区还是商业应用中。

其特点包括:

轻量级:Nginx 以其轻量级和高效性而闻名,对系统资源的占用非常小;
高并发能力:Nginx 能够处理大量的并发连接,这使得它在面对高流量网站时表现出色,在同类 Web 服务器中是领先的;
反向代理功能:作为反向代理服务器,Nginx 可以帮助提高网站的响应速度和可用性,同时隐藏后端服务器的复杂性,这使得 Nginx 在负载均衡和缓存方面非常有用;
社区支持:作为一个开源项目,Nginx 拥有一个活跃的开发者和用户社区,这意味着它经常得到更新和改进,以适应不断变化的网络环境;
安全性:Nginx 提供了多种安全措施,包括 SSL/TLS 加密支持,以及限制请求速率等,帮助网站防御各种网络攻击;
广泛应用:全球范围内,许多知名的大型网站和服务都在使用 Nginx,这证明了它的可靠性和性能。
Nginx 是基于 C 语言开发的,拥有高性能的基本保证。

运行原理

Nginx的运行原理主要基于其独特的事件驱动架构和异步非阻塞的I/O模型。Nginx采用了一个master-worker的进程模型,其中master进程负责管理和监控worker进程,而worker进程则负责处理实际的网络请求。这种架构使得Nginx能够充分利用多核CPU的优势,提高并发处理能力。

事件驱动架构:Nginx使用异步非阻塞的I/O模型来处理网络请求和I/O操作。在这种模型中,Nginx不会为每个连接创建一个新的线程或进程,而是使用事件循环来监听和处理各种事件,如新的连接请求、数据读取、数据写入等。这种处理方式使得Nginx能够同时处理大量的并发连接,而不会因为某个连接的阻塞而影响其他连接的处理。
master-worker进程模型:master进程主要负责读取配置文件、初始化系统环境、启动并监控worker进程等。而worker进程则负责处理具体的网络请求,包括接收请求、处理请求、发送响应等。当worker进程处理不过来时,可以通过增加worker进程的数量来提高并发处理能力

 特性


高性能:Nginx采用异步非阻塞的事件驱动模型,能够处理数以万计的并发连接,同时保持低延迟和高吞吐量。
稳定性:Nginx经过长时间的发展和完善,已经变得非常稳定,能够长时间运行而不需要重启。
事件驱动: Nginx使用事件驱动模型,能够处理大量并发连接。

模块化设计: Nginx提供了很多模块,如ngx_http_core_module、ngx_http_ssl_module、ngx_http_gzip_static_module等,可以通过加载不同的模块来增加额外的功能。

配置灵活: Nginx的配置可以通过nginx.conf文件进行管理,可以使用#进行注释,使用include指令包含其他配置文件


低资源消耗:Nginx在运行时占用的内存和CPU资源相对较少,适合在资源受限的环境中运行

功能

静态内容服务:
Nginx可以作为一个高性能的静态文件服务器,提供静态网页、图片、视频和其他静态内容的快速传输。
反向代理:
Nginx能够作为反向代理服务器,将客户端的请求转发给后端的应用服务器,隐藏真实的后端服务器,提升安全性和性能。
负载均衡:
Nginx支持多种负载均衡算法,如轮询、最小连接数、IP哈希等,可以实现请求的均衡分发,提高后端服务器的处理能力。
可以在Nginx中定义后端服务器组,并根据需要配置权重、持久化等参数。
动态内容服务:
Nginx可以与各种后端应用服务器集成,如PHP、Node.js、Python等,实现动态内容的处理和传递。
SSL/TLS支持:
Nginx支持SSL/TLS协议,可以提供安全的HTTPS服务,并支持常见的SSL功能,如密钥、证书的管理和配置。
URL重写:
Nginx提供强大的URL重写功能,可以根据需求对URL进行修改、重定向和映射,实现网站的美化和优化。
缓存加速:
Nginx可以使用内存、硬盘或者SSD等作为缓存存储空间,能够缓存静态内容和动态内容,并提供缓存的控制和管理。
Gzip压缩:
Nginx支持Gzip压缩算法,可以在传输过程中对内容进行压缩,减少传输的数据量,提升网站的性能。
日志记录:
Nginx可以将请求的访问日志和错误日志进行记录和保存,方便后续的分析和故障排查。
高并发支持:
Nginx采用异步非阻塞事件模型,能够同时处理成千上万的并发连接,适用于高并发的场景,如Web网站、API服务等。

应用场景

大型网站:Nginx的高并发处理能力和稳定性使其成为大型网站的首选Web服务器。
负载均衡:Nginx的负载均衡功能可以将请求分发到多个后端服务器上,提高系统的整体性能和稳定性。
API服务:Nginx可以作为API服务的反向代理和负载均衡器,提供统一的入口和访问控制。
静态文件服务:Nginx可以高效地处理静态文件请求,减少后端服务器的负担。
HTTPS服务:Nginx支持SSL/TLS加密,可以为HTTPS服务提供安全保障

架构图

nginx的安装

1,实验环境

给网卡配置IP,并启用

[root@bb ~]# vim /etc/NetworkManager/system-connections/eth0.connection 

[connection]

id=eth0

type=ethernet

interface-name=eth0



[ipv4]

method=manual

address1=172.25.254.200/24,172.25.254.2

dns=114.114.114.114;



[root@bb ~]# nmcli connection reload 
[root@bb ~]# nmcli connection up eth0

2,编译安装

在网上下载好压缩包,通过xftp把下载好的压缩包传输给linux

解压

[root@nginx1 ~]# tar zxf nginx-1.24.0.tar.gz 

安装需要编译的软件 gcc,pcre-devel,zlib-devel,openssl-devel

[root@nginx1 ~]# dnf install gcc pcre-devel zlib-devel openssl-devel -y


正在更新 Subscription Management 软件仓库。
无法读取客户身份

本系统尚未在权利服务器中注册。可使用 subscription-manager 进行注册。

上次元数据过期检查:0:18:56 前,执行于 2024年08月17日 星期六 14时30分10秒。
依赖关系解决。
=============================================================
 软件包          架构   版本                 仓库       大小
=============================================================
安装:
 gcc             x86_64 11.3.1-2.1.el9       AppStream  32 M
 openssl-devel   x86_64 1:3.0.1-43.el9_0     AppStream 4.1 M
 pcre-devel      x86_64 8.44-3.el9.3         AppStream 511 k
 zlib-devel      x86_64 1.2.11-34.el9        AppStream  47 k
安装依赖关系:
 glibc-devel     x86_64 2.34-40.el9          AppStream  47 k
 glibc-headers   x86_64 2.34-40.el9          AppStream 547 k
 kernel-headers  x86_64 5.14.0-162.6.1.el9_1 AppStream 3.4 M
 libxcrypt-devel x86_64 4.4.18-3.el9         AppStream  32 k
 make            x86_64 1:4.3-7.el9          BaseOS    542 k
 pcre-cpp        x86_64 8.44-3.el9.3         AppStream  28 k
 pcre-utf16      x86_64 8.44-3.el9.3         AppStream 188 k
 pcre-utf32      x86_64 8.44-3.el9.3         AppStream 178 k

事务概要
=============================================================
安装  12 软件包

总计:42 M
安装大小:102 M
下载软件包:
运行事务检查
事务检查成功。
运行事务测试
事务测试成功。
运行事务
  准备中  :                                              1/1 
  安装    : make-1:4.3-7.el9.x86_64                     1/12 
  安装    : pcre-utf32-8.44-3.el9.3.x86_64              2/12 
  安装    : pcre-utf16-8.44-3.el9.3.x86_64              3/12 
  安装    : pcre-cpp-8.44-3.el9.3.x86_64                4/12 
  安装    : kernel-headers-5.14.0-162.6.1.el9_1.x86_    5/12 
  安装    : glibc-headers-2.34-40.el9.x86_64            6/12 
  安装    : libxcrypt-devel-4.4.18-3.el9.x86_64         7/12 
  安装    : glibc-devel-2.34-40.el9.x86_64              8/12 
  安装    : gcc-11.3.1-2.1.el9.x86_64                                                                                               9/12 
  安装    : pcre-devel-8.44-3.el9.3.x86_64                                                                                         10/12 
  安装    : zlib-devel-1.2.11-34.el9.x86_64                                                                                        11/12 
  安装    : openssl-devel-1:3.0.1-43.el9_0.x86_64                                                                                  12/12 
  运行脚本: openssl-devel-1:3.0.1-43.el9_0.x86_64                                                                                  12/12 
  验证    : gcc-11.3.1-2.1.el9.x86_64                                                                                               1/12 
  验证    : glibc-devel-2.34-40.el9.x86_64                                                                                          2/12 
  验证    : glibc-headers-2.34-40.el9.x86_64                                                                                        3/12 
  验证    : kernel-headers-5.14.0-162.6.1.el9_1.x86_64                                                                              4/12 
  验证    : libxcrypt-devel-4.4.18-3.el9.x86_64                                                                                     5/12 
  验证    : openssl-devel-1:3.0.1-43.el9_0.x86_64                                                                                   6/12 
  验证    : pcre-cpp-8.44-3.el9.3.x86_64                                                                                            7/12 
  验证    : pcre-devel-8.44-3.el9.3.x86_64                                                                                          8/12 
  验证    : pcre-utf16-8.44-3.el9.3.x86_64                                                                                          9/12 
  验证    : pcre-utf32-8.44-3.el9.3.x86_64                                                                                         10/12 
  验证    : zlib-devel-1.2.11-34.el9.x86_64                                                                                        11/12 
  验证    : make-1:4.3-7.el9.x86_64                                                                                                12/12 
已更新安装的产品。

已安装:
  gcc-11.3.1-2.1.el9.x86_64                          glibc-devel-2.34-40.el9.x86_64              glibc-headers-2.34-40.el9.x86_64        
  kernel-headers-5.14.0-162.6.1.el9_1.x86_64         libxcrypt-devel-4.4.18-3.el9.x86_64         make-1:4.3-7.el9.x86_64                 
  openssl-devel-1:3.0.1-43.el9_0.x86_64              pcre-cpp-8.44-3.el9.3.x86_64                pcre-devel-8.44-3.el9.3.x86_64          
  pcre-utf16-8.44-3.el9.3.x86_64                     pcre-utf32-8.44-3.el9.3.x86_64              zlib-devel-1.2.11-34.el9.x86_64         

完毕!

切换到nginx的目录下,并创建一个用户

[root@nginx1 ~]# cd nginx-1.24.0/
[root@nginx1 nginx-1.24.0]# useradd -s /sbin/nologin -M nginx

选择参数安装,并安装到/usr/local/nginx下

[root@nginx1 nginx-1.24.0]# ./configure --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_ssl_module \
> --with-http_v2_module \
> --with-http_realip_module \
> --with-http_stub_status_module \
> --with-http_gzip_static_module \
> --with-pcre \
> --with-stream \
> --with-stream_ssl_module \
> --with-stream_realip_module

make

[root@nginx1 nginx-1.24.0]# make && make install

启动nginx

[root@nginx1 nginx]# cd /usr/local/nginx/sbin/
[root@nginx1 sbin]# ./nginx 

查看

[root@nginx1 sbin]# ps -aux |grep nginx
root       39782  0.0  0.0   9836   928 ?        Ss   15:07   0:00 nginx: master process ./nginx
nginx      39783  0.0  0.2  13724  4720 ?        S    15:07   0:00 nginx: worker process
root       39787  0.0  0.1 221680  2316 pts/1    S+   15:07   0:00 grep --color=auto nginx

因为启动nginx的时候要切换到/usr/local/nginx/sbin下,所以可以编译参数,使其可以在直接在任何路径下直接nginx就可以启动

[root@nginx1 ~]# 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
                                        






[root@nginx1 ~]# source ~/.bash_profile 

此时就可以在家目录下直接nginx启动

[root@nginx1 ~]# ps aux | grep nginx
root       39815  0.0  0.1 221680  2316 pts/1    S+   15:13   0:00 grep --color=auto nginx
[root@nginx1 ~]# nginx
[root@nginx1 ~]# ps aux | grep nginx
root       39817  0.0  0.0   9836   932 ?        Ss   15:13   0:00 nginx: master process nginx
nginx      39818  0.0  0.2  13724  4612 ?        S    15:13   0:00 nginx: worker process
root       39820  0.0  0.1 221680  2356 pts/1    S+   15:13   0:00 grep --color=auto nginx

可以用-v查看版本,-V查看具体的信息

[root@nginx1 ~]# nginx -v

nginx version: nginx/1.24.0


[root@nginx1 ~]# nginx -V

nginx version: nginx/1.24.0
built by gcc 11.3.1 20220421 (Red Hat 11.3.1-2) (GCC) 
built with OpenSSL 3.0.1 14 Dec 2021
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module

nginx的平滑升级和回滚

1,通过xftp把更高级的版本传输到Linux上,还有其相关的模块

2,解压

[root@nginx1 ~]# tar zxf nginx-1.26.1.tar.gz 
[root@nginx1 ~]# tar zxf echo-nginx-module-0.63.tar.gz 

选择参数,在之前的基础上加多了一个

[root@nginx1 nginx-1.26.1]# ./configure --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --add-module=/root/echo-nginx-module-0.63 \
> --with-http_ssl_module \
> --with-http_v2_module \
> --with-http_realip_module \
> --with-http_stub_status_module \
> --with-http_gzip_static_module \
> --with-pcre \
> --with-stream \
> --with-stream_ssl_module \
> --with-stream_realip_module

make,直接make就可以了,这次不需要再make install

[root@Nginx nginx-1.26.1]# make

把之前版本nginx给复制,以防新版本出问题,可以回滚

[root@nginx1 ~]# cd /usr/local/nginx/sbin/
[root@nginx1 sbin]# ls
nginx
[root@nginx1 sbin]# cp nginx nginx-24
[root@nginx1 sbin]# ls
nginx  nginx-24

复制新版本的nginx过去

[root@nginx1 sbin]# \cp -f /root/nginx-1.26.1/objs/nginx /usr/local/nginx/sbin/nginx

可以用-t来查看文件有没有问题

[root@nginx1 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

查看nginx的进程

[root@nginx1 sbin]# ps aux | grep nginx
root       43275  0.0  0.0   9836   932 ?        Ss   17:16   0:00 nginx: master process nginx
nginx      43276  0.0  0.2  13724  5288 ?        S    17:16   0:00 nginx: worker process
root       43308  0.0  0.1 221680  2360 pts/1    S+   17:18   0:00 grep --color=auto nginx

使用kill -USR2

[root@nginx1 sbin]# kill -USR2 43275


[root@nginx1 sbin]# ps aux | grep nginx

root       43275  0.0  0.1   9836  2624 ?        Ss   17:16   0:00 nginx: master process nginx
nginx      43276  0.0  0.2  13724  5288 ?        S    17:16   0:00 nginx: worker process
root       43309  0.0  0.3   9872  5948 ?        S    17:18   0:00 nginx: master process nginx
nginx      43310  0.0  0.2  13760  4632 ?        S    17:18   0:00 nginx: worker process
root       43312  0.0  0.1 221680  2252 pts/1    S+   17:18   0:00 grep --color=auto nginx

此时就会多出一个nginx的进程,但这个进程并没有被使用,还是1.24这个版本生效

回收旧版本

[root@nginx1 sbin]# kill -WINCH 43275

此时旧版本的nginx就会被停掉,新版本会生效

[root@nginx1 sbin]# ps aux | grep nginx


root       43275  0.0  0.1   9836  2624 ?        Ss   17:16   0:00 nginx: master process nginx
root       43309  0.0  0.3   9872  5948 ?        S    17:18   0:00 nginx: master process nginx
nginx      43310  0.0  0.2  13760  4632 ?        S    17:18   0:00 nginx: worker process
root       43321  0.0  0.1 221680  2312 pts/1    S+   17:26   0:00 grep --color=auto nginx

可以使用curl -I来查看

[root@nginx1 sbin]# curl -I 172.25.254.200


HTTP/1.1 200 OK
Server: nginx/1.26.1
Date: Sat, 17 Aug 2024 09:26:36 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Sat, 17 Aug 2024 07:04:14 GMT
Connection: keep-alive
ETag: "66c04b6e-267"
Accept-Ranges: bytes

回滚

在/usrlocal/nginx/sbin 

先在把nginx复制出来,再把1.24版本的复制回去,

[root@nginx1 sbin]# ls 
nginx  nginx-24

[root@nginx1 sbin]# cp nginx nginx-26

[root@nginx1 sbin]# ls
nginx  nginx-24  nginx-26

[root@nginx1 sbin]# mv nginx-24 nginx
mv:是否覆盖'nginx'? y

在使用kill命令,

[root@nginx1 sbin]# ps aux | grep nginx

root       43275  0.0  0.1   9836  2624 ?        Ss   17:16   0:00 nginx: master process nginx
root       43309  0.0  0.3   9872  5948 ?        S    17:18   0:00 nginx: master process nginx
nginx      43310  0.0  0.2  13760  5104 ?        S    17:18   0:00 nginx: worker process
root       43358  0.0  0.1 221680  2316 pts/1    S+   17:30   0:00 grep --color=auto nginx


[root@nginx1 sbin]# kill -HUP 43275


[root@nginx1 sbin]# ps aux | grep nginx

root       43275  0.0  0.1   9836  2624 ?        Ss   17:16   0:00 nginx: master process nginx
root       43309  0.0  0.3   9872  5948 ?        S    17:18   0:00 nginx: master process nginx
nginx      43310  0.0  0.2  13760  5104 ?        S    17:18   0:00 nginx: worker process
nginx      43359  0.0  0.2  13724  4696 ?        S    17:31   0:00 nginx: worker process
root       43361  0.0  0.1 221680  2316 pts/1    S+   17:31   0:00 grep --color=auto nginx

此时就h会变回原来的1.24版本

nginx的启动文件

[root@nginx1 ~]# vim /lib/systemd/system/nginx.service

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.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
PrivateTmp=true
[Install]
WantedBy=multi-user.target


[root@nginx1 ~]# systemctl daemon-reload

nginx的命令参数

效果

[root@nginx1 ~]# nginx -v
nginx version: nginx/1.26.1


[root@nginx1 ~]# nginx -V

nginx version: nginx/1.26.1
built by gcc 11.3.1 20220421 (Red Hat 11.3.1-2) (GCC) 
built with OpenSSL 3.0.1 14 Dec 2021
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --add-module=/root/echo-nginx-module-0.63 --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
[root@nginx1 ~]# 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

核心配置

1,全局配置

全局配置端,对全局生效,主要设置nginx的启动用户/组,启动的工作进程数量,工作模式,Nginx的PID路 径,日志路径等。

在/usr/local/nginx/conf/nginx.conf

user nginx nginx; 

error_log /usr/local/nginx/logs/error.log error; 

pid       /usr/local/nginx/logs/nginx.pid

http配置块

1,location的用法

在一个server中location配置段可存在多个,用于实现从uri到文件系统的路径映射; ngnix会根据用户请求的URI来检查定义的所有location,按一定的优先级找出一个最佳匹配, 而后应用其配置在没有使用正则表达式的时候,nginx会先在server中的多个location选取匹配度最 高的一个uri uri是用户请求的字符串,即域名后面的web文件路径 然后使用该location模块中的正则url和字符串,如果匹配成功就结束搜索,并使用此location处理 此请求。

在nginx的配置文件里
[root@nginx1 ~]# vim /usr/local/nginx/conf/nginx.conf

创建路径
[root@nginx1 ~]# mkdir -p /usr/local/nginx/conf.d

在该路径下编写参数

[root@nginx1 ~]# vim /usr/local/nginx/conf.d/mm.conf

server {
        listen 80;
        server_name www.timinglee.org;
        root /data/web/html;
        index index.html;
}


创建路径,并写入内容

[root@nginx1 ~]# mkdir -p /data/web/html

[root@nginx1 ~]# echo 111 > /data/web/html/index.html

重启nginx程序

[root@nginx1 ~]# nginx -s reload

测试

[root@nginx1 ~]# curl www.timinglee.org

111

如果没有本地解析可以访问IP或者在/etc/hosts做解析

root和alias

root

root:指定web的家目录,在定义location的时候,文件的绝对路径等于 root+location

1.在配置文件里

[root@nginx1 ~]# vim /usr/local/nginx/conf.d/mm.conf

server {
        listen 80;
        server_name www.timinglee.org;
        root /data/web/html;
        index index.html;

        location /test/ {
                root /data/web;
        }
}

创建路径,并写入内容,重启nginx

[root@nginx1 ~]# mkdir -p /data/web/test

[root@nginx1 ~]# echo test > /data/web/test/index.html

[root@nginx1 ~]# nginx -s reload

此时可以在window里的浏览器做测试,也可以在本虚拟机里测试

1.在window里要做解析

测试

此时为成功

alias 别名

alias:定义路径别名,会把访问的路径重新定义到其指定的路径,文档映射的另一种机制;仅能用于 location上下文,此指令使用较少

1.在配置文件里添加参数,并重启nginx

[root@nginx1 ~]# vim /usr/local/nginx/conf.d/mm.conf

server {
        listen 80;
        server_name www.timinglee.org;
        root /data/web/html;
        index index.html;

        location /test/ {
                root /data/web;
        }

        location /test2 {
                alias /data/web/test;
        }
}


[root@nginx1 ~]# nginx -s reload

在浏览器测试

此时访问的是test2但最后还是访问test

location的语法

配置的优先级

1,在目录匹配( = 不能目录匹配)

~和~*同级,是最优先匹配的,其次是不带符号的,最后是^~

2,在文件匹配

=是最优先匹配的,其次是~和~*,第三是不带符号的,最后是^~

实验

1,在配置文件里添加参数,

[root@nginx1 ~]# vim /usr/local/nginx/conf.d/mm.conf 


server {
        listen 80;
        server_name www.timinglee.org;
        root /data/web/html;
        index index.html;


        location = /test/imdex.html {
                root /data/web1;
        }

        location ^~ /t {
                root /data/web2;
        }

        location ~ .html$ {
                root /data/web3;
        }

        location ~* .HTML$ {
                root /data/wb4;
        }

        location /test {
                root /data/web5;
        }
}

2,创建目录和写入文件内容,重启nginx

[root@nginx1 ~]# mkdir -p /data/web{1..5}/test

[root@nginx1 ~]# echo web1 > /data/web1/test/index.html

[root@nginx1 ~]# echo web2 > /data/web2/test/index.html

[root@nginx1 ~]# echo web3 > /data/web3/test/index.html

[root@nginx1 ~]# echo web4 > /data/web4/test/index.html

[root@nginx1 ~]# echo web5 > /data/web5/test/index.html


[root@nginx1 ~]# nginx -s reload

可以在浏览器测试

nginx的用户认证

1,创建用户和密码

[root@nginx1 ~]# htpasswd -cmb /usr/local/nginx/conf/.htpasswd mm 123

Adding password for user mm

可以查看

[root@nginx1 ~]# cat /usr/local/nginx/conf/.htpasswd 

mm:$apr1$fgi91ACK$lL8tIdd7yM02F6yEAgt9o0

编辑文件,并重启

[root@nginx1 ~]# vim /usr/local/nginx/conf.d/mm.conf 

server {
        listen 80;
        server_name www.timinglee.org;
        root /data/web/html;
        index index.html;

        location /mm {
                root /data/web/html/timinglee.org;
                index index.html;
                auth_basic "login password";
                auth_basic_user_file "/usr/local/nginx/conf/.htpasswd";
        }

}


[root@nginx1 ~]# nginx -s reload

创建文件路径,并写入内容

[root@nginx1 ~]# mkdir /data/web/html/timinglee.org/mm -p

[root@nginx1 ~]# echo 111111 > /data/web/html/timinglee.org/mm/index.html

在浏览器测试

此时就访问成功

错误页面和日志自定义

1,错误页面

编辑配置文件,并重启服务

[root@nginx1 ~]# vim /usr/local/nginx/conf.d/mm.conf 

server {
        listen 80;
        server_name www.timinglee.org;
        root /data/web/html;
        index index.html;
        error_page 404 /40x.html;

        location /40x.html {
                root /data/web/html/error;
        }

}

[root@nginx1 ~]# nginx -s reload

创建文件路径,并写入内容

[root@nginx1 ~]# mkdir -p /data/web/html/error 
[root@nginx1 ~]# echo error page > /data/web/html/error/40x.html

在浏览器测试

此时在访问一个没有路径的时候就会自动跳转到自己定义的页面

2,日志自定义

1,创建文件路径

[root@nginx1 log]# mkdir -p /var/log/nginx

2,编辑配置文件,并重启

[root@nginx1 log]# vim /usr/local/nginx/conf.d/mm.conf 

server {
        listen 80;
        server_name www.timinglee.org;
        root /data/web/html;
        index index.html;
        error_page 404 /40x.html;
        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        location /40x.html {
                root /data/web/html/error;
        }

}

[root@nginx1 log]# nginx -s reload

测试:可以在浏览器随便访问,使其产生日志

查看

此时日志就会产生在自己所编辑的路径下

文件检测和长链接

1,检侧文件

创建访问页面

[root@nginx1 log]# echo not exit > /data/web/html/error/default.html

编辑server,并重启

[root@nginx1 log]# vim /usr/local/nginx/conf.d/mm.conf

server {
        listen 80;
        server_name www.timinglee.org;
        root /data/web/html;
        index index.html;
        error_page 404 /40x.html;
        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;
        try_files $uri $uri.html $uri/index.html /error/default.html;

        location /40x.html {
                root /data/web/html/error;
        }

}


[root@nginx1 log]# nginx -s reload

在浏览器测试

此时如果访问的文件不存在就会跳转到自定义的路径,提示文件不存在

2,长链接

1,在nginx的主配置文件里添加keepalive_requests参数,启动长链接功能

[root@nginx1 log]# vim /usr/local/nginx/conf/nginx.conf

此时我把一次长连接上所允许请求的的数量写为2,

测试前如果虚拟机里没有telnet,可以 使用yum先安装

[root@nginx1 log]# yum install telnet -y


正在更新 Subscription Management 软件仓库。
无法读取客户身份

本系统尚未在权利服务器中注册。可使用 subscription-manager 进行注册。

上次元数据过期检查:0:52:42 前,执行于 2024年08月19日 星期一 19时34分57秒。
依赖关系解决。
==============================================================
 软件包     架构       版本               仓库           大小
==============================================================
安装:
 telnet     x86_64     1:0.17-85.el9      AppStream      66 k

事务概要
==============================================================
安装  1 软件包

总计:66 k
安装大小:121 k
下载软件包:
运行事务检查
事务检查成功。
运行事务测试
事务测试成功。
运行事务
  准备中  :                                               1/1 
  安装    : telnet-1:0.17-85.el9.x86_64                   1/1 
  运行脚本: telnet-1:0.17-85.el9.x86_64                   1/1 
  验证    : telnet-1:0.17-85.el9.x86_64                   1/1 
已更新安装的产品。

已安装:
  telnet-1:0.17-85.el9.x86_64                                 

完毕!

测试

因为一次长连接上所允许请求的的数量写为2,所以在访问2次后就回自动退出

nginx作为下载服物器

1,编辑配置文件location,并重启

[root@nginx1 ~]# vim /usr/local/nginx/conf.d/mm.conf


server {
        listen 80;
        server_name www.timinglee.org;
        root /data/web/html;
        index index.html;
####### error_page 404 /40x.html;
        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;
        try_files $uri $uri.html $uri/index.html /error/default.html;


        location /download {
                autoindex on;
                autoindex_exact_size on;
                autoindex_localtime on;
                limit_rate 1024k;
        }
}

[root@nginx1 ~]# nginx -s reload

创建文件路径

[root@nginx1 ~]# mkdir -p /data/web/html/download

[root@nginx1 ~]# cp /root/anaconda-ks.cfg /data/web/html/download/

在浏览器测试

nginx的状态页

1,编辑配置文件,并重启服务

[root@nginx1 ~]# vim /usr/local/nginx/conf.d/mm.conf 

server {
        listen 80;
        server_name www.timinglee.org;
        root /data/web/html;
        index index.html;
        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;
        try_files $uri $uri.html $uri/index.html /error/default.html;

        location /status {
                stub_status;

        }

}

[root@nginx1 ~]# nginx -s reload

在浏览器测试

这个一般都是和用户认证一起做的,

则配置文件的内容为   配置完成后要重启

        location /status {
                stub_status;
                auth_basic "auth login";
                auth_basic_user_file /usr/local/nginx/conf/.htpasswd;
                allow 172.25.254.1;
                deny all;

        }

测试,此时就会需要账户和密码才能登录并访问到

nginx的压缩功能

1,编辑nginx的主配置文件

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

重启服务

[root@nginx1 ~]# nginx -s reload

创建文件进行测试,分别在文件里做一个小于1k的和大于1k的

[root@nginx1 ~]# mkdir /data/web/html/log


[root@nginx1 ~]# echo 200 > /data/web/html/log/test.html

[root@nginx1 ~]# cp  /usr//local/nginx/logs/access.log /data/web/html/log/data.txt

可以用du -sh 来查看文件的大小

[root@nginx1 ~]# du -sh /usr/local/nginx/logs/access.log 

12K	/usr/local/nginx/logs/access.lo

测试

此时两个文件一个小于1k的不被压缩,而大于1k的被压缩

nginx的变量使用

1,$remote_addr; 存放了客户端的地址,注意是客户端的公网IP

配置文件

[root@nginx1 ~]# vim /usr/local/nginx/conf.d/mm.conf 

server {
        listen 80;
        server_name www.timinglee.org;
        root /data/web/html;
        index index.html;
        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;
        try_files $uri $uri.html $uri/index.html /error/default.html;

        location /aa {
                default_type bb/html;
                echo $remote_addr;
        }
}

[root@nginx1 ~]# nginx -s reload

测试

2,$args; #变量中存放了URL中的所有参数

配置文件

[root@nginx1 ~]# vim /usr/local/nginx/conf.d/mm.conf 

server {
        listen 80;
        server_name www.timinglee.org;
        root /data/web/html;
        index index.html;
        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;
        try_files $uri $uri.html $uri/index.html /error/default.html;

        location /aa {
                default_type bb/html;
                echo $remote_addr;
                echo $args;
        }
}

[root@nginx1 ~]# nginx -s reload

测试

配置文件

[root@nginx1 ~]# vim /usr/local/nginx/conf.d/mm.conf 

        location /aa {
                default_type bb/html;
                echo $remote_addr;
                echo $args;
                echo $document_root;
                echo $document_uri;
                echo $host;
        }

测试

配置文件

        location /aa {
                default_type bb/html;
                echo $remote_addr;
                echo $args;
                echo $document_root;
                echo $document_uri;
                echo $host;
                echo $request_filename;
                echo $scheme;
                echo $server_protocol;
                echo $server_addr;
                echo $server_name;
                echo $server_port;
        }

测试

自定义变量

在配置文件里,添加配置,并重启nginx

[root@nginx1 ~]# vim /usr/local/nginx/conf.d/mm.conf

server {
        listen 80;
        server_name www.timinglee.org;
        root /data/web/html;
        index index.html;
        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;
        try_files $uri $uri.html $uri/index.html /error/default.html;


        location /bb {
                default_type cc/html;
                set $name timinglee;
                echo $name;

        }

}

[root@nginx1 ~]# nginx -s reload

测试

也可以结合已有的变量来进行自己定义变量

配置文件

     location /bb {
                default_type cc/html;
                set $name timinglee;
                echo $name;
                set $my_port $server_port;
                echo $my_port;
                echo $server_name:$server_port;
        }

测试

if指令

配置文件,使在服务不存在的文件时会直接 返回定义的内容,如果存在会直接显示该文件里的内容

[root@nginx1 ~]# vim /usr/local/nginx/conf.d/mm.conf 


server {
        listen 80;
        server_name www.timinglee.org;
        root /data/web/html;
        index index.html;
        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;
        try_files $uri $uri.html $uri/index.html /error/default.html;

        location /test {
                if (!-e $request_filename){
                        echo "$request_filename is no exit";

                }


        }

}



[root@nginx1 ~]# nginx -s reload

测试

[root@nginx1 ~]# curl www.timinglee.org/test

/data/web/html/test is no exit

break指令

用于中断当前相同作用域(location)中的其他Nginx配置 与该指令处于同一作用域的Nginx配置中,位于它前面的配置生效 位于后面的 ngx_http_rewrite_module 模块中指令就不再执行 Nginx服务器在根据配置处理请求的过程中遇到该指令的时候,回到上一层作用域继续向下读取配置,、 该指令可以在server块和locationif块中使用

配置文件

[root@nginx1 ~]# vim /usr/local/nginx/conf.d/mm.conf 


        location /break {
                default_type text.html;
                set $name mqw;
                echo $name;
                set $id 32;
                echo $id;
        }


[root@nginx1 ~]# nginx -s reload

测试,此时是没有加breakz指令的

加上break指令后

        location /break {
                default_type text.html;
                set $name mqw;
                echo $name;
                break;
                set $id 32;
                echo $id;
        }

测试,此时break后的就会被打断

可以结合if指令来做判断

配置

        location /break {
                default_type text.html;
                set $name mqw;
                echo $name;
                if ($http_user_agent = curl){
                break;
                        }
                set $id 32;
                echo $id;
        }

return指令

配置

        location /return {
                default_type text.html;
                if (!-e $request_filename){
                        return 301 http://www.baidu.com;
                }
        }

测试,此时服务没有文件就会被定向到百度去

正则表达式

rewrite指令

通过正则表达式的匹配来改变URI,可以同时存在一个或多个指令,按照顺序依次对URI进行匹配, rewrite主要是针对用户请求的URL或者是URI做具体处理

永久重定向

配置

[root@nginx1 ~]# vim /usr/local/nginx/conf.d/mm.conf 

server {
        listen 80;
        server_name www.timinglee.org;
        root /data/web/html;
        index index.html;
        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;
        try_files $uri $uri.html $uri/index.html /error/default.html;

        location / {
                root /data/web/html/re;
                index index.html;
                rewrite / http://www.timinglee.com permanent;

        }

}

[root@nginx1 ~]# nginx -s reload

测试

临时重定向



    location / {
          root /data/web/html/re;
          index index.html;
          rewrite / http://www.timinglee.com redirect;

    }


break和last

配置场景  创建文件和内容,

[root@nginx1 ~]# mkdir  /data/web/html/{test1,test2,break,last} -p

[root@nginx1 ~]# echo test1 > /data/web/html/test1/index.html

[root@nginx1 ~]# echo test2 > /data/web/html/test2/index.html

[root@nginx1 ~]# echo last > /data/web/html/last/index.html

[root@nginx1 ~]# echo break > /data/web/html/break/index.html

配置,并重启

[root@nginx1 ~]# vim /usr/local/nginx/conf.d/mm.conf 

server {
        listen 80;
        server_name www.timinglee.org;
        root /data/web/html;
        index index.html;
        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;
        try_files $uri $uri.html $uri/index.html /error/default.html;

        location /break {
                rewrite ^/break/(.*) /test1/$1;
                rewrite ^/test1/(.*) /test2/$1;
        }
        location /last {
                rewrite ^/last/(.*) /test1/$1;
                rewrite ^/test1/(.*) /test2/$1;
        }
        location /test1 {
                default_type text.html;
                echo "00000";
        }
        location /test2 {
                root /data/web/html;
        }


}

[root@nginx1 ~]# nginx -s reload

此时在浏览器可以正常访问到文件的内容

而当在内部加了break后,会直接打断location的执行,即使后面的location同也会被打断

配置

        location /break {
                rewrite ^/break/(.*) /test1/$1 break;
                rewrite ^/test1/(.*) /test2/$1;
        }

测试

而加上last只会打断在这个location语句块last后面的执行

        location /last {
                rewrite ^/last/(.*) /test1/$1 last;
                rewrite ^/test1/(.*) /test2/$1;
        }

测试

自动跳转https

1,创建目录,制作证书

[root@nginx1 ~]# mkdir -p /data/web/html/certs

[root@nginx1 ~]# openssl req -newkey rsa:2048 -nodes -sha256 -keyout /data/web/html/certs/timinglee.org.key -x509 -days 365 -out /data/web/html/certs/timinglee.org.crt


.........+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*..+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*..+....+......+.........+.........+......+.....+......+...+....+......+.........+.....+.+..+.............+......+..............+.........+............+...+...+............+......+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
...+...+...+.........+...+..+...+.+..............+......+....+...+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*................................+.....+....+..+.+..+...+.........+....+.........+...+...+......+.....+....+..+.......+...+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*..+..+.........+.+.........+.....+....+............+.........+...+...+...+.....+.......+............+..+.........+.+...+...........+......+...+.+..+.......+........+.......+...+........+.+...+..+...+............+.+...+..+....+......+..................+.....+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:cn
State or Province Name (full name) []:tim
Locality Name (eg, city) [Default City]:gx
Organization Name (eg, company) [Default Company Ltd]:qz
Organizational Unit Name (eg, section) []:timinglee
Common Name (eg, your name or your server's hostname) []:tim
Email Address []:tim

编辑配置server和location

[root@nginx1 ~]# vim /usr/local/nginx/conf.d/mqw.conf


server {
        listen 443 ssl;
        listen 80;
        ssl_certificate /data/web/html/certs/timinglee.org.crt;
        ssl_certificate_key /data/web/html/certs/timinglee.org.key;
        ssl_session_cache shared:sslcache:20m;
        ssl_session_timeout 10m;
        server_name aaa.timinglee.org;

        location / {
                root /data/web/html/pc;
                index index.html;
                if ($scheme = http ){
                        rewrite / https://$host redirect;
                }
        }
}

[root@nginx1 ~]# nginx -s reload

在浏览器测试,输入http的格式他会自动跳到https

测试之前要在window里做解析

在C:\Windows\System32\drivers\etc添加一个为aaa.timinglee.org的域名

nginx防盗链

新建一台虚拟机

[root@hh ~]# vim /etc/NetworkManager/system-connections/eth0.connection

[connection]

id=eth0

type=ethernet

interface-name=eth0



[ipv4]

method=manual

address1=172.25.254.100/24,172.25.254.2

dns=114.114.114.114;


[root@hh ~]# nmcli connection reload 
[root@hh ~]# nmcli connection up eth0 

安装httpd

[root@hh ~]# yum install httpd -y


正在更新 Subscription Management 软件仓库。
无法读取客户身份

本系统尚未在权利服务器中注册。可使用 subscription-manager 进行注册。

AppStream                     198 MB/s | 6.1 MB     00:00    
BaseOS                        151 MB/s | 1.7 MB     00:00    
依赖关系解决。
==============================================================
 软件包              架构    版本            仓库        大小
==============================================================
安装:
 httpd               x86_64  2.4.53-7.el9    AppStream   54 k
安装依赖关系:
 apr                 x86_64  1.7.0-11.el9    AppStream  127 k
 apr-util            x86_64  1.6.1-20.el9    AppStream   98 k
 apr-util-bdb        x86_64  1.6.1-20.el9    AppStream   15 k
 httpd-core          x86_64  2.4.53-7.el9    AppStream  1.5 M
 httpd-filesystem    noarch  2.4.53-7.el9    AppStream   18 k
 httpd-tools         x86_64  2.4.53-7.el9    AppStream   89 k
 redhat-logos-httpd  noarch  90.4-1.el9      AppStream   18 k
安装弱的依赖:
 apr-util-openssl    x86_64  1.6.1-20.el9    AppStream   17 k
 mod_http2           x86_64  1.15.19-2.el9   AppStream  153 k
 mod_lua             x86_64  2.4.53-7.el9    AppStream   64 k

事务概要
==============================================================
安装  11 软件包

总计:2.1 M
安装大小:5.9 M
下载软件包:
运行事务检查
事务检查成功。
运行事务测试
事务测试成功。
运行事务
  准备中  :                                               1/1 
  安装    : apr-1.7.0-11.el9.x86_64                      1/11 
  安装    : apr-util-bdb-1.6.1-20.el9.x86_64             2/11 
  安装    : apr-util-openssl-1.6.1-20.el9.x86_64         3/11 
  安装    : apr-util-1.6.1-20.el9.x86_64                 4/11 
  安装    : httpd-tools-2.4.53-7.el9.x86_64              5/11 
  安装    : redhat-logos-httpd-90.4-1.el9.noarch         6/11 
  运行脚本: httpd-filesystem-2.4.53-7.el9.noarch         7/11 
  安装    : httpd-filesystem-2.4.53-7.el9.noarch                                                                                    7/11 
  安装    : httpd-core-2.4.53-7.el9.x86_64                                                                                          8/11 
  安装    : mod_http2-1.15.19-2.el9.x86_64                                                                                          9/11 
  安装    : mod_lua-2.4.53-7.el9.x86_64                                                                                            10/11 
  安装    : httpd-2.4.53-7.el9.x86_64                                                                                              11/11 
  运行脚本: httpd-2.4.53-7.el9.x86_64                                                                                              11/11 
  验证    : apr-1.7.0-11.el9.x86_64                                                                                                 1/11 
  验证    : apr-util-1.6.1-20.el9.x86_64                                                                                            2/11 
  验证    : apr-util-bdb-1.6.1-20.el9.x86_64                                                                                        3/11 
  验证    : apr-util-openssl-1.6.1-20.el9.x86_64                                                                                    4/11 
  验证    : httpd-2.4.53-7.el9.x86_64                                                                                               5/11 
  验证    : httpd-core-2.4.53-7.el9.x86_64                                                                                          6/11 
  验证    : httpd-filesystem-2.4.53-7.el9.noarch                                                                                    7/11 
  验证    : httpd-tools-2.4.53-7.el9.x86_64                                                                                         8/11 
  验证    : mod_http2-1.15.19-2.el9.x86_64                                                                                          9/11 
  验证    : mod_lua-2.4.53-7.el9.x86_64                                                                                            10/11 
  验证    : redhat-logos-httpd-90.4-1.el9.noarch                                                                                   11/11 
已更新安装的产品。

已安装:
  apr-1.7.0-11.el9.x86_64                       apr-util-1.6.1-20.el9.x86_64                  apr-util-bdb-1.6.1-20.el9.x86_64         
  apr-util-openssl-1.6.1-20.el9.x86_64          httpd-2.4.53-7.el9.x86_64                     httpd-core-2.4.53-7.el9.x86_64           
  httpd-filesystem-2.4.53-7.el9.noarch          httpd-tools-2.4.53-7.el9.x86_64               mod_http2-1.15.19-2.el9.x86_64           
  mod_lua-2.4.53-7.el9.x86_64                   redhat-logos-httpd-90.4-1.el9.noarch         

完毕!

在默认访问路径下添加页面

[root@hh ~]# vim /var/www/html/index.html


<html>

  <head>
    <meta http-equiv=Content-Type content="text/html;charset=utf-8">
    <title>盗链</title>
</head>

  <body>
    <img src="http://www.timinglee.org/images/m.png" >
    <h1 style="color:red">欢迎大家</h1>
    <p><a href=http://www.timinglee.org>狂点老李</a>出门见喜</p>
  </body>

</html>

在nginx的主机里,添加路径和页面

[root@nginx1 ~]# mkdir -p /data/web/html/images

使用xftp来给虚拟机添加图片

添加配置,并重启

[root@nginx1 ~]# vim /usr/local/nginx/conf.d/mm.conf 

server {
        listen 80;
        server_name www.timinglee.org;
        root /data/web/html;
        index index.html;
        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;
        try_files $uri $uri.html $uri/index.html /error/default.html;

        location /images {

        }

}

[root@nginx1 ~]# nginx -s reload

此时在浏览器测试

然后点击之后就会跳转到www.timinglee.org这台主机的页面

如果配置了防盗链

配置

        location / {
                valid_referers none blocked server_names *.timinglee.org ~/.baidu/.;
                if ( $invalid_referer ){
                        return 404;
                }
        }

此时,就会阻止别人盗链

点击后也会出现404

反向代理

1,多添加一台虚拟机

[root@hh2 ~]# vim /etc/NetworkManager/system-connections/eth0.connection

[connection]

id=eth0

type=ethernet

interface-name=eth0



[ipv4]

method=manual

address1=172.25.254.110/24,172.25.254.2

dns=114.114.114.114;


[root@hh2 ~]# nmcli connection reload 
[root@hh2 ~]# nmcli connection up eth0 

安装httpd,启动服务

[root@hh2 ~]# yum install httpd -y

[root@hh2 ~]# systemctl start httpd

l两台主机添加内容

[root@hh ~]# echo 100 > /var/www/html/index.html

[root@hh2 ~]# echo 110 > /var/www/html/index.html

2,配置

[root@nginx1 ~]# vim /usr/local/nginx/conf.d/mm.conf 

server {
        listen 80;
        server_name www.timinglee.org;

        location / {
                proxy_pass http://172.25.254.100:80;
        }
        location /static {
                proxy_pass http://172.25.254.110:80;
        }
}


[root@nginx1 ~]# nginx -s reload

在110主机上

[root@hh2 ~]# mkdir -p /var/www/html/static

[root@hh2 ~]# echo 110 > /var/www/html/static/index.html

测试

反向代理的缓存功能

1,在主配置文件里添加参数

编辑配置文件

[root@nginx1 ~]# vim /usr/local/nginx/conf.d/mm.conf 

server {
        listen 80;
        server_name www.timinglee.org;

        location / {
                proxy_pass http://172.25.254.100:80;
        }
        location /static {
                proxy_pass http://172.25.254.110:80;
                proxy_cache proxycache;
                proxy_cache_key $request_uri;
                proxy_cache_valid 200 302 301 10m;
                proxy_cache_valid any 1m;
        }
}


[root@nginx1 ~]# nginx -s reload

进行压测测试

[root@hh ~]# ab -n1000 -c100 http://www.timinglee.org/static/index.html


This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking www.timinglee.org (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software:        Apache/2.4.53
Server Hostname:        www.timinglee.org
Server Port:            80

Document Path:          /static/index.html
Document Length:        196 bytes

Concurrency Level:      100
Time taken for tests:   0.134 seconds
Complete requests:      1000
Failed requests:        0
Non-2xx responses:      1000
Total transferred:      394000 bytes
HTML transferred:       196000 bytes
Requests per second:    7464.86 [#/sec] (mean)
Time per request:       13.396 [ms] (mean)
Time per request:       0.134 [ms] (mean, across all concurrent requests)
Transfer rate:          2872.22 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   2.5      0      10
Processing:     4   12   7.3     10      62
Waiting:        1   10   5.7     10      50
Total:          5   12   8.6     11      70

Percentage of the requests served within a certain time (ms)
  50%     11
  66%     12
  75%     12
  80%     13
  90%     21
  95%     29
  98%     42
  99%     55
 100%     70 (longest request)

反向代理的负载均衡

Nginx 可以基于ngx_http_upstream_module模块提 供服务器分组转发、权重分配、状态监测、调度算法等高级功能

修改配置文件,并重启服务

[root@nginx1 ~]# vim /usr/local/nginx/conf.d/mm.conf

upstream web {
        server 172.25.254.100:80 fail_timeout=15s max_fails=3;
        server 172.25.254.110:80 fail_timeout=15s max_fails=3;
}

server {
        listen 80;
        server_name www.timinglee.org;

        location / {
                proxy_pass http://web;
        }


}

[root@nginx1 ~]# nginx -s reload

测试

可以修改算法(默认是轮询)添加

如下示例

upstream web {
        ip_hash;
        server 172.25.254.100:80 fail_timeout=15s max_fails=3;
        server 172.25.254.110:80 fail_timeout=15s max_fails=3;
}

nginx实现tcp负载均衡

udp主要用于 DNS的域名解析,其配置方式和指令和http 代理类似,其基于ngx_stream_proxy_module模块实现tcp 负载,另外基于模块ngx_stream_upstream_module实现后端服务器分组转发、权重分配、状态监测、 调度算法等高级功能。 如果编译安装,需要指定 --with-stream 选项才能支持ngx_stream_proxy_module模块

1,在后端主机上安装bing,两台都装

[root@hh ~]# yum install bind -y


正在更新 Subscription Management 软件仓库。
无法读取客户身份

本系统尚未在权利服务器中注册。可使用 subscription-manager 进行注册。

上次元数据过期检查:0:31:14 前,执行于 2024年08月20日 星期二 16时38分27秒。
依赖关系解决。
==============================================================
 软件包            架构   版本                仓库       大小
==============================================================
安装:
 bind              x86_64 32:9.16.23-5.el9_1  AppStream 507 k
安装依赖关系:
 bind-dnssec-doc   noarch 32:9.16.23-5.el9_1  AppStream  50 k
 python3-bind      noarch 32:9.16.23-5.el9_1  AppStream  72 k
 python3-ply       noarch 3.11-14.el9         AppStream 111 k
安装弱的依赖:
 bind-dnssec-utils x86_64 32:9.16.23-5.el9_1  AppStream 121 k

事务概要
==============================================================
安装  5 软件包

总计:861 k
安装大小:2.5 M
下载软件包:
运行事务检查
事务检查成功。
运行事务测试
事务测试成功。
运行事务
  准备中  :                                               1/1 
  安装    : python3-ply-3.11-14.el9.noarch                1/5 
  安装    : python3-bind-32:9.16.23-5.el9_1.noarch        2/5 
  安装    : bind-dnssec-doc-32:9.16.23-5.el9_1.noarch     3/5 
  安装    : bind-dnssec-utils-32:9.16.23-5.el9_1.x86_64   4/5 
  运行脚本: bind-32:9.16.23-5.el9_1.x86_64                5/5 
  安装    : bind-32:9.16.23-5.el9_1.x86_64                5/5 
  运行脚本: bind-32:9.16.23-5.el9_1.x86_64                5/5 
  验证    : bind-32:9.16.23-5.el9_1.x86_64                1/5 
  验证    : bind-dnssec-doc-32:9.16.23-5.el9_1.noarch     2/5 
  验证    : bind-dnssec-utils-32:9.16.23-5.el9_1.x86_64   3/5 
  验证    : python3-bind-32:9.16.23-5.el9_1.noarch        4/5 
  验证    : python3-ply-3.11-14.el9.noarch                5/5 
已更新安装的产品。

已安装:
  bind-32:9.16.23-5.el9_1.x86_64                              
  bind-dnssec-doc-32:9.16.23-5.el9_1.noarch                   
  bind-dnssec-utils-32:9.16.23-5.el9_1.x86_64                 
  python3-bind-32:9.16.23-5.el9_1.noarch                      
  python3-ply-3.11-14.el9.noarch                              

完毕!

编辑主配置文件,两台都改

[root@hh ~]# vim /etc/named.conf 

[root@hh ~]# vim /etc/named.rfc1912.zones



创建文件,两台都要改,并启动

[root@hh ~]# cd /var/named/

[root@hh named]# cp /var/named/named.localhost  /var/named/timinglee.org.zone -p

[root@hh named]# vim timinglee.org.zone

$TTL 1D
@       IN SOA  ns.timinglee.org. root.riminglee.org. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      ns.timinglee.org.
ns      A       172.25.254.100
www     A       172.25.254.100


[root@hh named]# systemctl start named

查看

[root@hh named]# dig www.timinglee.org @172.25.254.100


; <<>> DiG 9.16.23-RH <<>> www.timinglee.org @172.25.254.100
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 60132
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
; COOKIE: ae677f57fab0b06f0100000066c4619fce73b49c6d9cfbfa (good)
;; QUESTION SECTION:
;www.timinglee.org.		IN	A

;; ANSWER SECTION:
www.timinglee.org.	86400	IN	A	172.25.254.100

;; Query time: 6 msec
;; SERVER: 172.25.254.100#53(172.25.254.100)
;; WHEN: Tue Aug 20 17:27:59 CST 2024
;; MSG SIZE  rcvd: 90

2,在nginx主机上

在主配置文件添加一个在http块外的子配置文件

创建文件

[root@nginx1 ~]# mkdir -p /usr/local/nginx/tcpconf.d

编辑配置文件,并重启

[root@nginx1 ~]# vim /usr/local/nginx/tcpconf.d/dns.conf


stream {
        upstream dns {
                server 172.25.254.100:53 fail_timeout=15s max_fails=3;
                server 172.25.254.110:53 fail_timeout=15s max_fails=3;
        }

        server {
                listen 53 udp reuseport;
                proxy_timeout 20s;
                proxy_pass dns;
        }
}

[root@nginx1 ~]# nginx -s reload

可以检查端口是否开启

[root@nginx1 ~]# netstat -antlupe | grep 53


tcp        0      0 172.25.254.200:22       172.25.254.1:65369      ESTABLISHED 0          23238      1023/sshd: root [pr 
udp        0      0 0.0.0.0:53              0.0.0.0:*                           0          41400      2323/nginx: master  
udp        0      0 0.0.0.0:5353            0.0.0.0:*                           70         23012      879/avahi-daemon: r 
udp6       0      0 :::5353                 :::*                                70         23013      879/avahi-daemon: r 

此时就可以实现tcp的负载均衡

[root@nginx1 ~]# dig www.timinglee.org @172.25.254.200


; <<>> DiG 9.16.23-RH <<>> www.timinglee.org @172.25.254.200
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 51524
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
; COOKIE: 5ce8d05211adb3ed0100000066c468351961815cd1b1d338 (good)
;; QUESTION SECTION:
;www.timinglee.org.		IN	A

;; ANSWER SECTION:
www.timinglee.org.	86400	IN	A	172.25.254.100

;; Query time: 11 msec
;; SERVER: 172.25.254.200#53(172.25.254.200)
;; WHEN: Tue Aug 20 17:56:05 CST 2024
;; MSG SIZE  rcvd: 90



[root@nginx1 ~]# dig www.timinglee.org @172.25.254.200



; <<>> DiG 9.16.23-RH <<>> www.timinglee.org @172.25.254.200
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 63448
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
; COOKIE: 02e5b7d846e2d6490100000066c46838b0ddbe787e31810d (good)
;; QUESTION SECTION:
;www.timinglee.org.		IN	A

;; ANSWER SECTION:
www.timinglee.org.	86400	IN	A	172.25.254.110

;; Query time: 8 msec
;; SERVER: 172.25.254.200#53(172.25.254.200)
;; WHEN: Tue Aug 20 17:56:08 CST 2024
;; MSG SIZE  rcvd: 90

nginx的数据库的负载均衡

1,在后端安装mysql,两台都装

[root@hh2 ~]# dnf install mariadb-server -y


正在更新 Subscription Management 软件仓库。
无法读取客户身份

本系统尚未在权利服务器中注册。可使用 subscription-manager 进行注册。

上次元数据过期检查:0:43:59 前,执行于 2024年08月20日 星期二 16时49分55秒。
依赖关系解决。
==============================================================
 软件包              架构   版本              仓库       大小
==============================================================
安装:
 mariadb-server      x86_64 3:10.5.16-2.el9_0 AppStream 9.4 M
安装依赖关系:
 mariadb             x86_64 3:10.5.16-2.el9_0 AppStream 1.6 M
 mariadb-common      x86_64 3:10.5.16-2.el9_0 AppStream  39 k
 mariadb-connector-c x86_64 3.2.6-1.el9_0     AppStream 203 k
 mariadb-connector-c-config
                     noarch 3.2.6-1.el9_0     AppStream  11 k
 mariadb-errmsg      x86_64 3:10.5.16-2.el9_0 AppStream 227 k
 mysql-selinux       noarch 1.0.5-1.el9_0     AppStream  37 k
 perl-DBD-MariaDB    x86_64 1.21-16.el9_0     AppStream 156 k
 perl-File-Copy      noarch 2.34-479.el9      AppStream  30 k
 perl-Sys-Hostname   x86_64 1.23-479.el9      AppStream  29 k
安装弱的依赖:
 mariadb-backup      x86_64 3:10.5.16-2.el9_0 AppStream 6.4 M
 mariadb-gssapi-server
                     x86_64 3:10.5.16-2.el9_0 AppStream  21 k
 mariadb-server-utils
                     x86_64 3:10.5.16-2.el9_0 AppStream 222 k

事务概要
==============================================================
安装  13 软件包

总计:18 M
安装大小:108 M
下载软件包:
运行事务检查
事务检查成功。
运行事务测试
事务测试成功。
运行事务
  准备中  :                                               1/1 
  安装    : mariadb-connector-c-config-3.2.6-1.el9_0.    1/13 
  安装    : mariadb-common-3:10.5.16-2.el9_0.x86_64      2/13 
  安装    : mariadb-connector-c-3.2.6-1.el9_0.x86_64     3/13 
  安装    : perl-Sys-Hostname-1.23-479.el9.x86_64        4/13 
  安装    : perl-DBD-MariaDB-1.21-16.el9_0.x86_64        5/13 
  安装    : mariadb-errmsg-3:10.5.16-2.el9_0.x86_64      6/13 
  安装    : perl-File-Copy-2.34-479.el9.noarch           7/13 
  运行脚本: mysql-selinux-1.0.5-1.el9_0.noarch           8/13 
  安装    : mysql-selinux-1.0.5-1.el9_0.noarch           8/13 
  运行脚本: mysql-selinux-1.0.5-1.el9_0.noarch           8/13 
libsemanage.semanage_direct_install_info: Overriding mysql module at lower priority 100 with module at priority 200.

  安装    : mariadb-backup-3:10.5.16-2.el9_0.x86_64      9/13 
  安装    : mariadb-gssapi-server-3:10.5.16-2.el9_0.x   10/13 
  安装    : mariadb-server-utils-3:10.5.16-2.el9_0.x8   11/13 
  运行脚本: mariadb-server-3:10.5.16-2.el9_0.x86_64     12/13 
  安装    : mariadb-server-3:10.5.16-2.el9_0.x86_64     12/13 
  运行脚本: mariadb-server-3:10.5.16-2.el9_0.x86_64     12/13 
  安装    : mariadb-3:10.5.16-2.el9_0.x86_64            13/13 
  运行脚本: mysql-selinux-1.0.5-1.el9_0.noarch          13/13 
  运行脚本: mariadb-3:10.5.16-2.el9_0.x86_64            13/13 
  验证    : mariadb-3:10.5.16-2.el9_0.x86_64             1/13 
  验证    : mariadb-backup-3:10.5.16-2.el9_0.x86_64      2/13 
  验证    : mariadb-common-3:10.5.16-2.el9_0.x86_64      3/13 
  验证    : mariadb-connector-c-3.2.6-1.el9_0.x86_64     4/13 
  验证    : mariadb-connector-c-config-3.2.6-1.el9_0.    5/13 
  验证    : mariadb-errmsg-3:10.5.16-2.el9_0.x86_64      6/13 
  验证    : mariadb-gssapi-server-3:10.5.16-2.el9_0.x    7/13 
  验证    : mariadb-server-3:10.5.16-2.el9_0.x86_64      8/13 
  验证    : mariadb-server-utils-3:10.5.16-2.el9_0.x8    9/13 
  验证    : mysql-selinux-1.0.5-1.el9_0.noarch          10/13 
  验证    : perl-DBD-MariaDB-1.21-16.el9_0.x86_64       11/13 
  验证    : perl-File-Copy-2.34-479.el9.noarch          12/13 
  验证    : perl-Sys-Hostname-1.23-479.el9.x86_64       13/13 
已更新安装的产品。

已安装:
  mariadb-3:10.5.16-2.el9_0.x86_64                            
  mariadb-backup-3:10.5.16-2.el9_0.x86_64                     
  mariadb-common-3:10.5.16-2.el9_0.x86_64                     
  mariadb-connector-c-3.2.6-1.el9_0.x86_64                    
  mariadb-connector-c-config-3.2.6-1.el9_0.noarch             
  mariadb-errmsg-3:10.5.16-2.el9_0.x86_64                     
  mariadb-gssapi-server-3:10.5.16-2.el9_0.x86_64              
  mariadb-server-3:10.5.16-2.el9_0.x86_64                     
  mariadb-server-utils-3:10.5.16-2.el9_0.x86_64               
  mysql-selinux-1.0.5-1.el9_0.noarch                          
  perl-DBD-MariaDB-1.21-16.el9_0.x86_64                       
  perl-File-Copy-2.34-479.el9.noarch                          
  perl-Sys-Hostname-1.23-479.el9.x86_64                       

完毕!

编辑配置文件。让后面测试时可以直观的看出来

[root@hh ~]# vim /etc/my.cnf.d/mariadb-server.cnf 

启动服务

[root@hh2 ~]# systemctl start mariadb.service 

配置远程访问授权,两台都做

[root@hh ~]# mysql

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 10.5.16-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


MariaDB [(none)]> create user mqw@'%' identified by '123';


Query OK, 0 rows affected (0.002 sec)


MariaDB [(none)]> grant ALL on *.* to mqw@'%';


Query OK, 0 rows affected (0.001 sec)

在nginx主机上,编辑配置文件



        upstream mysql {
                server 172.25.254.100:3306 fail_timeout=15s max_fails=3;
                server 172.25.254.110:3306 fail_timeout=15s max_fails=3;
        }

        server {
                listen 3306;
                proxy_timeout 60s;
                proxy_pass mysql;
        }

查看端口i

测试

先安装mysql的客户端来使主机能使用MySQL命令

[root@nginx1 ~]# dnf install mariadb -y
[root@nginx1 ~]# mysql -u mqw -p -h 172.25.254.200
Enter password: 


Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 10.5.16-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.



MariaDB [(none)]> select @@server_id;


+-------------+
| @@server_id |
+-------------+
|         100 |
+-------------+
1 row in set (0.001 sec)

MariaDB [(none)]> \q
Bye


[root@nginx1 ~]# mysql -u mqw -p -h 172.25.254.200
Enter password: 


Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 10.5.16-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


MariaDB [(none)]> select @@server_id;


+-------------+
| @@server_id |
+-------------+
|         110 |
+-------------+
1 row in set (0.001 sec)

实现 FastCGI

FastCGI‌(Fast Common Gateway Interface)是一个编程接口,旨在加速公共网关接口(CGI)的性能。它通过减少Web服务器与CGI程序之间互动的开销,使得服务器能够同时处理更多的Web请求。FastCGI通过使用持续的进程来处理一系列的请求,而不是为每个请求创建一个新的进程,从而提高了性能和效率。这种机制由FastCGI进程管理器管理,而不是由Web服务器直接管理,实现了应用程序与Web服务器的分离,提供了更高的安全性和稳定性

特点

FastCGI的主要特点和优势包括:

‌高性能‌:通过减少不必要的进程创建和销毁,FastCGI显著提高了处理大量并发请求的能力。
‌分布式计算‌:支持在Web服务器以外的机器上运行FastCGI应用程序,充分利用系统性能和提升安全性。
‌模块化认证和身份检查‌:FastCGI可以执行模块化的认证和身份检查,提供更多的功能和角色。
‌语言无关性‌:与CGI一样,FastCGI也是语言无关的,支持任何语言编写的处理程序。
‌高安全性‌:FastCGI进程独立于Web服务器运行,提供了比CGI更高的安全性。
FastCGI的实现包括几个关键部分:初始化、接收请求循环、处理请求、以及响应结束循环。它作为一个独立的代码,提供了一个跨平台和在Web服务器上使用的无知识产权的方法。FastCGI的版权属于Open Market公司,该公司提供FastCGI的免费使用并将其作为一个开放的标准。FastCGI的支持广泛,包括Apache、Nginx、IIS、Lighttpd等Web服务器

程重新编译nginx

删除之前的nginx,(如果需要之前的配置可以使用平滑升级或回滚来保留)

[root@nginx1 local]# rm -fr /usr/local/nginx/

重新编译nginx

通过xftp来给虚拟机传输压缩包

解压

[root@nginx1 ~]# tar zxf memc-nginx-module-0.20.tar.gz 

[root@nginx1 ~]# tar zxf srcache-nginx-module-0.33.tar.gz 

编译

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


[root@nginx1 nginx-1.26.1]# ./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 \
> --user=nginx \
> --group=nginx \
> --with-http_ssl_module \
> --with-http_v2_module \
> --with-http_realip_module \
> --with-http_stub_status_module \
> --with-http_gzip_static_module \
> --with-stream \
> --with-stream_ssl_module \
> --with-pcre \
> --with-stream_realip_module
[root@nginx1 nginx-1.26.1]# make && make install 

启动nginx服务

[root@nginx1 ~]# nginx 

安装php

通过xftp来给虚拟机传输PHP的相关压缩包

安装php前先把相关的捆绑包先安装

root@nginx1 php-8.3.9]# yum install -y bzip2 systemd-devel libxml2-devel sqlite-devel libpng-devel libcurl-devel


正在更新 Subscription Management 软件仓库。
无法读取客户身份

本系统尚未在权利服务器中注册。可使用 subscription-manager 进行注册。

上次元数据过期检查:3:03:44 前,执行于 2024年08月20日 星期二 15时43分01秒。
软件包 bzip2-1.0.8-8.el9.x86_64 已安装。
依赖关系解决。
=========================================================================================================================================
 软件包                               架构                       版本                                仓库                           大小
=========================================================================================================================================
安装:
 libxml2-devel                        x86_64                     2.9.13-2.el9                        AppStream                     903 k
 sqlite-devel                         x86_64                     3.34.1-5.el9                        AppStream                     137 k
 systemd-devel                        x86_64                     250-12.el9_1                        AppStream                     636 k
安装依赖关系:
 cmake-filesystem                     x86_64                     3.20.2-7.el9                        AppStream                      27 k
 sqlite                               x86_64                     3.34.1-5.el9                        AppStream                     751 k
 xz-devel                             x86_64                     5.2.5-8.el9_0                       AppStream                      59 k

事务概要
=========================================================================================================================================
安装  6 软件包

总计:2.5 M
安装大小:12 M
下载软件包:
运行事务检查
事务检查成功。
运行事务测试
事务测试成功。
运行事务
  准备中  :                                                                                                                          1/1 
  安装    : xz-devel-5.2.5-8.el9_0.x86_64                                                                                            1/6 
  安装    : sqlite-3.34.1-5.el9.x86_64                                                                                               2/6 
  安装    : cmake-filesystem-3.20.2-7.el9.x86_64                                                                                     3/6 
  安装    : libxml2-devel-2.9.13-2.el9.x86_64                                                                                        4/6 
  安装    : sqlite-devel-3.34.1-5.el9.x86_64                                                                                         5/6 
  安装    : systemd-devel-250-12.el9_1.x86_64                                                                                        6/6 
  运行脚本: systemd-devel-250-12.el9_1.x86_64                                                                                        6/6 
  验证    : cmake-filesystem-3.20.2-7.el9.x86_64                                                                                     1/6 
  验证    : libxml2-devel-2.9.13-2.el9.x86_64                                                                                        2/6 
  验证    : sqlite-3.34.1-5.el9.x86_64                                                                                               3/6 
  验证    : sqlite-devel-3.34.1-5.el9.x86_64                                                                                         4/6 
  验证    : systemd-devel-250-12.el9_1.x86_64                                                                                        5/6 
  验证    : xz-devel-5.2.5-8.el9_0.x86_64                                                                                            6/6 
已更新安装的产品。

已安装:
  cmake-filesystem-3.20.2-7.el9.x86_64  libxml2-devel-2.9.13-2.el9.x86_64  sqlite-3.34.1-5.el9.x86_64  sqlite-devel-3.34.1-5.el9.x86_64 
  systemd-devel-250-12.el9_1.x86_64     xz-devel-5.2.5-8.el9_0.x86_64     

完毕!

有些没有的可以到阿里云的镜像站里找

[root@nginx1 ~]# rpm -ivh oniguruma-devel-6.9.6-1.el9.5.x86_64.rpm 


警告:oniguruma-devel-6.9.6-1.el9.5.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 350d275d: NOKEY
Verifying...                          ################################# [100%]
准备中...                          ################################# [100%]
正在升级/安装...
   1:oniguruma-devel-6.9.6-1.el9.5    ################################# [100%]
[root@nginx1 ~]# yum install oniguruma-devel -y


正在更新 Subscription Management 软件仓库。
无法读取客户身份

本系统尚未在权利服务器中注册。可使用 subscription-manager 进行注册。

上次元数据过期检查:0:06:37 前,执行于 2024年08月20日 星期二 18时49分01秒。
软件包 oniguruma-devel-6.9.6-1.el9.5.x86_64 已安装。
依赖关系解决。
无需任何处理。
完毕!
此时编译PHP所需要的环境的捆绑包已经安装好了
 

编译PHP

解压

[root@nginx1 ~]# tar zxf php-8.3.9.tar.gz 
[root@nginx1 ~]# cd php-8.3.9/

[root@nginx1 php-8.3.9]# ./configure --prefix=/usr/local/php \> --with-config-file-path=/usr/local/php/etc \
> --enable-fpm \
> --with-fpm-user=nginx \
> --with-fpm-group=nginx \
> --with-curl \
> --with-iconv \
> --with-mhash \
> --with-zlib \
> --with-openssl \
> --enable-mysqlnd \
> --with-mysqli \
> --with-pdo-mysql \
> --disable-debug \
> --enable-sockets \
> --enable-soap \
> --enable-xml \
> --enable-ftp \
> --enable-gd \
> --enable-exif \
> --enable-mbstring \
> --enable-bcmath \
> --with-fpm-systemd
[root@nginx1 php-8.3.9]# make && make install

配置优化

[root@nginx1 ~]# cd /usr/local/php/etc/
[root@nginx1 etc]# cp php-fpm.conf.default php-fpm.conf
[root@nginx1 etc]# vim php-fpm.conf

去掉注释

[root@nginx1 etc]# cd php-fpm.d
[root@nginx1 php-fpm.d]# cp www.conf.default www.conf
[root@nginx1 php-fpm.d]# cd /root/php-8.3.9/
[root@nginx1 php-8.3.9]# cp php.ini-production /usr/local/php/etc/php.ini
[root@nginx1 php-8.3.9]# vim /usr/local/php/etc/php.ini 

修改时区

[root@nginx1 php-8.3.9]# cp sapi/fpm/php-fpm.service /lib/systemd/system/
[root@nginx1 php-8.3.9]# vim  /lib/systemd/system/php-fpm.service 

注释掉该内容

刷新,并启动php服务

[root@nginx1 php-8.3.9]# systemctl daemon-reload 

[root@nginx1 php-8.3.9]# systemctl start php-fpm.service 

可以查看开启没有

[root@nginx1 php-8.3.9]# netstat -antlupe |grep php


tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      0          151513     158560/php-fpm: mas 

可以添加php的环境变量

[root@nginx1 ~]# vim .bash_profile 

[root@nginx1 ~]# source ~/.bash_profile 

php配置方法

创建文件,并编写

[root@nginx1 ~]# mkdir -p /data/web/php


[root@nginx1 ~]# vim  /data/web/php/index.php

<?
        phpinfo();
?>

在nginx主配置文件里

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

创建文件目录

[root@nginx1 ~]# mkdir -p /usr/local/nginx/conf.d/

编辑配置文件,并重启

[root@nginx1 ~]# vim /usr/local/nginx/conf.d/php.conf

server {
        listen 80;
        server_name www.timinglee.org;
        root /data/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;
        }
}

[root@nginx1 ~]# nginx -s reload

在浏览器测试

php的缓存

安装memcache模块

解压后安装

[root@nginx1 ~]# tar zxf memcache-8.2.tgz 

[root@nginx1 ~]# cd memcache-8.2/

[root@nginx1 memcache-8.2]# yum install autoconf -y


正在更新 Subscription Management 软件仓库。
无法读取客户身份

本系统尚未在权利服务器中注册。可使用 subscription-manager 进行注册。

上次元数据过期检查:1:40:38 前,执行于 2024年08月20日 星期二 18时49分01秒。
依赖关系解决。
==============================================================
 软件包            架构   版本                仓库       大小
==============================================================
安装:
 autoconf          noarch 2.69-38.el9         AppStream 685 k
安装依赖关系:
 m4                x86_64 1.4.19-1.el9        AppStream 304 k
 perl-File-Compare noarch 1.100.600-479.el9   AppStream  24 k
 perl-File-Copy    noarch 2.34-479.el9        AppStream  30 k

事务概要
==============================================================
安装  4 软件包

总计:1.0 M
安装大小:2.8 M
下载软件包:
运行事务检查
事务检查成功。
运行事务测试
事务测试成功。
运行事务
  准备中  :                                               1/1 
  安装    : perl-File-Copy-2.34-479.el9.noarch            1/4 
  安装    : perl-File-Compare-1.100.600-479.el9.noarch    2/4 
  安装    : m4-1.4.19-1.el9.x86_64                        3/4 
  安装    : autoconf-2.69-38.el9.noarch                   4/4 
  运行脚本: autoconf-2.69-38.el9.noarch                   4/4 
  验证    : autoconf-2.69-38.el9.noarch                   1/4 
  验证    : m4-1.4.19-1.el9.x86_64                        2/4 
  验证    : perl-File-Compare-1.100.600-479.el9.noarch    3/4 
  验证    : perl-File-Copy-2.34-479.el9.noarch            4/4 
已更新安装的产品。

已安装:
  autoconf-2.69-38.el9.noarch                                 
  m4-1.4.19-1.el9.x86_64                                      
  perl-File-Compare-1.100.600-479.el9.noarch                  
  perl-File-Copy-2.34-479.el9.noarch                          

完毕!
[root@nginx1 memcache-8.2]# phpize


Configuring for:
PHP Api Version:         20230831
Zend Module Api No:      20230831
Zend Extension Api No:   420230831
[root@nginx1 memcache-8.2]# ./configure && make && make install

加载memcache模块

[root@nginx1 ~]# vim /usr/local/php/etc/php.ini 

部署memcache

[root@nginx1 ~]# dnf install memcached -y


正在更新 Subscription Management 软件仓库。
无法读取客户身份

本系统尚未在权利服务器中注册。可使用 subscription-manager 进行注册。

上次元数据过期检查:1:56:01 前,执行于 2024年08月20日 星期二 18时49分01秒。
依赖关系解决。
==============================================================
 软件包              架构     版本          仓库         大小
==============================================================
安装:
 memcached           x86_64   1.6.9-7.el9   AppStream   178 k
安装依赖关系:
 memcached-selinux   x86_64   1.6.9-7.el9   AppStream    28 k

事务概要
==============================================================
安装  2 软件包

总计:207 k
安装大小:472 k
下载软件包:
运行事务检查
事务检查成功。
运行事务测试
事务测试成功。
运行事务
  准备中  :                                               1/1 
  运行脚本: memcached-selinux-1.6.9-7.el9.x86_64          1/2 
  安装    : memcached-selinux-1.6.9-7.el9.x86_64          1/2 
  运行脚本: memcached-selinux-1.6.9-7.el9.x86_64          1/2 
  运行脚本: memcached-1.6.9-7.el9.x86_64                  2/2 
  安装    : memcached-1.6.9-7.el9.x86_64                  2/2 
  运行脚本: memcached-1.6.9-7.el9.x86_64                  2/2 
  运行脚本: memcached-selinux-1.6.9-7.el9.x86_64          2/2 
  运行脚本: memcached-1.6.9-7.el9.x86_64                  2/2 
  验证    : memcached-1.6.9-7.el9.x86_64                  1/2 
  验证    : memcached-selinux-1.6.9-7.el9.x86_64          2/2 
已更新安装的产品。

已安装:
  memcached-1.6.9-7.el9.x86_64                                
  memcached-selinux-1.6.9-7.el9.x86_64                        

完毕!

启动

[root@nginx1 ~]# systemctl start memcached.service 

查看是否加载了memcache模块

[root@nginx1 ~]# php -m |grep mem
memcache

复制测试文件到nginx的发布目

[root@nginx1 ~]# cd memcache-8.2/

[root@nginx1 memcache-8.2]# cp example.php memcache.php /data/web/php

修改内容

[root@nginx1 memcache-8.2]# vim /data/web/php/memcache.php 

模拟取出数据,不断刷新

此时需要账户和密码

进行压测

PHP高速缓存

编辑配置文件,并重启

[root@nginx1 ~]# vim /usr/local/nginx/conf.d/php.conf 

upstream memcache {
        server 127.0.0.1:11211;
        keepalive 512;
}
server {
        listen 80;
        server_name www.timinglee.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 127.0.0.1:9000;
                fastcgi_index index.php;
                include fastcgi.conf;
        }
}

[root@nginx1 ~]# nginx -s reload


压测

nginx二次版本开发

‌OpenResty‌是一个基于Nginx与Lua的高性能Web平台,它内部集成了大量精良的Lua库、第三方模块以及大多数的依赖项,用于方便地搭建能够处理超高并发、扩展性极高的动态Web应用、Web服务和动态网关。OpenResty的目标是让Web服务直接跑在Nginx服务内部,充分利用Nginx的非阻塞I/O模型,不仅仅对HTTP客户端请求进行高性能响应,甚至于对远程后端如MySQL、PostgreSQL、Memcached以及Redis等也都进行一致的高性能响应

特点

‌高性能‌:利用Nginx的轻量级、高性能、高并发的特性,以及Lua语言的轻量、小巧、可移植、快速的脚本语言特性,实现高性能的Web服务。
‌扩展性‌:通过汇聚各种设计精良的Nginx模块,将Nginx有效地变成一个强大的通用Web应用平台,可以使用Lua脚本语言调动Nginx支持的各种C以及Lua模块,快速构造出足以胜任10K乃至1000K以上单机并发连接的高性能Web应用系统。
‌灵活性‌:OpenResty提供了多个阶段(如init_by_lua_file、init_worker_by_lua_file等)来执行业务逻辑,使得开发者可以根据需要在不同的处理阶段插入Lua代码,实现丰富的功能扩展。

要先停掉原有的nginx,在进行安装

killall -9 nginx

编译安装 openresty

先安装需要的捆绑包,并创建用户

[root@Nginx ~]#dnf -y install gcc pcre-devel openssl-devel perl

[root@Nginx ~]#useradd -r -s /sbin/nologin nginx

通过xftp传输给虚拟机openresty压缩包

解压

[root@nginx1 ~]# tar zxf openresty-1.25.3.1.tar.gz 

编译

[root@nginx1 ~]# cd openresty-1.25.3.1/
[root@nginx1 openresty-1.25.3.1]# ./configure \
> --prefix=/usr/local/openresty \
> --user=nginx --group=nginx \
> --with-http_ssl_module \
> --with-http_v2_module \
> --with_http_realip_module \
> --with-http_stub_status_module \
> --with-pcre --with-stream \
> --with-stream_ssl_module \
> --with-http_gzip_static_module \
> --with-stream_realip_module


[root@Nginx openresty-1.17.8.2]#make && make install

配置环境变量

[root@nginx1 ~]# vim ~/.bash_profile 

[root@nginx1 ~]# source ~/.bash_profile 

启动服务

[root@nginx1 ~]# openresty 

在浏览器访问测试

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值