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)
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 ~]#