美文网首页
第7章 Nginx动态添加模块

第7章 Nginx动态添加模块

作者: hubery_zhao | 来源:发表于2023-02-28 11:18 被阅读0次

1.1 添加nginx模块

之前想要测试防盗链模块,但是提示测试失败,想起来没有安装这个模块,现在平滑添加防盗链模块

[root@centos-13 conf]# nginx.bak02221118 -t

nginx: [emerg] unknown directive "secure_link" in /root/nginx/conf/nginx.conf:21

nginx: configuration file /root/nginx/conf/nginx.conf test failed

1.1.1 查看nginx安装了哪些模块


nginx version: nginx/1.22.1

built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)

built with OpenSSL 1.0.2k-fips 26 Jan 2017

TLS SNI support enabled

configure arguments: --prefix=/root/nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-stream --with-http_realip_module --with-http_flv_module --with-http_random_index_module --with-mail --with-pcre # 这里是已经安装了的模块信息

1.1.1 添加模块、配置编译

进入nginx压缩包解压,进入重新配置nginx信息,然后编译。 不要知悉make install , 不然就覆盖了原有nginx,就不是平滑升级了

[root@centos-13 ~]# cd /data1
[root@centos-13 data1]# ls
nginx-1.22.1  nginx-1.22.1.tar.gz
[root@centos-13 data1]# cd nginx-1.22.1
[root@centos-13 nginx-1.22.1]# ls
auto     CHANGES.ru  configure  html     Makefile  objs    src
CHANGES  conf        contrib    LICENSE  man       README
[root@centos-13 nginx-1.22.1]#  ./configure  --prefix=/root/nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-stream --with-http_realip_module --with-http_flv_module --with-http_random_index_module --with-mail --with-pcre  --with-http_secure_link_module
#  在末尾新增--with-http_secure_link_module

