美文网首页
NGINX 安装ECHO模块

NGINX 安装ECHO模块

作者: 胸口砕大石 | 来源:发表于2021-08-20 15:53 被阅读0次

    传送门

    项目地址
    安装教程

    安装GIT

     yum install git -y
    

    下载放到/opt/nginx-plugs/echo-moudle目录下

    git clone https://github.com/openresty/echo-nginx-module.git
    

    扩展nginx的功能需要从新编译,编译前必须停止服务;如果服务不停止,则无法用新生成的nginx二级制程序替代原有程序

    nginx -s stop   
    

    配置先进入/opt/nginx-plugs/echo-moudle,并给config添加之星权限

    真相

    通过yum安装的nginx,需要下载yum安装的nginx相同版本的nginx源码,然后通过源码进行编译,在编译中添加新的模块,然后将编译完成后的nginx可执行文件进行替换。

    用NGINX -V获取原有参数

    [root@nginx echo-moudle]# nginx -V
    nginx version: nginx/1.20.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=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
    

    下载解压

    # 官网下载nginx-1.12.1 并解压
    $ wget http://nginx.org/download/nginx-1.12.2.tar.gz
    $ tar -zxvf nginx-1.12.2.tar.gz
    

    备份原有nginx和配置(已照快照省略)

    # 暂时备份到用户目录下,以备操作错误时,进行还原
    $ cp /usr/sbin/nginx ~/nginx.back
    $ cp -r /ect/nginx ~/nginx.conf.back
    

    编译nginx,添加模块

    # 安装编译的依赖包
    yum install gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel libxml2 libxml2-dev libxslt-devel gd-devel perl-devel perl-ExtUtils-Embed GeoIP GeoIP-devel GeoIP-data gperftools redhat-rpm-config
     
    # 进入nginx,进行编译,编译时需要将上面查看到的现有nginx的参数都带上,然后在最后面添加你需要新增的模块
    $ cd nginx-1.12.1
      
    # 其中最后的--add-module=第三方模块的存放路径,第三方模块需要提前下载好
    # 例子:--add-module=/opt/nginx/echo-nginx-module-0.62
    # 不管是第三方模块,还是自身模块,都直接在最后添加参数即可。
    $ ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --add-module=/opt/nginx/echo-nginx-module-0.62
     
     
     
    # 编译,注意:这里很多说不能make install,只make就好了,其实不然,
    # 在执行./configure命令时则已经查出了原有nginx的各个文件的路径,如果原来没有nginx,则会默认安装到/usr/local/nginx目录下。
    # 直接执行make install 则会自动替换原来的nginx,无须手动,也不会中断nginx进程。
    $ make && make install 
    

    检查新nginx,重启生效

    # 编译后,会自动替换原有nginx,查看nginx配置,是否还是之前的,查看conf.d内的配置是否还在
    $ cat /etc/nginx/nginx.conf 
    $ ls /etc/nginx/conf.d
     
    # 查看nginx参数,看看我们新增的模块在不在
    $ nginx -V
     
    # 重启nginx生效
    $ systemctl restart nginx
    

    ECHO使用

    location /echo {
           echo "hello world";
           default_type text/html;     
       }
    当访问www.xxx.com/echo时,将会在浏览器上打印"hello world"
    如果不加default_type text/html;,
    访问www.xxx.com/echo时,将会把它当做文件下载下来,
    因为echo默认不是mime里所支持的格式;
    加上这项是告诉浏览器用html的格式去解析它
    

    相关文章

      网友评论

          本文标题:NGINX 安装ECHO模块

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