万字详解高性能web服务器nginx

一、apache经典的web服务端

1、apache prefork模型

一个进程对应一个线程

2、apache worker模型

一个多子进程可以对应多个线程

3、apache event模型

多了一个监听线程,可以将真实请求传递给服务线程,执行完有允许它释放,增强了高并发场景下的请求处理能力。

二、Nginx-高性能的web服务端

1、基于nginx工作场景

2、服务端I/O流程

网络I/O

3、I/O模型

(1)同步异步:消息通信机制

(2)阻塞/非阻塞:调用者在等待结果返回之前所处的状态

4、网络I/O模型

(1)同步阻塞型

(2)同步非阻塞型

(3)信号驱动式

(4)多路复用

(5)异步非阻塞

5、五种IO对比

6、I/O 的具体实现方式

Nginx支持在多种不同的操作系统实现不同的事件驱动模型,但是其在不同的操作系统甚至是不同的系统 版本上面的实现方式不尽相同,主要有以下实现方式:

select:apache,列表有限制

poll:列表没限制

epoll:nginx,列表中只记录已经好了的请求

比较图

7、零拷贝解决传统IO问题

相关技术有:

MMAP

SENDFILE

DMA辅助的SENDFILE

三、nginx架构和安装

1、进程架构

2、进程之间的通信

3、nginx模块

4、nginx的编译安装

官方源码包下载地址: https://nginx.org/en/download.html

先安装依赖环境

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

创建一个名为nginx的用户,并将其shell设置为/sbin/nologin,同时不创建主目录

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

解压
[root@nginx ~]# tar zxf nginx-1.24.0.tar.gz
[root@nginx ~]# cd nginx-1.24.0/
[root@nginx nginx-1.24.0]# ls
auto     CHANGES.ru  configure  html     man     src
CHANGES  conf        contrib    LICENSE  README


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

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

[root@nginx nginx-1.24.0]# vim ~/.bash_profile

export PATH=$PATH:/usr/local/nginx/sbin

[root@nginx nginx-1.24.0]# source ~/.bash_profile
[root@nginx nginx-1.24.0]# 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-ht  tp_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_stat  us_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ss  l_module --with-stream_realip_module

设定Nginx启动文件

[root@nginx nginx-1.24.0]#  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

然后就可以使用以下命令启动nginx服务
[root@nginx nginx-1.24.0]# systemctl start nginx

再查看以下进程
[root@nginx nginx-1.24.0]# ps aux | grep nginx
root       56017  0.0  0.0   9836   928 ?        Ss   11:42   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      56018  0.0  0.1  13724  4804 ?        S    11:42   0:00 nginx: worker process
root       57776  0.0  0.0 221664  2232 pts/1    S+   11:47   0:00 grep --color=auto nginx

5、平滑升级

[root@nginx ~]# wget https://nginx.org/download/nginx-1.26.2.tar.gz

[root@nginx ~]# ls
anaconda-ks.cfg  Documents  Music         nginx-1.24.0.tar.gz  Pictures  Templates
Desktop          Downloads  nginx-1.24.0  nginx-1.26.2.tar.gz  Public    Videos
[root@nginx ~]# tar zxf nginx-1.26.2.tar.gz
[root@nginx ~]# cd nginx-1.26.2/

开始编译新版本
[root@nginx nginx-1.26.2]# ./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@nginx nginx-1.26.2]# make

查看两个版本

[root@nginx nginx-1.26.2]# ll  objs/nginx /usr/local/nginx/sbin/nginx
-rwxr-xr-x 1 root root 5753736 Aug 15 11:56 objs/nginx
-rwxr-xr-x 1 root root 5679504 Aug 15 11:08 /usr/local/nginx/sbin/nginx

备份旧版nginx命令
[root@nginx nginx-1.26.2]# cd /usr/local/nginx/sbin/
[root@nginx sbin]# cp nginx nginx.24

复制新版nginx命令过去

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

检测一下

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

查看一下旧版本的进程id
[root@nginx sbin]# ps aux | grep nginx
root       56017  0.0  0.0   9836   928 ?        Ss   11:42   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      56018  0.0  0.1  13724  4804 ?        S    11:42   0:00 nginx: worker process
root       67547  0.0  0.0 221664  2280 pts/1    S+   11:58   0:00 grep --color=auto nginx

向master进程发送USR2信号
[root@nginx sbin]# kill -USR2 56017

就可以看到新版本进程已经起来
[root@nginx sbin]# ps aux | grep nginx
root       56017  0.0  0.0   9836  2548 ?        Ss   11:42   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      56018  0.0  0.1  13724  4804 ?        S    11:42   0:00 nginx: worker process
root       67914  0.0  0.1   9840  6076 ?        S    11:59   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      67915  0.0  0.1  13728  4712 ?        S    11:59   0:00 nginx: worker process
root       67941  0.0  0.0 221664  2288 pts/1    S+   12:00   0:00 grep --color=auto nginx

查看一下nginx版本信息
[root@nginx sbin]# curl -I localhost
HTTP/1.1 200 OK
Server: nginx/1.26.2
Date: Thu, 15 Aug 2024 04:00:25 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Thu, 15 Aug 2024 03:08:59 GMT
Connection: keep-alive
ETag: "66bd714b-267"
Accept-Ranges: bytes

