企业高性能web服务器——Nginx

目录

一、nginx的进程结构

二、nginx的安装

2.1.配置nginx的启动文件

三、核心配置示例

1、新建一个PCweb站点

(1)、在总配置文件中定义子配置文件路径

(2)、新建子配置文件路径并编辑子配置文件

(3)、编辑本地hosts(c:)文件,在浏览器输入ip可以查看到网页内容

(4)、配置访问不同路径的网页内容

(5)、创建软连接,定义路径别名。

2、location 的详细使用

(1)、精确匹配:=  (不能指定目录,只能匹配文件)

(2)、区分大小写:~

(3)、不区分大小写:~*

(4)、匹配uri的前缀:^~

(5)、匹配uri的后缀:

(6)、匹配优先级

3、用户认证

4、自定义错误页面

5、自定义错误日志

6、检测文件是否存在

(1)、编辑配置文件

​编辑 (2)、测试

7、nginx的长链接

8、nginx 做为下载服务器

​编辑

9、nginx的状态页

10、 nginx的压缩功能

(1)、分别生成两个文件,一个小于1K,一个大于1K.

(2)、配置nginx主配置文件

(3)、 查看响应报文头部,可以看出超过1k的文件显示压缩:在传输过程中被压缩,原文件不变

四、nginx的变量使用

4.1、内置变量

4.2、自定义变量

五、nginx rewrite的相关功能

5.1、rewrite模块指令

5.1.1、if指令

5.2、rewrite案例

5.2.1、rewrite案例:break与last

5.2.2、rewrite案例:自动跳转https

​编辑

5.2.3、判断文件是否存在:

5.3、 nginx防盗链

(1)、实现盗链

(2)、实现防盗链

六、nginx的反向代理

6.1、指定location实现反向代理(动静分离)

6.2、反向代理的缓存功能 

七、nginx的负载均衡

 7.1、实验环境

7.2、部署后端的两台server

7.3、基于cookie的会话绑定

 7.4、实现nginx的四层负载均衡

实例1:mysql

实例2:dns

7.5、FastCGI 案例:Nginx和php-fpm在同一服务器

7.5.1、重新安装nginx

7.5.2、安装php

 7.5.3、相关配置优化

7.5.4、生成主配置文件

7.5.5、生成启动文件

7.5.6、准备php测试页面

7.5.7、nginx 配置转发

​编辑7.5.7、 查看

7.6、php的动态动态扩展模块——缓存模块

7.6.1、安装memcache

 7.6.2、部署memcache

7.6.3、查看

7.6.4、测试

7.7、nginx-php——高速缓存

7.7.1、编辑配置文件

7.7.2、测试

7.8、nginx的二次开发版本—— openresty


一、nginx的进程结构

web 请求处理机制
多进程方式:服务器每接收到一个客户端请求就有服务器的主进程生成一个子进程响应客户端,直
到用户关闭连接,这样的优势是处理速度快,子进程之间相互独立,但是如果访问过大会导致服务
器资源耗尽而无法提供请求
多线程方式:与多进程方式类似,但是每收到一个客户端请求会有服务进程派生出一个线程和此客
户端进行交互,一个线程的开销远远小于一个进程,因此多线程方式在很大程度减轻了 web 服务器
对系统资源的要求,但是多线程也有自己的缺点,即当多个线程位于同一个进程内工作的时候,可
以相互访问同样的内存地址空间,所以他们相互影响,一旦主进程挂掉则所有子线程都不能工作
了, IIS服务器使用了多线程的方式,需要间隔一段时间就重启一次才能稳定。Nginx 是多进程组织模型,而且是一个由 Master 主进程和 Worker 工作进程组成。
主进程 (master process) 的功能:
  • 对外接口:接收外部的操作(信号)
  • 对内转发:根据外部的操作的不同,通过信号管理 Worker
  • 监控:监控 worker 进程的运行状态,worker 进程异常终止后,自动重启 worker 进程
  • 读取Nginx 配置文件并验证其有效性和正确性
  • 建立、绑定和关闭socket连接
  • 按照配置生成、管理和结束工作进程
  • 接受外界指令,比如重启、升级及退出服务器等指令
  • 不中断服务,实现平滑升级,重启服务并应用新的配置
  • 开启日志文件,获取文件描述符
  • 不中断服务,实现平滑升级,升级失败进行回滚处理
  • 编译和处理perl脚本