[root@centos-13 nginx-1.22.1]# make
make -f objs/Makefile
make[1]: Entering directory `/data1/nginx-1.22.1'
…

1.1.1 备份、置换

备份原nginx ,然后新nginx置换老nginx

[root@centos-13 nginx-1.22.1]# cp /root/nginx/sbin/nginx /root/nginx/sbin/nginx.bak02221118

[root@centos-13 nginx-1.22.1]# mv objs/nginx /root/nginx/sbin/

mv: overwrite ‘/root/nginx/sbin/nginx’? y

[root@centos-13 nginx-1.22.1]# ls /root/nginx/sbin/

nginx nginx.bak02221118

1.1 添加第三方模块

1.1.1 学习和下载第三方模块

这里我们演示在 Nginx 中使用第三方模块。 Openresty 社区提供了一款 Nginx中的 Echo 模块,即 echo-nginx-module。在 Nginx 中添加了该模块后,我们在配置文件中可以使用该模块提供的 echo 指令返回用户响应,简单方便。该模块的源码在 github 上(https://github.com/openresty/echo-nginx-module),并且有良好的文档和使用示例,非常方便开发者使用。

image.png

1.1.1 编译第三方模块到现有nginx中

[root@centos-13 data1]# ll
total 1128
drwxr-xr-x. 5 root root     174 Aug  1 07:52 echo-nginx-module-master      # 已下载解压完成
-rw-r--r--. 1 root root   77281 Nov 22 22:52 master.zip
drwxr-xr-x. 9 a    a        186 Nov 18 18:33 nginx-1.22.1
-rw-r--r--. 1 root root 1073948 Oct 26 07:00 nginx-1.22.1.tar.gz
[root@centos-13 data1]# cd nginx-1.22.1
[root@centos-13 nginx-1.22.1]# pwd
/data1/nginx-1.22.1
[root@centos-13 nginx-1.22.1]# ./configure  --prefix=/root/nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-stream --with-http_realip_module --with-http_flv_module --with-http_random_index_module --with-mail --with-pcre --with-http_secure_link_module  --add-module=/data1/echo-nginx-module-master
…
[root@centos-13 nginx-1.22.1]# make
…
[root@centos-13 nginx-1.22.1]# cd objs/
[root@centos-13 objs]# ll
total 8528
drwxr-xr-x. 3 root root      17 Nov 22 23:58 addon
-rw-r--r--. 1 root root   18631 Nov 22 23:58 autoconf.err
-rw-r--r--. 1 root root   60343 Nov 22 23:58 Makefile
-rwxr-xr-x. 1 root root 8520264 Nov 23 00:00 nginx
-rw-r--r--. 1 root root    5517 Nov 23 00:00 nginx.8
-rw-r--r--. 1 root root    7710 Nov 22 23:58 ngx_auto_config.h
-rw-r--r--. 1 root root     657 Nov 22 23:58 ngx_auto_headers.h
-rw-r--r--. 1 root root    9581 Nov 22 23:58 ngx_modules.c
-rw-r--r--. 1 root root   94136 Nov 23 00:00 ngx_modules.o
drwxr-xr-x. 9 root root      91 Nov 18 18:33 src
[root@centos-13 objs]# cp /root/nginx/sbin/nginx  /root/nginx/sbin/nginx.bak20221122
[root@centos-13 objs]# mv nginx  /root/nginx/sbin/
mv: overwrite ‘/root/nginx/sbin/nginx’? y
[root@centos-13 objs]# nginx -t
nginx: the configuration file /root/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /root/nginx/conf/nginx.conf test is successful
[root@centos-13 objs]# nginx -s reload
[root@centos-13 objs]# ps aux|grep nginx
root      43902  0.0  0.1  46552  1948 ?        S    00:06   0:00 nginx: worker process
root      43913  0.0  0.0 112812   980 pts/0    R+   00:06   0:00 grep --color=auto nginx
root     105900  0.0  0.2  46316  2016 ?        Ss   Nov22   0:00 nginx: master process nginx

1.1.1 修改nginx配置

user root;

worker_processes 1;

events {

 worker_connections 1024;

}

http {

 include mime.types;

 default_type application/octet-stream;

 sendfile on;

 keepalive_timeout 65;

 server {

 listen 80;

 server_name localhost;

 location / {

 root html;

 index index.html index.htm;

 }

 location /timed_hello {

 default_type text/plain;

 echo_reset_timer;

 echo hello world;

 echo "'hello world' takes about $echo_timer_elapsed sec.";

 echo hiya igor;

 echo "'hiya igor' takes about $echo_timer_elapsed sec.";

 }

 location /echo_with_sleep {

 default_type text/plain;

 echo hello world;

 echo_flush;

 # ensure the client can see previous output immediately

 echo_sleep 2.5;

 # in sec

 echo "'hello' takes about $echo_timer_elapsed sec.";

 }

 error_page 500 502 503 504 /50x.html;

 location = /50x.html {

 root html;

 }

 }

}

Kill掉nginx,重启nginx ,测试端口

[root@centos-13 conf]# nginx -t
nginx: the configuration file /root/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /root/nginx/conf/nginx.conf test is successful
[root@centos-13 conf]# nginx -s reload
[root@centos-13 conf]# curl  127.0.0.1/timed_hello
hello world
'hello world' takes about 0.000 sec.
hiya igor
'hiya igor' takes about 0.000 sec.
[root@centos-13 conf]# curl  127.0.0.1/echo_with_sleep
hello world
'hello' takes about 2.504 sec.  # 这里延时了2.5秒

启动 Nginx 后,我们就可以在浏览器上请求者两个 URI 地址,看到相应 echo返回的信息了。第二个配置是使用了 echo_sleep 指令,会使得请求在休眠 2.5s 才返回


image.png
image.png

相关文章

网友评论

      本文标题:第7章 Nginx动态添加模块

      本文链接:https://www.haomeiwen.com/subject/sgjafdtx.html