回收旧版本

[root@nginx sbin]# kill -WINCH 56017

查看进程旧版本的工作进程已经停止了
[root@nginx sbin]# ps aux | grep nginx
root       56017  0.0  0.0   9836  2548 ?        Ss   11:42   0:00 nginx: master process /usr/local/nginx/sbin/nginx
root       67914  0.0  0.1   9840  6076 ?        S    11:59   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      67915  0.0  0.1  13728  5252 ?        S    11:59   0:00 nginx: worker process
root       68199  0.0  0.0 221664  2280 pts/1    S+   12:00   0:00 grep --color=auto nginx

6、回滚

如果升级的版本发现问题需要回滚,可以重新拉起旧版本的worker

备份新版本的nginx命令

[root@nginx sbin]# cp nginx nginx.26.2
[root@nginx sbin]# ls
nginx  nginx.24  nginx.26.2
[root@nginx sbin]# mv nginx.24 nginx
mv: overwrite 'nginx'? y


[root@nginx sbin]# kill -HUP 56017
[root@nginx sbin]# ps aux | grep nginx
root       56017  0.0  0.0   9836  2548 ?        Ss   11:42   0:00 nginx: master process /usr/local/nginx/sbin/nginx
root       67914  0.0  0.1   9840  6076 ?        S    11:59   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      67915  0.0  0.1  13728  5252 ?        S    11:59   0:00 nginx: worker process
nginx      72961  0.0  0.1  13724  4804 ?        S    12:14   0:00 nginx: worker process
root       73077  0.0  0.0 221664  2228 pts/1    S+   12:14   0:00 grep --color=auto nginx

[root@nginx sbin]# kill -WINCH 67914
[root@nginx sbin]# ps aux | grep nginx
root       56017  0.0  0.0   9836  2548 ?        Ss   11:42   0:00 nginx: master process /usr/local/nginx/sbin/nginx
root       67914  0.0  0.1   9840  6076 ?        S    11:59   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      72961  0.0  0.1  13724  4804 ?        S    12:14   0:00 nginx: worker process
root       73471  0.0  0.0 221664  2288 pts/1    S+   12:15   0:00 grep --color=auto nginx
[root@nginx sbin]# curl -I localhost
HTTP/1.1 200 OK
Server: nginx/1.24.0
Date: Thu, 15 Aug 2024 04:15:52 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Thu, 15 Aug 2024 03:08:59 GMT
Connection: keep-alive
ETag: "66bd714b-267"
Accept-Ranges: bytes

四、Nginx核心配置

1、全局配置cpu的核心绑定

编辑主配置文件

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

查看进程就可以看到两个子进程

2、HTTP配置块

nginx配置中的root和alias

先在主配置文件中添加参数

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

创建一个子配置目录,在子配置文件中添加server语句块

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

创建目录

[root@nginx ~]# mkdir /data/web/test1 -p
[root@nginx ~]# echo /data/web/test1 > /data/web/test1/index.html
[root@nginx ~]# mkdir /data/web/html -p
[root@nginx ~]# echo hhh > /data/web/html/index.html
[root@nginx ~]# nginx -s reload
测试:

3、location的使用

编辑子配置文件vhost.conf,添加多条location语句块独立不同的访问目录条件。

测试访问目录时其优先值:

将4和5的位置调换

可以看到4和5的优先级相同只是两个位置不一样就会先访问前面的,下面将最后两个location注释掉测其他的优先级。

可以看到访问目录的优先级:(~,~*)> 不带符号 >  ^~ > 精确匹配

因为=是只能精确文件不能精确目录的哟

接下来我们改成访问文件再进行优先级测试:

可以看到访问文件的优先级:

精确匹配  >  (~,~*)  >  不带符号  >  ^~

4、Nginx 账户认证功能

先创建用户认证文件

[root@nginx web]# htpasswd -cm /usr/local/nginx/.htpasswd admin
New password:
Re-type new password:
Adding password for user admin
[root@nginx web]# cat /usr/local/nginx/.htpasswd
admin:$apr1$T9YZi8os$uzTzzHeyMLCRfEXDe9vNU/
[root@nginx web]# htpasswd -m /usr/local/nginx/.htpasswd jcl
New password:
Re-type new password:
Adding password for user jcl
[root@nginx web]# cat /usr/local/nginx/.htpasswd
admin:$apr1$T9YZi8os$uzTzzHeyMLCRfEXDe9vNU/
jcl:$apr1$Aq5R3XTt$6A7Xdh1l0pZjzMeF6hyoQ/
 

然后编辑子配置文件添加用户认证的参数

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

效果测试:

访问成功

5、自定义错误页面

编辑子配置文件添加错误页面参数,表示404状态码去访问40x.html文件。

创建文件

[root@nginx web]# mkdir -p /data/web/errorpage

[root@nginx web]# echo error page Qinchenxi is BigBadegg > /data/web/errorpage/40x.html

测试访问一个没有创建的目录

6、自定义错误日志

编辑子配置文件添加自定义错误日志的参数和访问成功日志的参数,定义错误和访问日志路径

先创建自定义好的存放日志的目录

[root@nginx jcl.org]# mkdir /var/log/jcl.org

加载服务会自动生成配置文件自定义好的日志文件
[root@nginx jcl.org]# nginx -s reload