工作进程( worker process )的功能:
  • 所有 Worker 进程都是平等的
  • 实际处理:网络请求,由 Worker 进程处理
  • Worker进程数量:一般设置为核心数,充分利用CPU资源,同时避免进程数量过多,导致进程竞争
  • CPU资源,
  • 增加上下文切换的损耗
  • 接受处理客户的请求
  • 将请求依次送入各个功能模块进行处理
  • I/O调用,获取响应数据
  • 与后端服务器通信,接收后端服务器的处理结果
  • 缓存数据,访问缓存索引,查询和调用缓存数据
  • 发送请求结果,响应客户的请求
  • 接收主程序指令,比如重启、升级和退出等

二、nginx的安装

Nginx 版本
  • Mainline version 主要开发版本,一般为奇数版本号,比如1.19
  • Stable version 当前最新稳定版,一般为偶数版本,:1.20
  • Legacy versions 旧的稳定版,一般为偶数版本,:1.18
  • Nginx安装可以使用yum或源码安装,但是推荐使用源码编译安装
yum 的版本比较旧
  • 编译安装可以更方便自定义相关路径
  • 使用源码编译可以自定义相关功能,更方便业务的上的使用
[root@Nginx ~]# dnf install gcc pcre-devel zlib-devel openssl-devel -y
[root@Nginx nginx-1.24.0]# useradd -s /sbin/nologin -M nginx
[root@Nginx nginx]# tar zxf nginx-1.24.0.tar.gz
[root@Nginx nginx-1.24.0]# useradd -s /sbin/nologin -M nginx
[root@Nginx nginx]# cd nginx-1.24.0/
[root@Nginx nginx-1.24.0]# ls
auto CHANGES.ru configure html Makefile objs src
CHANGES conf contrib LICENSE man README
[root@Nginx nginx-1.24.0]# ./configure --prefix=/usr/local/nginx \
--user=nginx \ # 指定nginx运行用户
--group=nginx \ # 指定nginx运行组
--with-http_ssl_module \ # 支持https://
--with-http_v2_module \ # 支持http版本2
--with-http_realip_module \ # 支持ip透传
--with-http_stub_status_module \ # 支持状态页面
--with-http_gzip_static_module \ # 支持压缩
--with-pcre \ # 支持正则
--with-stream \ # 支持tcp反向代理
--with-stream_ssl_module \ # 支持tcp的ssl加密
--with-stream_realip_module # 支持tcp的透传ip
[root@Nginx nginx-1.24.0]# make && make install

2.1.配置nginx的启动文件

[root@Nginx ~]# vim /lib/systemd/system/nginx.service
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
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@Nginx ~]# systemctl daemon-reload
[root@Nginx ~]# systemctl start nginx

三、核心配置示例

1、新建一个PCweb站点

(1)、在总配置文件中定义子配置文件路径

vim /usr/local/nginx/conf/nginx.conf

(2)、新建子配置文件路径并编辑子配置文件

mkdir /usr/local/nginx/conf.d
vim /usr/local/nginx/conf.d/vhost.conf

#写入网站首页内容
echo www.gaoyingjie.org:hello world! > /data/web/html/index.html

(3)、编辑本地hosts(c:)文件,在浏览器输入ip可以查看到网页内容

(4)、配置访问不同路径的网页内容

vim /usr/local/nginx/conf.d/vhost.conf

#写入该路径的网页内容
echo test1:hello world! > /data/web/test1/index.html

查看:

(5)、创建软连接,定义路径别名。

当用户访问172.25.254.100/test2/时,出现的是test1的网页内容。

2、location 的详细使用

(1)、精确匹配:=  (不能指定目录,只能匹配文件)

