nginx如何新增模块——add-module

niginx部署

      这里参考:
      https://blog.csdn.net/weixin_42313749/article/details/100088174

新的需求–添加一个echo模块

在nginx已经安装的情况下新增echo模块

  • 下载需要新增的模块(升级nginx与此一样)
    模块:v0.61.tar.gz

  • 解压到与nginx包解压同级的目录

[root@nginx ~]# cd /usr/src/
[root@nginx src]# ls
debug  kernels  nginx-1.16.1  nginx-1.16.1.tar.gz  v0.61.tar.gz
[root@nginx src]# tar xf v0.61.tar.gz 
[root@nginx src]# ls
debug  echo-nginx-module-0.61  kernels  nginx-1.16.1  nginx-1.16.1.tar.gz  v0.61.tar.gz
  • 进入nginx的解压目录
[root@nginx src]# cd nginx-1.16.1/
[root@nginx nginx-1.16.1]# nginx -V      #查看原有的编译选项并复制
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log

  • 重新配置与编译
    语法:./configure --add-module=…/新增模块目录
    原有的编译选项+新增的模块
  [root@nginx nginx-1.16.1]# ./configure --add-module=/usr/src/echo-nginx-module-0.61 \
    --prefix=/usr/local/nginx \
    > --user=nginx \
    > --group=nginx \
    > --with-debug \
    > --with-http_ssl_module \
    > --with-http_realip_module \
    > --with-http_image_filter_module \
    > --with-http_gunzip_module \
    > --with-http_gzip_static_module \
    > --with-http_stub_status_module \
    > --http-log-path=/var/log/nginx/access.log \
    > --error-log-path=/var/log/nginx/error.log

  • 编译好之后,直接make,禁止make install,make install会直接覆盖以前的安装配置
[root@nginx nginx-1.16.1]# make
......
[root@nginx nginx-1.16.1]# ls
auto     CHANGES.ru  configure  html     Makefile  objs    src
CHANGES  conf        contrib    LICENSE  man       README
[root@nginx nginx-1.16.1]# cd objs/
[root@nginx objs]# ls
addon         Makefile  nginx.8            ngx_auto_headers.h  ngx_modules.o
autoconf.err  nginx     ngx_auto_config.h  ngx_modules.c       src

新安装编译的在当前目录的objs里面,在做下一步前,去备份原来的nginx配置文件

  • 停止服务并备份原有的程序文件
[root@nginx ~]#nginx -s stop
[root@nginx ~]# cd /usr/local/nginx/sbin/
[root@nginx sbin]# ls
nginx
[root@nginx sbin]# mv nginx{,bak}
[root@nginx sbin]# ls
nginx.bak
  • 将新编译的程序文件替换原有的程序文件
[root@nginx sbin]# cp /usr/src/nginx-1.16.1/objs/nginx  /usr/local/nginx/sbin/
[root@nginx sbin]# ls
nginx  nginx.bak
[root@nginx sbin]# ll
总用量 12976
-rwxr-xr-x  1 root root 6948144 8月  28 10:42 nginx
-rwxr-xr-x. 1 root root 6335704 8月  28 01:57 nginx.bak

  • 启动nginx
[root@nginx ~]#nginx
[root@nginx sbin]# ss -antl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128               *:111                           *:*                  
LISTEN     0      128               *:80                            *:*                  
LISTEN     0      5      192.168.122.1:53                            *:*                  
LISTEN     0      128               *:22                            *:*                  
LISTEN     0      128       127.0.0.1:631                           *:*                  
LISTEN     0      100       127.0.0.1:25                            *:*                  
LISTEN     0      128       127.0.0.1:6010                          *:*                  
LISTEN     0      128              :::111                          :::*                  
LISTEN     0      128              :::22                           :::*                  
LISTEN     0      128             ::1:631                          :::*                  
LISTEN     0      100             ::1:25                           :::*                  
LISTEN     0      128             ::1:6010                         :::*         

验证:

  • 在配置文件里修改location
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
location / {
            root   html;
            echo   'hello world';
            index  index.html index.htm;
        }


           location = /abc {
             echo ' I LOVE YOU ';
             root html;
             index index.html;
         }
  
  • 刷新配置文件
[root@nginx ~]# nginx -s reload              

//在另一个终端 192.168.176.112下打开   
[root@mysql ~]# curl http://192.168.176.111/abc
I LOVE YOU
[root@mysql ~]# curl http://192.168.176.111/abcd
hellow world
[root@mysql ~]# curl http://192.168.176.111/abc
hellow world

总结

nginx的配置文件很多,特别注意location这个,做的时候,记得关闭防火墙和selinux。验证的时,只能在终端里看到效果,web界面无法看到

发布了60 篇原创文章 · 获赞 3 · 访问量 2093

猜你喜欢

转载自blog.csdn.net/weixin_42313749/article/details/100167154