切换到目录里面可以看到自动生成两个文件
[root@nginx web]# cd /var/log/jcl.org/
[root@nginx jcl.org]# ll
total 8
-rw-r--r-- 1 root root 357 Aug 16 14:33 access.log
-rw-r--r-- 1 root root 209 Aug 16 14:29 error.log
测试访问两次,一次访问错误一次访问正确,看日志:

7、检测index文件是否存在

[root@nginx jcl.org]# mkdir /data/web/html/error -p
[root@nginx jcl.org]# echo "index.html is not exit" > /data/web/html/error/default.html

8、长链接设置

(1)请求次数参数
[root@nginx jcl.org]# vim /usr/local/nginx/conf/nginx.conf

安装测试工具

[root@nginx ~]# dnf install telnet -y

访问两次自动退出

(2)请求时间参数

该配置文件中的65表示最大维持会话的时间,60表示客户访问的最大维持会话时间

如果设置为5,表示会话连接5秒后自动断开

重启完后再访问就可以看到timeout时间

9、作为下载服务器配置

[root@nginx jcl.org]# mkdir /data/web/download
[root@nginx jcl.org]# dd if=/dev/zero of=/data/web/download/jclfile bs=1M count=100
100+0 records in
100+0 records out
104857600 bytes (105 MB, 100 MiB) copied, 0.047698 s, 2.2 GB/s


[root@nginx jcl.org]# vim /usr/local/nginx/conf.d/vhost.conf
[root@nginx jcl.org]# nginx -s reload

浏览器访问download目录

查看文件下载速度

五、Nginx的高级配置

1、nginx状态页

[root@nginx jcl.org]# cd /usr/local/nginx/conf.d/
[root@nginx conf.d]# vim status.conf

编辑windows中的hosts文件

2、nginx的压缩功能

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

重启服务 

[root@nginx conf.d]# nginx -s reload

打印内容在指定小文件
[root@nginx conf.d]# echo hello jcl > /data/web/html/small.html

设置大于1K的文件在指定的目录中
[root@nginx conf.d]# cat /usr/local/nginx/logs/access.log  > /data/web/html/big.html

查看一下
[root@nginx conf.d]# du -sh /usr/local/nginx/logs/access.log
12M     /usr/local/nginx/logs/access.log

测试

[root@nginx conf.d]# curl --head --compressed 172.25.254.100/small.html
HTTP/1.1 200 OK
Server: nginx/1.26.2
Date: Fri, 16 Aug 2024 08:19:53 GMT
Content-Type: text/html
Content-Length: 10
Last-Modified: Fri, 16 Aug 2024 08:15:34 GMT
Connection: keep-alive
Keep-Alive: timeout=60
ETag: "66bf0aa6-a"
Accept-Ranges: bytes

[root@nginx conf.d]# curl --head --compressed 172.25.254.100/big.html
HTTP/1.1 200 OK
Server: nginx/1.26.2
Date: Fri, 16 Aug 2024 08:20:07 GMT
Content-Type: text/html
Last-Modified: Fri, 16 Aug 2024 08:16:27 GMT
Connection: keep-alive
Keep-Alive: timeout=60
ETag: W/"66bf0adb-b06751"
Content-Encoding: gzip

3、nginx中的变量

编辑自己创建的子配置文件var.conf

通过curl展示一下变量的具体内容

4、nginxrewrite相关功能

rewrite是nginx服务器的重要功能之一,用于实现URL的重写,URL的重写是非常有用的功能 比如它可以在我们改变网站结构之后,不需要客户端修改原来的书签,也无需其他网站修改我们的 链接,就可以设置为访问,另外还可以提高访问的安全性

4.1if指令

用于条件判断结果选择不同的nginx配置,可以配置在server或location块中进行配置,Nginx的if语法仅能使用if做单次判断,不支持使用if else或者if elif这样的多重判断

使用正则表达式对变量进行匹配,匹配成功时if指令认为条件为true,否则认为false,变量与表达式之间 使用以下符号链接:

=#比较变量和字符串是否相等,相等时if指令认为该条件为true,反之为false
!=#比较变量和字符串是否不相等,不相等时if指令认为条件为true,反之为false
~#区分大小写字符,可以通过正则表达式匹配,满足匹配条件为真,不满足匹配条件为假
!~#区分大小写字符,判断是否匹配,不满足匹配条件为真,满足匹配条件为假
~*#不区分大小写字符,可以通过正则表达式匹配,满足匹配条件为真,不满足匹配条件为假
!~*#不区分大小字符,判断是否匹配,满足匹配条件为假,不满足匹配条件为真
-f 和 !-f#判断请求的文件是否存在和是否不存在
-d和!-d#判断请求的目录是否存在和是否不存在
-x和!-x#判断文件是否可执行和是否不可执行
-e和!-e#判断请求的文件或目录是否存在和是否不存在(包括文件,目录,软链接)

#注意: #如果$变量的值为空字符串或0,则if指令认为该条件为false,其他条件为true。

#nginx 1.0.1之前$变量的值如果以0开头的任意字符串会返回false

测试效果:

编辑/usr/local/nginx/conf.d/var.conf

[root@nginx html]# nginx -s reload

当test2文件不存在时访问