vim /usr/local/nginx/conf.d/vhost.conf 
echo web2/test/index.html > /data/web2/test/index.html
nginx -s reload

在浏览器中输入目录+文件测试:

(2)、区分大小写:~

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

[root@nginx ~]# mkdir /data/web3/HTML
[root@nginx ~]# mkdir /data/web3/html

[root@nginx ~]# echo html/HTML > /data/web3/html/index.html
[root@nginx ~]# nginx -s reload

浏览器访问www.gaoyingjie.org/HTML/,则显示访问不到

但是访问www.gaoyingjie.org/html/,则可以访问 

(3)、不区分大小写:~*

[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf 
[root@nginx ~]# echo ~*:html/HTML > /data/web3/html/index.html 

 浏览器访问www.gaoyingjie.org/html/ 时,可以访问到内容:

  浏览器访问www.gaoyingjie.org/HTML/ 时,也可以访问到内容:

(4)、匹配uri的前缀:^~

[root@nginx ~]# mkdir /data/web3/test1

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

浏览器访问www.gaoyingjie.org/test/ ,可以访问到test

 浏览器访问www.gaoyingjie.org/test1/ ,也可以访问到test

(5)、匹配uri的后缀:

[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf 
[root@nginx ~]# mkdir /data/web4/test
[root@nginx ~]# mkdir /data/web5/test
[root@nginx ~]# echo /data/web5/test > /data/web5/test/a.js
[root@nginx ~]# echo /data/web4/test > /data/web4/test/a.js
[root@nginx ~]# nginx -s reload

区分大小写:~ ,不区分大小写:~*

 浏览器输入www.gaoyingjie.org/test/

(6)、匹配优先级

对目录匹配  (~* =  ~) > 不带符号 > ^~ >=     #=不能指定目录所以排在最后
对文件匹配  = > (* = ~) >不带符号 > ^~

3、用户认证

htpasswd -cm /usr/local/nginx/.htpasswd admin
vim /usr/local/nginx/conf.d/vhost.conf 
mkdir /data/web/gyj -p
echo gyj > /data/web/gyj/index.html
nginx -s reload

4、自定义错误页面

5、自定义错误日志

编辑配置文件:

[root@nginx ~]# ll /usr/local/nginx/logs/
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf 
[root@nginx ~]# mkdir /var/log/gaoyingjie.org

[root@nginx ~]#cat /var/log/gaoyingjie.org/access.log 
[root@nginx ~]#cat /var/log/gaoyingjie.org/error.log

6、检测文件是否存在

若不存在则定义到defaul.html页面

(1)、编辑配置文件

vim /usr/local/nginx/conf.d/vhost.conf 
mkdir /data/web/html/error
echo error default > /data/web/html/error/default.html
curl www.gaoyingjie.org/123.html  #不存在123.html文件

 (2)、测试

7、nginx的长链接

下载测试工具:

yum install telnet -y

8、nginx 做为下载服务器

新建下载目录:

 参数介绍:

autoindex on | off;   # 自动文件索引功能,默为 off
autoindex_exact_size on | off; 
 # 计算文件确切大小(单位 bytes ), off 显示大概大小(单位 K 、 M),默认 on
autoindex_localtime on | off ; # 显示本机时间而非 GMT( 格林威治 ) 时间,默认 off
autoindex_format html | xml | json | jsonp; # 显示索引的页面文件风格,默认 html
limit_rate rate;
# 限制响应客户端传输速率 ( GET HEAD 以外的所有方法 ),单位b/s,bytes/second # 默认值 0, 表示无限制 , 此指令由

输入www.gaoyingjie.org/download进去下载页

9、nginx的状态页

10、 nginx的压缩功能

Nginx 支持对指定类型的文件进行压缩然后再传输给客户端,而且压缩还可以设置压缩比例,压缩后的文
件大小将比源文件显著变小,样有助于降低出口带宽的利用率,降低企业的 IT 支出,不过会占用相
应的 CPU 资源。
参数介绍:
# 启用或禁用 gzip 压缩,默认关闭
gzip on | off;
# 压缩比由低到高从 1 9 ,默认为 1 ,值越高压缩后文件越小,但是消耗 cpu 比较高。基本设定未 4 或者 5
gzip_comp_level 4;
# 禁用 IE6 gzip 功能,早期的 IE6 之前的版本不支持压缩
gzip_disable "MSIE [1-6]\.";
#gzip 压缩的最小文件,小于设置值的文件将不会压缩
gzip_min_length 1k;
# 启用压缩功能时,协议的最小版本,默认 HTTP/1.1
gzip_http_version 1.0 | 1.1;
# 指定 Nginx 服务需要向服务器申请的缓存空间的个数和大小 , 平台不同 , 默认 :32 4k 或者 16 8k;
gzip_buffers number size;
# 指明仅对哪些类型的资源执行压缩操作 ; 默认为 gzip_types text/html ,不用显示指定,否则出错
gzip_types mime-type ...;
# 如果启用压缩,是否在响应报文首部插入 “Vary: Accept-Encoding”, 一般建议打开
gzip_vary on | off;
# 预压缩,即直接从磁盘找到对应文件的 gz 后缀的式的压缩文件返回给用户,无需消耗服务器 CPU ,注意: 来自于 ngx_http_gzip_static_module 模块
gzip_static on | off;

(1)、分别生成两个文件,一个小于1K,一个大于1K.

(2)、配置nginx主配置文件

(3)、 查看响应报文头部,可以看出超过1k的文件显示压缩:在传输过程中被压缩,原文件不变

四、nginx的变量使用

添加echo模块

上传echo模块的压缩包,查看已安装好的配置文件和模块:

nginx -V

进入nginx安装目录:

cd nginx-1.24.0/

重新编译模块:

[root@nginx ~]# nginx -V
nginx version: nginx/1.24.0
built by gcc 11.4.1 20230605 (Red Hat 11.4.1-2) (GCC) 
built with OpenSSL 3.0.7 1 Nov 2022
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --user=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_realip_module 


#将--add-module=/root/echo-nginx-module-0.63添加到已编译的模块后面重新编译
[root@nginx nginx-1.24.0]# ./configure --prefix=/usr/local/nginx --user=nginx --user=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_realip_module --add-module=/root/echo-nginx-module-0.63

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

4.1、内置变量

编辑子配置文件:

[root@php ~]# vim /usr/local/nginx/conf.d/vhosts.conf
[root@php ~]# mkdir /data/web/html/var
[root@php ~]# echo /data/web/html/var > /data/web/html/var/index.html

测试:

[root@php ~]# curl www.gaoyingjie.org/var?name=gyj&&id=6666

4.2、自定义变量

假如需要自定义变量名称和值,使用指令 set $variable value;
指定 key 并给其定义一个变量,变量可以调用 Nginx 内置变量赋值给 key ,另外set 定义格式为 set $key value value 可以是 text, variables 和两者的组合。

编辑子配置文件:

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

测试:

[root@php ~]# curl www.gaoyingjie.org/var

五、nginx rewrite的相关功能

5.1、rewrite模块指令

5.1.1、if指令

= # 比较变量和字符串是否相等,相等时 if 指令认为该条件为 true ,反之为 false
!= # 比较变量和字符串是否不相等,不相等时 if 指令认为条件为 true ,反之为 false
~ # 区分大小写字符,可以通过正则表达式匹配,满足匹配条件为真,不满足匹配条件为假
!~ # 区分大小写字符 , 判断是否匹配,不满足匹配条件为真,满足匹配条件为假
~* # 不区分大小写字符,可以通过正则表达式匹配,满足匹配条件为真,不满足匹配条件为假
!~* # 不区分大小字符 , 判断是否匹配,满足匹配条件为假,不满足匹配条件为真
-f !-f # 判断请求的文件是否存在和是否不存在
-d !-d # 判断请求的目录是否存在和是否不存在
-x !-x # 判断文件是否可执行和是否不可执行
-e !-e # 判断请求的文件或目录是否存在和是否不存在 ( 包括文件,目录,软链接 )
# 注意:
# 如果 $ 变量的值为空字符串或 0 ,则 if 指令认为该条件为 false ,其他条件为 true
#nginx 1.0.1 之前 $ 变量的值如果以 0 开头的任意字符串会返回 false

编辑配置文件:

[root@php ~]# vim /usr/local/nginx/conf.d/vhosts.conf
[root@php ~]# nginx -s reload

测试:

[root@php ~]# mkdir /data/web/html/{test1,test2}
[root@php ~]# echo  /data/web/html/test1 > /data/web/html/test1/index.html
[root@php ~]# echo  /data/web/html/test2 > /data/web/html/test2/index.html

5.2、rewrite案例

rewirte正则表达式格式:
. # 匹配除换行符以外的任意字符
\w # 匹配字母或数字或下划线或汉字
\s # 匹配任意的空白符
\d # 匹配数字
\b # 匹配单词的开始或结束
^ # 匹配字付串的开始
$ # 匹配字符串的结束
* # 匹配重复零次或更多次
+ # 匹配重复一次或更多次
? # 匹配重复零次或一次
(n) # 匹配重复 n
{n,} # 匹配重复 n 次或更多次
{n,m} # 匹配重复 n m
*? # 匹配重复任意次,但尽可能少重复
+? # 匹配重复 1 次或更多次,但尽可能少重复
?? # 匹配重复 0 次或 1 次,但尽可能少重复
{n,m}? # 匹配重复 n m 次,但尽可能少重复
{n,}? # 匹配重复 n 次以上,但尽可能少重复
\W # 匹配任意不是字母,数字,下划线,汉字的字符
\S # 匹配任意不是空白符的字符
\D # 匹配任意非数字的字符
\B # 匹配不是单词开头或结束的位置
[^x] # 匹配除了 x 以外的任意字符
[^lee] # 匹配除了 magedu 这几个字母以外的任意字符

5.2.1、rewrite案例:break与last

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

编辑配置文件:

[root@php ~]# vim /usr/local/nginx/conf.d/vhosts.conf
[root@php ~]# nginx -s reload

 测试:

[root@php ~]# mkdir /data/web/html/break
[root@php ~]# echo /data/web/html/break > /data/web/html/break/index.html
[root@php ~]# mkdir /data/web/html/last
[root@php ~]# echo /data/web/html/last > /data/web/html/last/index.html

5.2.2、rewrite案例:自动跳转https

5.2.3、判断文件是否存在:

5.3、 nginx防盗链

(1)、实现盗链

准备一个web服务器(172.25.254.10),写入网站内容,在该站点盗取另一个(172.25.254.100)的图片资源。

1、网站html文件内容:
<html>

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

  <body>
    <img src="http://www.gaoyingjie.org/images/1.png" >
    <h1 style="color:red">欢迎大家</h1>
    <p><a href=http://www.gaoyingjie.org</a>出门见喜</p>
  </body>

</html>

2、在172.25.254.100的服务器的images文件夹内上传图片1.png。

浏览器输入:http://172.25.254.10可以看到该站点自动连接到100服务器的图片

(2)、实现防盗链

配置文件:

在100服务器http的发布目录内上传盗链图片,意味着当其他站点盗链1.png时,会自动替换成盗链图片。

 重启nginx后,再次访问:http://172.25.254.10,就会出现盗链图片。

六、nginx的反向代理

6.1、指定location实现反向代理(动静分离)

参数介绍:

  • proxy_pass;
# 用来设置将客户端请求转发给的后端服务器的主机
# 可以是主机名 ( 将转发至后端服务做为主机头首部 ) IP 地址:端口的方式
# 也可以代理到预先设置的主机群组,需要模块 ngx_http_upstream_module 支持
  • proxy_pass_header field;
# 透传
# 默认 nginx 在响应报文中不传递后端服务器的首部字段 Date, Server, X-Pad, X-Accel 等参数
# 如果要传递的话则要使用 proxy_pass_header field 声明将后端服务器返回的值传递给客户端
#field 首部字段大小不敏感
# 示例 : 透传后端服务器的 Server Date 首部给客户端 , 同时不再响应报中显示前端服务器的 Server 字段
proxy_pass_header Server;
proxy_pass_header Date;
  • proxy_pass_request_body on | off;
# 是否向后端服务器发送 HTTP 实体部分 , 可以设置在 http,server location 块,默认即为开启
  • proxy_pass_request_headers on | off;
# 是否将客户端的请求头部转发给后端服务器,可以设置在 http,server location 块,默认即为开启

  • proxy_set_header;
# 可更改或添加客户端的请求头部信息内容并转发至后端服务器,比如在后端服务器想要获取客户端的真实 IP
时候,就要更改每一个报文的头部
  • proxy_connect_timeout time;
# 配置 nginx 服务器与后端服务器尝试建立连接的超时时间,默认为 60
用法如下: proxy_connect_timeout 6s;
#60s 为自定义 nginx 与后端服务器建立连接的超时时间 , 超时会返回客户端 504 响应码
  • proxy_read_timeout time;
# 配置 nginx 服务器向后端服务器或服务器组发起 read 请求后,等待的超时时间,默认 60s
  • proxy_send_timeout time;
# 配置 nginx 项后端服务器或服务器组发起 write 请求后,等待的超时 时间,默认 60s
  • proxy_http_version 1.0;
# 用于设置 nginx 提供代理服务的 HTTP 协议的版本,默认 http 1.0
  • proxy_ignore_client_abort off;
# 当客户端网络中断请求时, nginx 服务器中断其对后端服务器的请求。即如果此项设置为 on 开启,则服务器、
会忽略客户端中断并一直等着代理服务执行返回,如果设置为 off ,则客户端中断后 Nginx 也会中断客户端请求
并立即记录 499 日志,默认为 off

准备两台webserver,172.25.254.10、172.25.254.20,在10上安装php,并写入内容。

vim/var/www/index.php

在20上写入网站首页内容并修改端口为8080:

echo static:172.25.254.20 > /var/www/html/static/index.html

vim /etc/httpd/conf/httpd.conf

 在100上nginx配置文件:

测试:

当访问www.gaoyingjie.org/static/时:

当访问www.gapyingjie.org/index.php时:

6.2、反向代理的缓存功能 

缓存配置:在nginx的总配置文件http块内

子配置文件:

 可以看到目录自动生成:

检测:验证缓存目录结构及文件大小

七、nginx的负载均衡

 7.1、实验环境

主机IP
nginx172.25.254.100
apache(server)172.25.254.10
apache1(server)172.25.254.20

7.2、部署后端的两台server

#server1
[root@apache20 ~]# yum install httpd -y
[root@apache20 ~]# echo "web1 172.25.254.10" > /var/www/html/index.html
[root@apache20 ~]# systemctl enable --now httpd
#server2
[root@apache30 ~]# yum install httpd -y
[root@apache30 ~]# echo "web2 172.25.254.20" >> /var/www/html/index.html
[root@apache30 ~]# systemctl enable --now httpd

#访问测试
[root@centos8 ~]# curl http://172.25.254.20
web1 172.25.254.10
[root@centos8 ~]# curl http://172.25.254.30
web2 172.25.254.20

7.3、基于cookie的会话绑定

配置文件:

 检测:基于cookie值的变化而访问不同的服务器,cookie值相同则访问相同服务器,实现会话绑定

 7.4、实现nginx的四层负载均衡

  • 实例1:mysql

在后端两台server上安装数据库并更改配置文件:

[root@apache20 ~]# yum install mariadb-server -y
[root@apache20 ~]# vim /etc/my.cnf.d/mariadb-server.cnf
#server1
[root@server1 ~]# mysql -e "grant all on *.* to gyj@'%' identified by 'gyj';"
 
#server2
[root@server2 ~]#  mysql -e "grant all on *.* to gyj@'%' identified by 'gyj';"
 

在两台server中开启mysql,查看server_id:

mysql -ugyj -pgyj -h 172.25.254.10 -e "select @@server_id"

配置nginx:

vim /usr/local/nginx/conf.d/tcp.conf 

因为该配置文件tcp.conf为四层代理,故在总配置文件的http块之外加上子配置文件路径:

vim /usr/local/nginx/conf/nginx.conf

在100主机上下载mariadb客户端:

dnf install mariadb -y

重启nginx并访问测试:

mysql -u gyj -pgyj -h 172.25.254.100

一次登录为server_id为10:

 二次登录为20:

  • 实例2:dns

在后端两台server下载dns,并且修改配置文件:

vim /etc/named.conf
vim /etc/named.rfc1912.zones 

 配置named.gaoyingjie.org文件:

cd /var/named/
cp named.localhost named.gaoyingjie.org -p
vim named.gaoyingjie.org

将这台10server的配置文件远程复制到另一台server20:

 scp -p /etc/nama.{conf,rfc1912.zones} root@172.25.254.20:/etc/

配置20的named.gaoyingjie.org文件

[root@apache1 named]# cp named.localhost named.gaoyingjie.org
[root@apache1 named]# vim named.gaoyingjie.org 

修改nginx的配置文件:

vim /usr/local/nginx/conf.d/tcp.conf 

启动两台server的dns,并且查看是否解析成功:

dig www.gaoyingjie.org 172.25.254.10
dig www.gaoyingjie.org 172.25.254.20

7.5、FastCGI 案例:Nginx和php-fpm在同一服务器

7.5.1、重新安装nginx

上传nginx和模块的压缩包并解压

[root@php nginx-1.26.1]# cd ~
[root@php ~]# ls
公共  音乐                           nginx-1.26.1
模板  桌面                           nginx-1.26.1.tar.gz
视频  anaconda-ks.cfg                openresty-1.25.3.1.tar.gz
图片  echo-nginx-module-0.63.tar.gz  php-8.3.9.tar.gz
文档  memcache-8.2.tgz               srcache-nginx-module-0.33.tar.gz
下载  memc-nginx-module-0.20.tar.gz
[root@php ~]# tar zxf echo-nginx-module-0.63.tar.gz 
[root@php ~]# tar zxf memc-nginx-module-0.20.tar.gz 
[root@php ~]# tar zxf srcache-nginx-module-0.33.tar.gz 
[root@php ~]# tar zxf nginx-1.26.1.tar.gz

编译:

[root@php ~]# cd nginx-1.26.1/
[root@php nginx-1.26.1]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_realip_module --add-module=/root/echo-nginx-module-0.63 --add-module=/root/memc-nginx-module-0.20 --add-module=/root/srcache-nginx-module-0.33

下载:

[root@php nginx-1.26.1]# make && makeinstall

验证版本及编译参数:

[root@php nginx-1.26.1]# vim ~/.bash_profile 
[root@php nginx-1.26.1]# source ~/.bash_profile 
[root@php nginx-1.26.1]# nginx -V
nginx version: nginx/1.26.1
built by gcc 11.4.1 20230605 (Red Hat 11.4.1-2) (GCC) 
built with OpenSSL 3.0.7 1 Nov 2022
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_realip_module --add-module=/root/echo-nginx-module-0.63 --add-module=/root/memc-nginx-module-0.20 --add-module=/root/srcache-nginx-module-0.33

编辑自启动文件:先不启动nginx

[root@php nginx-1.26.1]# vim /lib/systemd/system/nginx.service
[root@php nginx-1.26.1]# systemctl daemon-reload 

7.5.2、安装php

上传php并进行解压:

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

下载依赖:

[root@php php-8.3.9]# yum install -y bzip2 systemd-devel libxml2-devel sqlite-devel
libpng-devel libcurl-devel oniguruma-devel
[root@php php-8.3.9]# dnf install libcurl-devel-7.76.1-26.el9.x86_64 -y
[root@php php-8.3.9]# dnf install libpng-devel-2:1.6.37-12.el9.x86_64 -y

[root@php php-8.3.9]# wget https://mirrors.aliyun.com/rockylinux/9.4/devel/x86_64/kickstart/Packages/o/oniguruma-devel-6.9.6-1.el9.5.x86_64.rpm
[root@php php-8.3.9]# rpm -ivh oniguruma-devel-6.9.6-1.el9.5.x86_64.rpm

编译:

[root@php 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@php php-8.3.9]# make && make install

 7.5.3、相关配置优化

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

[root@php php-fpm.d]# cp www.conf.default www.conf -p
[root@php php-fpm.d]# vim www.conf

7.5.4、生成主配置文件

[root@php ~]# cd /root/php-8.3.9/
[root@php php-8.3.9]# cp php.ini-production /usr/local/php/etc/php.ini
#修改主配置文件的时区
[root@php php-8.3.9]# vim /usr/local/php/etc/php.ini 

7.5.5、生成启动文件

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

重启查看:

[root@php php-8.3.9]# systemctl start php-fpm.service 
[root@php php-8.3.9]# netstat -antlupe | grep php

7.5.6、准备php测试页面

[root@php conf]# mkdir /data/web/php -p
[root@php conf]# vim /data/web/php/index.php

7.5.7、nginx 配置转发

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

在nginx的总配置文件内加入子配置文件路径:

[root@php conf]# vim /usr/local/nginx/conf/nginx.conf
[root@php conf]# systemctl start nginx.service 
[root@php conf]# nginx -s reload

7.5.7、 查看

浏览器输入www.gaoyingjie.org/index.php查看:

  添加php的环境变量:

[root@php conf]# vim ~/.bash_profile
[root@php conf]# source ~/.bash_profile 

7.6、php的动态动态扩展模块——缓存模块

7.6.1、安装memcache

上传并解压:

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

 下载依赖:

[root@php memcache-8.2]# yum install autoconf -y
[root@php memcache-8.2]# phpize
Configuring for:
PHP Api Version:         20230831
Zend Module Api No:      20230831
Zend Extension Api No:   420230831

 编译:

[root@php memcache-8.2]# ls
autom4te.cache  config.h.in  configure.ac  docker       LICENSE       run-tests.php
build           config.m4    config.w32    Dockerfile   memcache.php  src
config9.m4      configure    CREDITS       example.php  README        tests
[root@php memcache-8.2]# ./configure && make && make install

 查看是否安装成功:

[root@php memcache-8.2]# ls /usr/local/php/lib/php/extensions/no-debug-non-zts-20230831/
memcache.so  opcache.so
[root@php ~]# php -m | grep mem

 复制测试文件到nginx首页目录中,并配置文件:

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

配置php加载memcache模块:

[root@php ~]# vim /usr/local/php/etc/php.ini 
[root@php ~]# systemctl reload php-fpm.service 

 7.6.2、部署memcache

[root@php ~]# yum install memcached -y
[root@php ~]# systemctl start memcached.service 
Created symlink /etc/systemd/system/multi-user.target.wants/memcached.service → /usr/lib/systemd/system/memcached.service.

[root@php ~]#  netstat -antlupe | grep memcache
[root@php ~]# cat /etc/sysconfig/memcached 

7.6.3、查看

浏览器输入www.gaoyingjie.org/memcache.php

浏览器输入www.gaoyingjie.org/example.php

7.6.4、测试

ab -n500 -c10 http://www.gaoyingjie.org/index.php
ab -n500 -c10 http://www.gaoyingjie.org/example.php

7.7、nginx-php——高速缓存

nginx 中默认不支持 memc srcache 功能,需要借助第三方模块来让 nginx 支持此功能,所以需要重新编译有( -- add-module=/root/memc-nginx-module-0.20  --add-module=/root/srcache-nginx-module- 0.33 )模块的nginx 。
让nginx直接从memcahce取,让memcache前置。

7.7.1、编辑配置文件

7.7.2、测试

7.8、nginx的二次开发版本—— openresty

上传并解压openresty

tar zxf openresty-1.25.3.1.tar.gz

编译:

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

下载并拷贝:

[root@nginx openresty-1.25.3.1]# make && make install
[root@nginx openresty-1.25.3.1]# ln -s /apps/openresty/bin/* /usr/bin/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值