Nginx 高级配置-第三方模块编译

              Nginx 高级配置-第三方模块编译

                                       作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

  第三模块是对nginx 的功能扩展,第三方模块需要在编译安装Nginx 的时候使用参数--add-module=PATH指定路径添加,有的模块是由公司的开发人员针对业务需求定制开发的,有的模块是开源爱好者开发好之后上传到github进行开源的模块,nginx支持第三方模块需要从源码重新编译支持,比如开源的echo模块 https://github.com/openresty/echo-nginx-module.

一.配置echo模块相关功能

1>.查看编译安装nginx的相关参数

[root@node101.yinzhengjie.org.cn ~]# nginx -V
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/yinzhengjie/softwares/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_modul
e --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@node101.yinzhengjie.org.cn ~]#

2>.主配置文件(生产环境中不建议更改主配置文件,因为在主配置文件中定义很多server会显得很臃肿,推荐在主配置文件中加载子配置文件将各个server分别放在不同的子配置文件中)

[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf/nginx.conf
worker_processes 4;
worker_cpu_affinity 00000001 00000010 00000100 00001000; events {
worker_connections 100000;
use epoll;
accept_mutex on;
multi_accept on;
} http {
include mime.types;
default_type application/octet-stream;
sendfile on;
gzip on;
charset utf-8; #最大缓存10000个文件,非活动数据超时时长60s
open_file_cache max=10000 inactive=60s;
#每间隔60s检查一下缓存数据有效性
open_file_cache_valid 60s;
#60秒内至少被命中访问5次才被标记为活动数据
open_file_cache_min_uses 5;
#缓存错误信息
open_file_cache_errors on; #隐藏Nginx server版本。
server_tokens off; #当文件大于等于给定大小时,同步(直接)写磁盘,而非写缓存。
directio 4m; #上传文件相关参数
client_max_body_size 10m;
client_body_buffer_size 16k;
client_body_temp_path /yinzhengjie/data/web/nginx/temp 1 2 2; #IE系列的浏览器禁用长连接,默认就是禁用了IE的长连接功能.
keepalive_disable msie6; #开启长连接后,返回客户端的会话保持时间为60s,单次长连接累计请求达到指定次数请求或65秒就会被断开,后面的60为发送给客户端应答
报文头部中显示的超时时间设置为60s:如不设置客户端将不显示超时时间。 keepalive_timeout 65 60; #在一次长连接上所允许请求的资源的最大数量
keepalive_requests 3; #导入其他路径的配置文件
include /yinzhengjie/softwares/nginx/conf.d/*.conf;
} [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]#

3>.编辑echo的相关配置

[root@node101.yinzhengjie.org.cn ~]# cat -n /yinzhengjie/softwares/nginx/conf.d/share.conf
1 server {
2 listen 80;
3 server_name node101.yinzhengjie.org.cn;
4
5 location / {
6 root /yinzhengjie/data/web/nginx/static;
7 index index.html;
8 }
9
10 location /nginx_status {
11 stub_status;
12 allow 172.30.1.108;
13 deny all;
14 }
15
16 location /hello {
17 echo "hello, world!";
18 }
19 }
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -t      #如下所述,目前的nginx压根就不认识echo指令,因此我们需要停掉nginx服务并编译支持echo指令的相关模块。
nginx: [emerg] unknown directive "echo" in /yinzhengjie/softwares/nginx/conf.d/share.conf:17
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test failed
[root@node101.yinzhengjie.org.cn ~]#

4>.将echo指令端注释并停止nginx服务,编译支持echo指令的相关模块

[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/share.conf
server {
listen 80;
server_name node101.yinzhengjie.org.cn; location / {
root /yinzhengjie/data/web/nginx/static;
index index.html;
} location /nginx_status {
stub_status;
allow 172.30.1.108;
deny all;
} #location /hello {
# echo "hello, world!";
#}
}
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 :::22 :::*
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -s stop
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 128 :::22 :::*
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

二.编译支持echo指令的相关模块

1>.查看支持echo指令的github地址(https://github.com/openresty/echo-nginx-module)

Nginx 高级配置-第三方模块编译

2>.使用git命令将github的项目克隆到本地

[root@node101.yinzhengjie.org.cn ~]# yum -y install git
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.huaweicloud.com
* extras: mirror.jdcloud.com
* updates: mirrors.huaweicloud.com
base | 3.6 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
Resolving Dependencies
--> Running transaction check
---> Package git.x86_64 0:1.8.3.1-20.el7 will be installed
--> Processing Dependency: perl-Git = 1.8.3.1-20.el7 for package: git-1.8.3.1-20.el7.x86_64
--> Processing Dependency: rsync for package: git-1.8.3.1-20.el7.x86_64
--> Processing Dependency: perl(Term::ReadKey) for package: git-1.8.3.1-20.el7.x86_64
--> Processing Dependency: perl(Git) for package: git-1.8.3.1-20.el7.x86_64
--> Processing Dependency: perl(Error) for package: git-1.8.3.1-20.el7.x86_64
--> Running transaction check
---> Package perl-Error.noarch 1:0.17020-2.el7 will be installed
---> Package perl-Git.noarch 0:1.8.3.1-20.el7 will be installed
---> Package perl-TermReadKey.x86_64 0:2.30-20.el7 will be installed
---> Package rsync.x86_64 0:3.1.2-6.el7_6.1 will be installed
--> Finished Dependency Resolution Dependencies Resolved =================================================================================================================================
Package Arch Version Repository Size
=================================================================================================================================
Installing:
git x86_64 1.8.3.1-20.el7 base 4.4 M
Installing for dependencies:
perl-Error noarch 1:0.17020-2.el7 base 32 k
perl-Git noarch 1.8.3.1-20.el7 base 55 k
perl-TermReadKey x86_64 2.30-20.el7 base 31 k
rsync x86_64 3.1.2-6.el7_6.1 base 404 k Transaction Summary
=================================================================================================================================
Install 1 Package (+4 Dependent packages) Total download size: 4.9 M
Installed size: 23 M
Downloading packages:
(1/5): perl-Git-1.8.3.1-20.el7.noarch.rpm | 55 kB 00:00:00
(2/5): perl-Error-0.17020-2.el7.noarch.rpm | 32 kB 00:00:00
(3/5): rsync-3.1.2-6.el7_6.1.x86_64.rpm | 404 kB 00:00:00
(4/5): perl-TermReadKey-2.30-20.el7.x86_64.rpm | 31 kB 00:00:00
(5/5): git-1.8.3.1-20.el7.x86_64.rpm | 4.4 MB 00:00:01
---------------------------------------------------------------------------------------------------------------------------------
Total 2.9 MB/s | 4.9 MB 00:00:01
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : 1:perl-Error-0.17020-2.el7.noarch 1/5
Installing : rsync-3.1.2-6.el7_6.1.x86_64 2/5
Installing : perl-TermReadKey-2.30-20.el7.x86_64 3/5
Installing : git-1.8.3.1-20.el7.x86_64 4/5
Installing : perl-Git-1.8.3.1-20.el7.noarch 5/5
Verifying : perl-Git-1.8.3.1-20.el7.noarch 1/5
Verifying : 1:perl-Error-0.17020-2.el7.noarch 2/5
Verifying : perl-TermReadKey-2.30-20.el7.x86_64 3/5
Verifying : git-1.8.3.1-20.el7.x86_64 4/5
Verifying : rsync-3.1.2-6.el7_6.1.x86_64 5/5 Installed:
git.x86_64 0:1.8.3.1-20.el7 Dependency Installed:
perl-Error.noarch 1:0.17020-2.el7 perl-Git.noarch 0:1.8.3.1-20.el7 perl-TermReadKey.x86_64 0:2.30-20.el7
rsync.x86_64 0:3.1.2-6.el7_6.1 Complete!
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# yum -y install git

[root@node101.yinzhengjie.org.cn ~]# cd /usr/local/src/
[root@node101.yinzhengjie.org.cn /usr/local/src]#
[root@node101.yinzhengjie.org.cn /usr/local/src]# ll
total 992
drwxr-xr-x 9 1001 1001 186 Dec 15 13:47 nginx-1.14.2
-rw-r--r-- 1 root root 1015384 Dec 4 2018 nginx-1.14.2.tar.gz
[root@node101.yinzhengjie.org.cn /usr/local/src]#
[root@node101.yinzhengjie.org.cn /usr/local/src]# git clone https://github.com/openresty/echo-nginx-module.git
Cloning into 'echo-nginx-module'...
remote: Enumerating objects: 15, done.
remote: Counting objects: 100% (15/15), done.
remote: Compressing objects: 100% (12/12), done.
remote: Total 3012 (delta 6), reused 7 (delta 3), pack-reused 2997
Receiving objects: 100% (3012/3012), 1.15 MiB | 473.00 KiB/s, done.
Resolving deltas: 100% (1617/1617), done.
[root@node101.yinzhengjie.org.cn /usr/local/src]#
[root@node101.yinzhengjie.org.cn /usr/local/src]# ll
total 992
drwxr-xr-x 6 root root 186 Dec 17 18:14 echo-nginx-module
drwxr-xr-x 9 1001 1001 186 Dec 15 13:47 nginx-1.14.2
-rw-r--r-- 1 root root 1015384 Dec 4 2018 nginx-1.14.2.tar.gz
[root@node101.yinzhengjie.org.cn /usr/local/src]#
[root@node101.yinzhengjie.org.cn /usr/local/src]#
[root@node101.yinzhengjie.org.cn /usr/local/src]# ll echo-nginx-module/
total 76
-rw-r--r-- 1 root root 3182 Dec 17 18:14 config
-rw-r--r-- 1 root root 1345 Dec 17 18:14 LICENSE
-rw-r--r-- 1 root root 54503 Dec 17 18:14 README.markdown
drwxr-xr-x 2 root root 4096 Dec 17 18:14 src
drwxr-xr-x 2 root root 4096 Dec 17 18:14 t
drwxr-xr-x 2 root root 55 Dec 17 18:14 util
-rw-r--r-- 1 root root 986 Dec 17 18:14 valgrind.suppress
[root@node101.yinzhengjie.org.cn /usr/local/src]#
[root@node101.yinzhengjie.org.cn /usr/local/src]# ll echo-nginx-module/src/          #很明显,都是C语言相关文件
total 184
-rw-r--r-- 1 root root 2494 Dec 17 18:14 ddebug.h
-rw-r--r-- 1 root root 8248 Dec 17 18:14 ngx_http_echo_echo.c
-rw-r--r-- 1 root root 758 Dec 17 18:14 ngx_http_echo_echo.h
-rw-r--r-- 1 root root 7689 Dec 17 18:14 ngx_http_echo_filter.c
-rw-r--r-- 1 root root 311 Dec 17 18:14 ngx_http_echo_filter.h
-rw-r--r-- 1 root root 4859 Dec 17 18:14 ngx_http_echo_foreach.c
-rw-r--r-- 1 root root 458 Dec 17 18:14 ngx_http_echo_foreach.h
-rw-r--r-- 1 root root 11397 Dec 17 18:14 ngx_http_echo_handler.c
-rw-r--r-- 1 root root 383 Dec 17 18:14 ngx_http_echo_handler.h
-rw-r--r-- 1 root root 4440 Dec 17 18:14 ngx_http_echo_location.c
-rw-r--r-- 1 root root 380 Dec 17 18:14 ngx_http_echo_location.h
-rw-r--r-- 1 root root 19729 Dec 17 18:14 ngx_http_echo_module.c
-rw-r--r-- 1 root root 4169 Dec 17 18:14 ngx_http_echo_module.h
-rw-r--r-- 1 root root 12268 Dec 17 18:14 ngx_http_echo_request_info.c
-rw-r--r-- 1 root root 1207 Dec 17 18:14 ngx_http_echo_request_info.h
-rw-r--r-- 1 root root 4628 Dec 17 18:14 ngx_http_echo_sleep.c
-rw-r--r-- 1 root root 435 Dec 17 18:14 ngx_http_echo_sleep.h
-rw-r--r-- 1 root root 22008 Dec 17 18:14 ngx_http_echo_subrequest.c
-rw-r--r-- 1 root root 612 Dec 17 18:14 ngx_http_echo_subrequest.h
-rw-r--r-- 1 root root 2122 Dec 17 18:14 ngx_http_echo_timer.c
-rw-r--r-- 1 root root 336 Dec 17 18:14 ngx_http_echo_timer.h
-rw-r--r-- 1 root root 6557 Dec 17 18:14 ngx_http_echo_util.c
-rw-r--r-- 1 root root 1533 Dec 17 18:14 ngx_http_echo_util.h
-rw-r--r-- 1 root root 2685 Dec 17 18:14 ngx_http_echo_var.c
-rw-r--r-- 1 root root 155 Dec 17 18:14 ngx_http_echo_var.h
[root@node101.yinzhengjie.org.cn /usr/local/src]#

[root@node101.yinzhengjie.org.cn /usr/local/src]# ll echo-nginx-module/

3>.编译安装nginx让其支持echo指令的(参考文档:https://github.com/openresty/echo-nginx-module#installation)

[root@node101.yinzhengjie.org.cn /usr/local/src]# nginx -V          #将配置参数的每一项都复制下来,一会要用来重新编译,这些参数最好都要一模一样。
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/yinzhengjie/softwares/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stu
b_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module --add-module=/usr/local/src/echo-nginx-module
[root@node101.yinzhengjie.org.cn /usr/local/src]#
[root@node101.yinzhengjie.org.cn /usr/local/src]#
[root@node101.yinzhengjie.org.cn /usr/local/src]# cd nginx-1.14.2/
[root@node101.yinzhengjie.org.cn /usr/local/src/nginx-1.14.2]#
[root@node101.yinzhengjie.org.cn /usr/local/src/nginx-1.14.2]# ./configure --prefix=/yinzhengjie/softwares/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 --add-module=/usr/local/src/echo-nginx-module
......
[root@node101.yinzhengjie.org.cn /usr/local/src/nginx-1.14.2]# echo $?
0
[root@node101.yinzhengjie.org.cn /usr/local/src/nginx-1.14.2]#
[root@node101.yinzhengjie.org.cn /usr/local/src/nginx-1.14.2]# make -j 4
......
[root@node101.yinzhengjie.org.cn /usr/local/src/nginx-1.14.2]# echo $?
0
[root@node101.yinzhengjie.org.cn /usr/local/src/nginx-1.14.2]#
[root@node101.yinzhengjie.org.cn /usr/local/src/nginx-1.14.2]# make install
......
[root@node101.yinzhengjie.org.cn /usr/local/src/nginx-1.14.2]# echo $?
0
[root@node101.yinzhengjie.org.cn /usr/local/src/nginx-1.14.2]#

三.继续配置第三方模块echo指令

1>.修改子配置文件,将之前注释的echo指令代码取消,检查语法通过,说明echo指令的相关模块编译成功

[root@node101.yinzhengjie.org.cn ~]# vim /yinzhengjie/softwares/nginx/conf.d/share.conf
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/share.conf
server {
listen 80;
server_name node101.yinzhengjie.org.cn; location / {
root /yinzhengjie/data/web/nginx/static;
index index.html;
} location /nginx_status {
stub_status;
allow 172.30.1.108;
deny all;
} location /hello {
echo "hello, world!";
}
}
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]#

2>.启动nginx

[root@node101.yinzhengjie.org.cn ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 128 :::22 :::*
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 :::22 :::*
[root@node101.yinzhengjie.org.cn ~]#

3>.浏览器访问定义echo指令的相关URL,发现文件被下载下来了(因为我们没有指定相关的MIME类型),如下图所示

Nginx 高级配置-第三方模块编译

4>.继续修改nginx的子配置文件,并指定默认的MIME类型为文本类型

[root@node101.yinzhengjie.org.cn ~]# vim /yinzhengjie/softwares/nginx/conf.d/share.conf
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/share.conf
server {
listen 80;
server_name node101.yinzhengjie.org.cn; location / {
root /yinzhengjie/data/web/nginx/static;
index index.html;
} location /nginx_status {
stub_status;
allow 172.30.1.108;
deny all;
} location /hello {
default_type text/html;
echo "hello, world!";
}
}
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -s reload
[root@node101.yinzhengjie.org.cn ~]#

5>.指定默认的MIME类型后,再次访问"http://node101.yinzhengjie.org.cn/hello",如下图所示。

Nginx 高级配置-第三方模块编译

四.在一个location中调用其它location的案例

1>.编辑子配置文件

[root@node101.yinzhengjie.org.cn ~]# vim /yinzhengjie/softwares/nginx/conf.d/share.conf
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/share.conf
server {
listen 80;
server_name node101.yinzhengjie.org.cn; location / {
root /yinzhengjie/data/web/nginx/static;
index index.html;
} location /nginx_status {
stub_status;
allow 172.30.1.108;
deny all;
} location /main {
index index.html;
default_type text/html;
echo "hello world,main-->";
echo_reset_timer;
echo_location /sub1;
echo_location /sub2;
echo "took $echo_timer_elapsed sec for total.";
} location /sub1 {
echo_sleep 1;
echo sub1;
} location /sub2 {
echo_sleep 1;
echo sub2;
} }
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]#

2>.重新加载配置文件

[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root 9297 1 0 18:30 ? 00:00:00 nginx: master process nginx
nginx 9372 9297 0 18:36 ? 00:00:00 nginx: worker process
nginx 9373 9297 0 18:36 ? 00:00:00 nginx: worker process
nginx 9374 9297 0 18:36 ? 00:00:00 nginx: worker process
nginx 9375 9297 0 18:36 ? 00:00:00 nginx: worker process
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -s reload
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root 9297 1 0 18:30 ? 00:00:00 nginx: master process nginx
nginx 9405 9297 5 18:46 ? 00:00:00 nginx: worker process
nginx 9406 9297 5 18:46 ? 00:00:00 nginx: worker process
nginx 9407 9297 6 18:46 ? 00:00:00 nginx: worker process
nginx 9408 9297 9 18:46 ? 00:00:00 nginx: worker process
[root@node101.yinzhengjie.org.cn ~]#

3>.浏览器访问"http://node101.yinzhengjie.org.cn/main",最终结果如下图所示

Nginx 高级配置-第三方模块编译

上一篇:Nginx 高级配置-变量使用


下一篇:跟我一起用node-express搭建一个小项目[一]