[root@nginx conf.d]# curl var.jcl.org/test2/
/data/web/html/test2/ is not exist

创建test2重新访问
[root@nginx conf.d]# mkdir -p /data/web/html/test2/
[root@nginx conf.d]# echo this is test2 > /data/web/html/test2/index.html
[root@nginx conf.d]# curl var.jcl.org/test2/
this is test2

4.2set指令

指定key并给其定义一个变量,变量可以调用Nginx内置变量赋值给key 另外set定义格式为set $key value,value可以是text, variables和两者的组合。

如下图的子配置文件中自定义一个变量后可以使用

4.3break

用于中断当前相同作用域(location)中的其他Nginx配置,与该指令处于同一作用域的Nginx配置中,位于它前面的配置生效,位于后面的 ngx_http_rewrite_module 模块中指令就不再执行,该指令可以在server块和locationif块中使用。

编辑子配置文件/usr/local/nginx/conf.d/var.conf

[root@nginx html]# nginx -s reload
[root@nginx html]# curl var.jcl.org/break
lee

指定火狐浏览器访问打印两个变量

[root@nginx html]# curl -A "firefox"  var.jcl.org/break
lee
666

4.4return

return用于完成对请求的处理,并直接向客户端返回响应状态码,处于此指令后的所有配 置都将不被执行,return可以在server、if 和 location块进行配置

编辑子配置文件/usr/local/nginx/conf.d/var.conf

当return文件不存在时,不执行后面打印命令

[root@nginx html]# curl -I var.jcl.org/return
HTTP/1.1 301 Moved Permanently
Server: nginx/1.26.2
Date: Mon, 19 Aug 2024 15:28:38 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
Location: http://www.baidu.com

创建文件后访问显示正常,执行后面打印命令

[root@nginx html]# mkdir -p /data/web/html/return
[root@nginx html]# curl -I var.jcl.org/return
HTTP/1.1 200 OK
Server: nginx/1.26.2
Date: Mon, 19 Aug 2024 15:36:04 GMT
Content-Type: text/html
Connection: keep-alive

[root@nginx html]# curl var.jcl.org/return
/data/web/html/return is exist

4.5rewrite

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

语法格式:

rewrite regex replacement [flag];

rewrite将用户请求的URI基于regex所描述的模式进行检查,匹配到时将其替换为表达式指定的新的URI 注意:如果在同一级配置块中存在多个rewrite规则,那么会自下而下逐个检查;被某条件规则替换完成 后,会重新一轮的替换检查,隐含有循环机制,但不超过10次;如果超过,提示500响应码,[flag]所表示的 标志位用于控制此循环机制 如果替换后的URL是以http://或https://开头,则替换结果会直接以重定向返回给客户端, 即永久重定向 301

正则表达式

.#匹配除换行符以外的任意字符
/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以外的任意字符
[^magedu]#匹配除了magedu 这几个字母以外的任意字符

4.5.1rewrite flag 使用介绍

利用nginx的rewrite的指令,可以实现url的重新跳转

rewrite有四种不同的flag,分别是

redirect(临时 重定向302)

permanent(永久重定向301)

breaklast(中断)

前两种是跳转型的flag,后两种是代理型的

临时和永久重定向的最大区别在于,临时重定向是通过访问的浏览器不存放url的重定向信息

永久是要存放。

redirect;#临时重定向,重写完成后以临时重定向方式直接返回重写后生成的新URL给客户端 #由客户端重新发起请求;使用相对路径,或者http://或https://开头,状态码:302
permanent;#重写完成后以永久重定向方式直接返回重写后生成的新URL给客户端 #由客户端重新发起请求,状态码:301
break;#重写完成后,停止对当前URL在当前location中后续的其它重写操作 #而后直接跳转至重写规则配置块之后的其它配置,结束循环,建议在location中使用 #适用于一个URL一次重写
last;#重写完成后,停止对当前URI在当前location中后续的其它重写操作, #而后对新的URL启动新一轮重写检查,不建议在location中使用 #适用于一个URL多次重写,要注意避免出现超过十次以及URL重写后返回错误的给用户

4.5.3rewrite案例
4.5.3.1域名永久与临时重定向

域名的临时的调整,后期可能会变,之前的域名或者URL可能还用、或者跳转的目的域名和URL还会跳 转,这种情况浏览器不会缓存跳转,临时重定向不会缓存域名解析记录(A记录),但是永久重定向会缓存。

[root@nginx html]# vim /usr/local/nginx/conf.d/var.conf

[root@nginx conf.d]# nginx -s reload

先在windows主机的host文件中加入域名解析

host文件的目录:C:\Windows\System32\drivers\etc

通过管理员权限打开:

进入目录后点击文件

在hosts文件中添加var.jcl.org(你配置文件中的域名)

在window上主机浏览器看测试效果:

4.5.3.2break和last区别案例

访问break请求被rewrite至test1,而访问test1转递请求再次被rewrite发送至test2,此测试last和break 分别有什么区别

通过实验演示区别:

首先先创建四个目录:

[root@nginx html]# mkdir /data/web/html/{test1,test2,break,last} -p
[root@nginx html]# echo this is test1 > /data/web/html/test1/index.html
[root@nginx html]# echo this is test2 > /data/web/html/test2/index.html
[root@nginx html]# echo this is last > /data/web/html/last/index.html
[root@nginx html]# echo this is break > /data/web/html/break/index.html

