Nginx⾼级配置-第三⽅模块编译
Nginx ⾼级配置-第三⽅模块编译
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
⼀.配置echo模块相关功能
1>.查看编译安装nginx的相关参数
[root@ ~]# 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@ ~]#
2>.主配置⽂件(⽣产环境中不建议更改主配置⽂件,因为在主配置⽂件中定义很多server会显得很臃肿,推荐在主配置⽂件中加载⼦配置⽂件将各个server分别放在不同的⼦配置⽂件中)
[root@ ~]# cat /yinzhengjie/softwares/nginx/f
worker_processes 4;
worker_cpu_affinity 00000001 00000010 00000100 00001000;
events {
worker_connections 100000;
use epoll;
accept_mutex on;
multi_accept on;
}
http {
include pes;
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@ ~]#
[root@ ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/f syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/f test is successful
[root@ ~]#
3>.编辑echo的相关配置
[root@ ~]# cat -n /yinzhengjie/softwares/nginx/conf.f
1 server {
2 listen 80;
3 server_name ;
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@ ~]#
[root@ ~]# nginx -t #如下所述,⽬前的nginx压根就不认识echo指令,因此我们需要停掉nginx服务并编译⽀持echo指令的相关模块。
nginx: [emerg] unknown directive "echo" in /yinzhengjie/softwares/nginx/conf.f:17
nginx: configuration file /yinzhengjie/softwares/nginx/f test failed
[root@ ~]#
4>.将echo指令端注释并停⽌nginx服务,编译⽀持echo指令的相关模块
[root@ ~]# cat /yinzhengjie/softwares/nginx/conf.f
server {
listen 80;
server_name ;
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@ ~]#
[root@ ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/f syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/f test is successful
[root@ ~]#
[root@ ~]# 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@ ~]#
[root@ ~]# nginx -s stop
[root@ ~]#
[root@ ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 128 :::22 :::*
[root@ ~]#
[root@ ~]#
⼆.编译⽀持echo指令的相关模块
2>.使⽤git命令将github的项⽬克隆到本地
[root@ ~]# yum -y install git
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.huaweicloud
* extras: mirror.jdcloud
* updates: mirrors.huaweicloud
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
---> arch 1:0.17020-2.el7 will be installed
---> arch 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:
nginx 配置文件(1/5): perl-Git-1.8.3.arch.rpm | 55 kB 00:00:00
(2/5): perl-Error-0.arch.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.arch 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.arch 5/5
Verifying : perl-Git-1.8.3.arch 1/5
Verifying : 1:perl-Error-0.arch 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:
rsync.x86_64 0:3.1.2-6.el7_6.1
Complete!
[root@ ~]#
[root@ ~]# yum -y install git
[root@ ~]# cd /usr/local/src/
[root@ /usr/local/src]#
[root@ /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.
[root@ /usr/local/src]#
[root@ /usr/local/src]# git clone github/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@ /usr/local/src]#
[root@ /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.
[root@ /usr/local/src]#
[root@ /usr/local/src]#
[root@ /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@ /usr/local/src]#
[root@ /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@ /usr/local/src]#
[root@ /usr/local/src]# ll echo-nginx-module/
[root@ /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@ /usr/local/src]#
[root@ /usr/local/src]#
[root@ /usr/local/src]# cd nginx-1.14.2/
[root@ /usr/local/src/nginx-1.14.2]#
[root@ /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 --w ......
[root@ /usr/local/src/nginx-1.14.2]# echo $?
[root@ /usr/local/src/nginx-1.14.2]#
[root@ /usr/local/src/nginx-1.14.2]# make -j 4
......
[root@ /usr/local/src/nginx-1.14.2]# echo $?
[root@ /usr/local/src/nginx-1.14.2]#
[root@ /usr/local/src/nginx-1.14.2]# make install
......
[root@ /usr/local/src/nginx-1.14.2]# echo $?
[root@ /usr/local/src/nginx-1.14.2]#
三.继续配置第三⽅模块echo指令
1>.修改⼦配置⽂件,将之前注释的echo指令代码取消,检查语法通过,说明echo指令的相关模块编译成功
[root@ ~]# vim /yinzhengjie/softwares/nginx/conf.f
[root@ ~]#
[root@ ~]# cat /yinzhengjie/softwares/nginx/conf.f
server {
listen 80;
server_name ;
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@ ~]#
[root@ ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/f syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/f test is successful
[root@ ~]#
2>.启动nginx
[root@ ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 128 :::22 :::*
[root@ ~]#
[root@ ~]# nginx
[root@ ~]#
[root@ ~]# 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@ ~]#
3>.浏览器访问定义echo指令的相关URL,发现⽂件被下载下来了(因为我们没有指定相关的MIME类型),如下图所⽰
4>.继续修改nginx的⼦配置⽂件,并指定默认的MIME类型为⽂本类型
[root@ ~]# vim /yinzhengjie/softwares/nginx/conf.f
[root@ ~]#
[root@ ~]# cat /yinzhengjie/softwares/nginx/conf.f
server {
listen 80;
server_name ;
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@ ~]#
[root@ ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/f syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/f test is successful
[root@ ~]#
[root@ ~]# nginx -s reload
[root@ ~]#
5>.指定默认的MIME类型后,再次访问"/hello",如下图所⽰。
四.在⼀个location中调⽤其它location的案例
1>.编辑⼦配置⽂件
[root@ ~]# vim /yinzhengjie/softwares/nginx/conf.f
[root@ ~]#
[root@ ~]# cat /yinzhengjie/softwares/nginx/conf.f
server {
listen 80;
server_name ;
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@ ~]#
[root@ ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/f syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/f test is successful
[root@ ~]#
2>.重新加载配置⽂件
[root@ ~]# 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@ ~]#
[root@ ~]# nginx -s reload
[root@ ~]#
[root@ ~]# 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@ ~]#
3>.浏览器访问"/main",最终结果如下图所⽰
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论