如何部署nginx的echo模块?

Nginx

1 基础知识

1.1 模块名称

echo-nginx-module

1.2 模块的作用

Nginx内部通过配置文件实现shell风格指令,例如,
– echo
– sleep
– time
– exec

2 环境部分

2.1 系统环境

IP Address = 10.168.0.154
Host Name = nginx_echo.cmdschool.org
OS = CentOS 7.3

2.2 yum源配置

yum -y install gcc gcc-c++ make expat-devel 
yum -y install rpm-build redhat-lsb
yum -y install vim wget

2.3 创建构建用户

useradd -u 1001 builder

2.4 关闭selinux

setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

3 构建rpm包部分

3.1 下载安装包

su - builder
cd ~
wget http://nginx.org/packages/centos/7/SRPMS/nginx-1.14.0-1.el7_4.ngx.src.rpm
wget -O echo-nginx-module-master.zip https://codeload.github.com/openresty/echo-nginx-module/zip/master

3.2 解压并测试构建环境

3.2.1 构建环境测试命令

rpm -ivh nginx-1.14.0-1.el7_4.ngx.src.rpm
rpmbuild -bb rpmbuild/SPECS/nginx.spec

3.2.2 异常情况

如果看到类似提示:

error: Failed build dependencies:
        openssl-devel >= 1.0.1 is needed by nginx-1:1.14.0-1.el7.ngx.x86_64
        zlib-devel is needed by nginx-1:1.14.0-1.el7.ngx.x86_64
        pcre-devel is needed by nginx-1:1.14.0-1.el7.ngx.x86_64

请先决绝包的依赖关系然后重试:

exit
yum -y install openssl-devel zlib-devel pcre-devel

3.2.3 正常情况

如果末尾类似的提示表示构建环境测试通过或称重新打包完成:

+ umask 022
+ cd /home/builder/rpmbuild/BUILD
+ cd nginx-1.14.0
+ /usr/bin/rm -rf /home/builder/rpmbuild/BUILDROOT/nginx-1.14.0-1.el7_4.ngx.x86_64
+ exit 0

3.3 配置webdav模块

unzip echo-nginx-module-master.zip
cp -a echo-nginx-module-master/ rpmbuild/BUILD/echo-nginx-module

3.4 修改构建文件

3.4.1 创建配置文件副本

cd rpmbuild/SPECS
cp nginx.spec nginx.spec.orig

3.4.2 修改配置文件参数

vim nginx.spec

修改如下行:

--with-stream_ssl_preread_module

增加参数后行如下:

--with-stream_ssl_preread_module --add-module=../echo-nginx-module

3.5 重新构建rpm包

diff -uN nginx.spec.orig nginx.spec > nginx-echo-ext.patch
patch -p0 < nginx-echo-ext.patch

如果出现如下提示,按照以下向导处理,

patching file nginx.spec
Reversed (or previously applied) patch detected!  Assume -R? [n] n
Apply anyway? [n] y
Hunk #1 FAILED at 55.
1 out of 1 hunk FAILED -- saving rejects to file nginx.spec.rej

继续完成重构

rpmbuild -bb nginx.spec
exit

4 配置Nginx服务

4.1 安装nginx包

cd /home/builder/rpmbuild/RPMS/x86_64/
yum -y install nginx-1.14.0-1.el7_4.ngx.x86_64.rpm

4.2 确认模块启用

nginx -V

显示如下:

nginx version: nginx/1.14.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (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 --add-module=../echo-nginx-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=../echo-nginx-module

4.3 启动服务并配置自启动

systemctl start nginx.service
systemctl enable nginx.service

4.4 配置防火墙

firewall-cmd --permanent --add-service http
firewall-cmd --reload
firewall-cmd --list-all

4.5 增加带echo的配置

vim /etc/nginx/conf.d/default.conf

加入如下行:

server {
    [..]
    location / {
        [..]
        echo "Hello word!";
    }
    [..]
}

4.6 校验配置文件

nginx -t

如果看到以下提示则达到预期:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

如果看到以下提示则重构有问题:

nginx: [emerg] unknown directive "echo" in /etc/nginx/conf.d/default.conf:11
nginx: configuration file /etc/nginx/nginx.conf test failed

4.7 重启服务使配置生效

systemctl restart nginx.service

4.8 测试输出

curl http://10.168.0.154

如果看到以下提示则达到预期:

Hello word!

==========================
参阅文档:
https://github.com/openresty/echo-nginx-module#echo

没有评论

发表回复

Nginx
如何解决https请求不安全http页被阻止问题?

1 前言 一个问题,一篇文章,一出故事。 笔者最近代理公司应用,发现https的页面有请求不安全的h …

Nginx
如何Nginx代理上游的子项目或文件夹?

1 前言 一个问题,一篇文章,一出故事。 笔者接到任务需要把Tomcat的其中一个应用号使用Ngin …

Nginx
如何安装部署RHEL 9 Nignx?

1 前言 一个问题,一篇文章,一出故事。 笔者需要基于RHEL 9部署Nginx环境,于是整理此文。 …