先查看正常的访问途径

编辑子配置文件

编辑完后保存退出,重新加载nginx

在windows的浏览器上访问

break跳转到test2上

访问last目录也跳转到test2上

 访问test1也是正常显示配置文件中编辑的内容

更改子配置文件,添加break和last参数后

效果测试:

因为我们要看出last和break的具体不同,所以访问的时候得具体到访问的文件

 

4.5.3.3自动跳转 https

首先生成证书文件

先创建一个存放证书的目录

[root@nginx nginx]# mkdir certs

手动生成
[root@nginx nginx]# openssl req  -newkey  rsa:2048 -nodes -sha256 -keyout /usr/local/nginx/certs/jcl.org.key -x509  -days 365 -out /usr/local/nginx/certs/jcl.org.crt

切换到目录里查看是否生成

[root@nginx nginx]# cd certs
[root@nginx certs]# ls
jcl.org.crt  jcl.org.key

编辑子配置文件加入全站加密的参数(证书参数、会话超时参数),location中表示强制走加密

[root@nginx conf.d]# vim vhosts.conf

server {
    listen 80;
    listen 443 ssl;
    server_name www.jcl.org;
    root /data/web/html;
    index index.html;
    ssl_certificate /usr/local/nginx/certs/jcl.org.crt;
    ssl_certificate_key /usr/local/nginx/certs/jcl.org.key;
    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;

     llocation / {
        if ( $scheme = http ){
            rewrite /(.*) https://$host/$1 redirect;
        }


}

测试效果:

通过输入域名直接使用https协议

出现以下界面点击继续访问

查看证书

4.5.3.4判断文件是否存在

编辑子配置文件

[root@nginx conf.d]# vim vhosts.conf

 location / {
        if ( $scheme = http ){
            rewrite /(.*) https://$host/$1 redirect;
        }

添加以下if语句表示如果访问的文件不存在自动跳转到主目录中去

        if ( !-e $request_filename ){
            rewrite /(.*) https://$host/index.html redirect;
        }
    }

效果测试:

访问www.jcl.org/abs

跳转到www.jcl.org/index.html

5、nginx的防盗链

5.1介绍

防盗链基于客户端携带的referer实现,referer是记录打开一个页面之前记录是从哪个页面跳转过来的标 记信息,如果别人只链接了自己网站图片或某个单独的资源,而不是打开了网站的整个页面,这就是盗链,referer就是之前的那个网站域名,正常的referer信息有以下几种:

none:        #请求报文首部没有referer首部,

                    #比如用户直接在浏览器输入域名访问web网站,就没有referer信息。

blocked:        #请求报文有referer首部,但无有效值,比如为空。

server_names:   #referer首部中包含本主机名及即nginx 监听的server_name。

arbitrary_string: #自定义指定字符串,但可使用*作通配符。示例: *.timinglee.org                                 www.timinglee.*

regular expression: #被指定的正则表达式模式匹配到的字符串,要使用~开头,例如:                                 ~.*\.timinglee\.com

5.2盗链网页配置

先在100主机上放两张照片在不同的目录下

 在/data/web/html下创建一个images目录存放一张图片

在该目录下放另一张图片,为了防止盗链

克隆一个虚拟机出来设置IP为172.25.254.10,安装apache服务httpd

[root@fangdao ~]# 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.jcl.org/images/nginxFangDao1.jpg" >
    <h1 style="color:red">欢迎大家</h1>
    <p><a href=http://www.jcl.org>点击我去看新世界</a>hahahaha</p>
  </body>

</html>

重启apache在浏览器访问测试:

可以看到大海这张图就是从172.25.254.100(www.jcl.org)上获取的,点击按钮还能跳转到100主机的访问页面,这就是盗链。

点击按钮会跑到100主机上去访问

那我们怎么防止盗链呢

首先编辑一个子配置文件

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

添加对方访问到我指定的界面

location /images  {
        valid_referers none blocked server_names *.jcl.org ~/.baidu/.; ----表示允许可以通过哪些跳转到我的界面
        if ( $invalid_referer ){
                rewrite ^/   http://www.jcl.org/nginxFangDao2.jpg;  ----如果除了以上域名外的访问就重定向到我的一张图片上
                #return 403;
        }

    }

加上让他访问不到我的界面

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

    }

重启nginx

[root@nginx conf.d]# nginx -s reload
再访问10主机

如果添加了下面的代码,就访问不到100主机的界面并且显示403

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

    }

六、nginx的反向代理功能

1、反向代理配置的参数

proxy_pass;

                #用来设置将客户端请求转发给的后端服务器的主机

                #可以是主机名(将转发至后端服务做为主机头首部)、IP地址:端口的方式

                #也可以代理到预先设置的主机群组,需要模块ngx_http_upstream_module支持

proxy_hide_header field;

                #用于nginx作为反向代理的时候 #在返回给客户端http响应时

                #隐藏后端服务器相应头部的信息

                #可以设置在http,server或location块

proxy_pass_header field;

                #透传

                #默认nginx在响应报文中不传递后端服务器的首部字段Date, Server, X-Pad, X-Accel等参数

                #如果要传递的话则要使用 proxy_pass_header field声明将后端服务器返回的值传递给客户端

                #field 首部字段大小不敏感

proxy_pass_request_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_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

2、反向代理两台客户端

在子配置文件中添加代理的后端服务器

在两台客户主机中安装httpd并打印内容在/var/www/html/index.html

[root@node1 ~]echo 172.25.254.10 > /var/www/html/index.html

[root@node2 ~]# mkdir -p /var/www/html/static
[root@node2 ~]# echo static - 172.25.254.20 > /var/www/html//static/index.html

在20主机中将httpd的主配置文件的端口号改为8080;

[root@node2 ~]# vim /etc/httpd/conf/httpd.conf

测试效果:

3、反向代理实现动静分离

在10主机安装php

[root@node1 ~]# yum install php -y

vim /var/www/html/index.php

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

测试:

4、反向代理的缓存功能

缓存功能默认为关闭状态,需要手动配置

proxy_cache zone_name | off; 默认off

                #指明调用的缓存,或关闭缓存机制;Context:http, server, location #zone_name 表示缓存的名称.需要由proxy_cache_path事先定义

proxy_cache_key string

                #缓存中用于“键”的内容,默认值:proxy_cache_key $scheme$proxy_host$request_uri;

proxy_cache_valid [code ...] time;

                #定义对特定响应码的响应内容的缓存时长,定义在http{...}中

proxy_cache_path;

                #定义可用于proxy功能的缓存;Context:http

proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | off ;

                #默认是off #在被代理的后端服务器出现哪种情况下,可直接使用过期的缓存响应客户端

proxy_cache_methods GET | HEAD | POST ...;

                #对哪些客户端请求方法对应的响应进行缓存,GET和HEAD方法总是被缓存

4.1非缓存场景压测

[root@node2 ~]# cd /var/www/html/static

准备测试页面

[root@node2 static]# cat /var/log/messages > ./log.html
[root@node2 static]# ab -n1000 -c100  http://www.jcl.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.jcl.org (be patient)
Completed 100 requests
Completed 200 requests

Server Software:        Apache
Server Hostname:        www.jcl.org
Server Port:            80

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

Concurrency Level:      100
Time taken for tests:   22.163 seconds
Complete requests:      1000
Failed requests:        0
Non-2xx responses:      1000
Total transferred:      474000 bytes
HTML transferred:       249000 bytes
Requests per second:    45.12 [#/sec] (mean)

可以看到每秒中的发送的请求速度,如果做了缓存这个速度会很快
Time per request:       2216.293 [ms] (mean)
Time per request:       22.163 [ms] (mean, across all concurrent requests)

Transfer rate:          20.89 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       98  345 464.5    113    3158
Processing:   100  999 1697.6    501   14678
Waiting:      100  878 1556.5    458   14678
Total:        200 1344 1719.1    899   14784

4.2准备缓存配置

编辑主配置文件

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

在http配置段添加下列参数定义缓存信息为缓存保存路径/usr/local/nginx/proxy_cache

定义缓存目录结构层次 2^4x2^8x2^8=2^20=1048576个目录

以及内存缓存的大小主要用于存放key和metadata

缓存有效时间为120s

最大磁盘空间为1g

 proxy_cache_path /usr/local/nginx/proxy_cache levels=1:2:2 keys_zone=proxycache:20m inactive=120s max_size=1g;

编辑子配置文件

[root@nginx conf.d]# vim huancun.conf

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

    location ~ \.php$ {
        proxy_pass http://172.25.254.10:80;
    }

    location /static{
        proxy_pass http://172.25.254.10:80;
        proxy_cache proxycache;——调用缓存名称为proxycache
        proxy_cache_key $request_uri;——缓存中使用uri的内容就是/后的内容
        proxy_cache_valid 200 302 301 10m;——定义200 302 301的响应内容的缓存时长为10分钟
        proxy_cache_valid any 1m;——定义其他值为1分钟,最后两条套同时添加
    }
}

重新加载并检测一下

[root@nginx conf.d]# nginx -s reload
[root@nginx conf.d]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

切换到缓存指定的目录查看是否生成了缓存文件

[root@nginx conf.d]# ll /usr/local/nginx/
total 0
drwxr-xr-x 2 root  root  40 Aug 18 10:53 html
drwxr-xr-x 2 root  root  58 Aug 18 12:35 logs
drwx------ 2 nginx root   6 Aug 18 21:24 proxy_cache
drwx------ 2 nginx root   6 Aug 18 12:35 proxy_temp

[root@nginx nginx]#  tree /usr/local/nginx/proxy_cache/
/usr/local/nginx/proxy_cache/

4.3访问并验证缓存

再次进行压测

[root@node2 static]# ab -n1000 -c100  http://www.jcl.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.jcl.org (be patient)
Completed 100 requests
Completed 200 requests
Server Software:        Apache
Server Hostname:        www.jcl.org
Server Port:            80

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

Concurrency Level:      100
Time taken for tests:   44.711 seconds
Complete requests:      1000
Failed requests:        0
Non-2xx responses:      1000
Total transferred:      474000 bytes
HTML transferred:       249000 bytes
Requests per second:    22.37 [#/sec] (mean)
Time per request:       4471.070 [ms] (mean)
Time per request:       44.711 [ms] (mean, across all concurrent requests)

Transfer rate:          10.35 [Kbytes/sec] received

5、http反向代理的负载均衡

5.1http upstream配置参数

自定义一组服务器,配置在http块内

配置一个后端web服务器,配置在upstream内,至少要有一个server服务器配置。

server支持的parameters如下:

weight=number        #设置权重,默认为1,实现类似于LVS中的WRR,WLC等

max_conns=number        #给当前后端server设置最大活动链接数,默认为0表示没有限制

max_fails=number        #后端服务器的下线条件,当客户端访问时,对本次调度选中的后端服务器连续进行检 测多少次,如果都失败就标记为不可用,默认为1次,当客户端访问时,才会利用TCP触发对探测后端服务器健康性 检查,而非周期性的探测

fail_timeout=time        #后端服务器的上线条件,对已经检测到处于不可用的后端服务器,每隔此时间间隔再 次进行检测是否恢复可用,如果发现可用,则将后端服务器参与调度,默认为10秒

backup        #设置为备份服务器,当所有后端服务器不可用时,才会启用此备用服务器 

down        #标记为down状态,可以平滑下线后端服务器

resolve          #当server定义的是主机名的时候,当A记录发生变化会自动应用新IP而不用重启nginx

hash KEY [consistent];         #基于指定请求报文中首部字段或者URI等key做hash计算,使用consistent参数,将使用ketama一致性 hash算法,适用于后端是Cache服务器(如varnish)时使用,consistent定义使用一致性hash运算,一致 性hash基于取模运算

hash $request_uri consistent;         #基于用户请求的uri做hash

hash $cookie_sessionid          #基于cookie中的sessionid这个key进行hash调度,实现会话绑定

ip_hash;         #源地址hash调度方法,基于的客户端的remote_addr(源地址IPv4的前24位或整个IPv6地址)做hash计 算,以实现会话保持

least_conn;         #最少连接调度算法,优先将客户端请求调度到当前连接最少的后端服务器,相当于LVS中的WLC

5.2反向代理示例:后端多台web服务器

编辑子配置文件

[root@nginx conf.d]# vim fuzai.conf

upstream webcluster{
    ip_hash;
    #hash $request_uri consistent;
    #hash $cookie_jcl;
    server 172.25.254.10:80 fail_timeout=15s max_fails=3;
    server 172.25.254.20:8080 fail_timeout=15s max_fails=3;
    #server 172.25.254.100:80 backup;
}

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

    location / {
        proxy_pass http://webcluster;
    }
}

测试效果:

都会访问到10因为用的算法ip_hash

使用对uri进行hash算法

对cookie值进行hash

6、四层负载均衡

6.1配置DNS

首先要在两台后端服务器上安装DNS服务

dnf install bind -y

vim /etc/named.conf

[root@node2 etc]# cd /var/named/

cp named.localhost jcl.org.zone -p

vim jcl.org.zone

[root@node2 etc]# vim named.rfc1912.zones

[root@node2 named]# scp -p /etc/named.{conf,rfc1912.zones} root@172.25.254.10:/etc

[root@node2 named]# scp -p /var/named/jcl.org.zone root@172.25.254.10:/var/named/jcl.org.zone

重启服务以后测试

6.2安装数据库mariadb

dnf install mariadb-server -y

编辑数据库配置文件增加一条id参数

[root@node2 named]# vim /etc/my.cnf.d/mariadb-server.cnf

在nginx服务器上编辑子配置文件

stream {

        upstream dns_server {

                server 172.25.254.20:53 max_fails=3 fail_timeout=30s;

                server 172.25.254.30:53 max_fails=3 fail_timeout=30s;

                }

                server {

                        listen 172.25.254.10:53 udp;

                        proxy_pass dns_server;

                        proxy_timeout 1s;

                }

          upstream mysql_server {

                server 172.25.254.20:3306 max_fails=3 fail_timeout=30s;

                server 172.25.254.30:3306 max_fails=3 fail_timeout=30s;

                }

                server {

                          listen 172.25.254.10:3306;

                          proxy_pass mysql_server; proxy_connect_timeout 30s;

                          proxy_timeout 300s;

                }

}

DNS

效果测试:

数据库的四层负载效果测试:

七、实现 FastCGI

介绍

CGI通用网关接口common gateway interface

FastCGI保留进程处理更多的请求

PHP—FPM FastCGI管理工具

1、重新编译安装ngin增加两个模块

解压需要的软件包

   72  tar zxf memc-nginx-module-0.20.tar.gz
   73  tar zxf nginx-1.26.1.tar.gz
   74  tar zxf php-8.3.9.tar.gz
   75  tar zxf srcache-nginx-module-0.33.tar.gz
   76  tar zxf echo-nginx-module-0.63.tar.gz
   77  tar zxf openresty-1.25.3.1.tar.gz

cd nginx-1.26.1/

安装依赖

 dnf install gcc pcre-devel zlib-devel openssl-devel -y

#######nginx编译安装检测命令#######

./configure --prefix=/usr/local/nginx --user=nginx --group=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 --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

编辑nginx的启动文件,内容在上面nginx编译安装已讲

 vim /lib/systemd/system/nginx.service

[root@nginx2 ~]# systemctl daemon-reload
[root@nginx2 ~]# systemctl start nginx

开始编译安装php

cd php-8.3.9/

yum install -y bzip2 systemd-devel libxml2-devel sqlite-devel libpng-devel libcurl-devel

cd /mnt

网络获取

wget oniguruma-devel包 https://mirrors.aliyun.com/rockylinux/9.4/devel/x86_64/kickstart/Packages/o/oniguruma-devel-6.9.6-1.el9.5.x86_64.rpm

dnf install oniguruma-devel-6.9.6-1.el9.5.x86_64.rpm

cd php-8.3.9/

#########编辑安装检测命令########
 ./configure --prefix=/usr/local/php --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

开始编译

 make && make install

2、优化php配置

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

[root@nginx2 php-fpm.d]# cd /root/php-8.3.9/

[root@nginx2 php-8.3.9]# cp php.ini-production /usr/local/php/etc/php.ini
[root@nginx2 php-8.3.9]# cd /usr/local/php/etc
[root@nginx2 etc]# vim php.ini

[root@nginx2 etc]# cd /root/php-8.3.9/sapi/fpm/

[root@nginx2 fpm]# cp php-fpm.service /lib/systemd/system/

[root@nginx2 fpm]# vim /lib/systemd/system/php-fpm.service

[root@nginx2 fpm]# systemctl daemon-reload
[root@nginx2 fpm]# systemctl start php-fpm
[root@nginx2 fpm]# netstat -antlupe   | grep php
tcp        0      0 0.0.0.0:9000            0.0.0.0:*               LISTEN         0          194515     210135/php-fpm: mas

3、准备php测试页面


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

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

[root@nginx2 sbin]#  source  ~/.bash_profile

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

[root@nginx2 nginx]# mkdir conf.d
[root@nginx2 nginx]# vim conf/nginx.conf

[root@nginx2 nginx]# cd conf.d
[root@nginx2 conf.d]# vim vhosts.conf

测试php页面的访问

4、提高php性能方案

4.1、php的动态扩展模块

(php的缓存模块)

配置php使其支持memcache

添加并解压memcache的功能模块

安装memcache模块

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

[root@Nginx memcache-8.2]# yum install autoconf

[root@Nginx memcache-8.2]# phpize

[root@Nginx memcache-8.2]# ./configure && make && make install

[root@Nginx memcache-8.2]# ls /usr/local/php/lib/php/extensions/no-debug-non-zts 20230831/

memcache.so opcache.so

配置php加载memcache模块

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

;extension=zip extension=memcache

;zend_extension=opcache

[root@Nginx ~]# systemctl reload php-fpm

[root@Nginx no-debug-non-zts-20230831]# php -m | grep mem

memcache

部署memcached

[root@nginx2 memcache-8.2]# yum install memcached -y

[root@nginx2 memcache-8.2]# systemctl enable --now memcached.service

[root@nginx2 memcache-8.2]# netstat -antlupe | grep memcache
tcp        0      0 127.0.0.1:11211         0.0.0.0:*               LISTEN      977        23621      915/memcached
tcp6       0      0 ::1:11211               :::*                    LISTEN      977  

[root@nginx2 memcache-8.2]# cat /etc/sysconfig/memcached
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS="-l 127.0.0.1,::1"
 

复制测试文件到nginx发布目录中

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

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

测试访问:

www.jcl.org/memcache.php

www.jcl.org/example.php

4.2php高速缓存

部署方法

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

upstream memcache {

        server 127.0.0.1:11211;

        keepalive 512;

        }

server {

        listen 80;

        server_name php.timinglee.org;

         root /data/php;

        location /memc {

                internal;

                memc_connect_timeout 100ms;

                memc_send_timeout 100ms;

                memc_read_timeout 100ms;

                set $memc_key $query_string;

                set $memc_exptime 300;

                memc_pass memcache;

                }

        location ~ \.php$ {

                set $key $uri$args;

                srcache_fetch GET /memc $key;

                srcache_store PUT /memc $key;

                fastcgi_pass 127.0.0.1:9000;

                fastcgi_index index.php;

                include fastcgi.conf;

                 }

 }

[root@Nginx ~]# systemctl start nginx.service

测试结果:

[root@node2 ~]# ab -n500 -c10 http://php.timinglee.org/index.php

Concurrency Level:     10

Time taken for tests:   0.255 seconds

Complete requests:     500

Failed requests:       0

八、nginx二次开发版本

编译安装openresty

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

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

[root@Nginx ~]#cd /usr/local/src

[root@Nginx src]#wget https://openresty.org/download/openresty-1.17.8.2.tar.gz

[root@Nginx src]#tar xf openresty-1.17.8.2.tar.gz

[root@Nginx src]#cd openresty-1.17.8.2/

[root@Nginx openresty-1.17.8.2]#./configure \

--prefix=/apps/openresty \--user=nginx --group=nginx \--with-http_ssl_module \--with-http_v2_module \--with_http_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.17.8.2]#make && make install

[root@Nginx openresty-1.17.8.2]#ln -s /usr/local/openresty/bin/* /usr/bin/

[root@Nginx openresty-1.17.8.2]#openresty -v

nginx version: openresty/1.17.8.2

[root@Nginx openresty-1.17.8.2]#openresty

[root@Nginx openresty-1.17.8.2]#ps -ef |grep nginx

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值