nginx 添加echo模块,实现在nginx中根据请求规则打印日志
提示:所有操作都是在root
用户下完成的。
前提:
- 已经安装了nginx,如果未安装,请先安装nginx,
yum install nginx -y
- 安装编译模块所需的依赖
yum install gcc -y
yum install pcre-devel openssl-devel -y
安装echo模块
-
下载echo-nginx-module。 下载地址
-
输入命令 nginx -V 查看yum安装的nginx版本信息,会输出对应版本号以及编译信息
nginx -V
输出结果:
3. 然后下载对应版本nginx的tar.gz的压缩包
下载地址
4. 将下载好的 nginx 和 echo-nginx-module 模块通过工具上传到虚拟机上,然后cp到 /usr/local/src 目录下,解压
解压完成以后进入nginx的解压目录
再进行下一步之前,最好把你 /usr/sbin/nginx 备份一下
5. 配置参数
输入命令:./configure --prefix=/usr/local/nginx --add-module=/usr/local/src/echo-nginx-module-0.61
再在后面加上配置参数
就是nginx -V输出的configure arguments
后面的,添加到后面,最终命令就是下面所示:
./configure --prefix=/usr/local/nginx --add-module=/usr/local/src/echo-nginx-module-0.61 --prefix=/usr/share/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 --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_auth_request_module --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'
- 编译
make -j2
- 复制Nginx
[root@VM_168_52_centos nginx-1.12.2]# pwd
/usr/local/src/nginx-1.12.2
[root@VM_168_52_centos nginx-1.12.2]# cd /usr/local/src/nginx-1.12.2
[root@VM_168_52_centos nginx-1.12.2]# ll
total 740
drwxr-xr-x 6 1001 1001 4096 Apr 25 17:34 auto
-rw-r--r-- 1 1001 1001 278202 Oct 17 2017 CHANGES
-rw-r--r-- 1 1001 1001 423948 Oct 17 2017 CHANGES.ru
drwxr-xr-x 2 1001 1001 4096 Apr 25 17:34 conf
-rwxr-xr-x 1 1001 1001 2481 Oct 17 2017 configure
drwxr-xr-x 4 1001 1001 4096 Apr 25 17:34 contrib
drwxr-xr-x 2 1001 1001 4096 Apr 25 17:34 html
-rw-r--r-- 1 1001 1001 1397 Oct 17 2017 LICENSE
-rw-r--r-- 1 root root 313 Apr 25 17:35 Makefile
drwxr-xr-x 2 1001 1001 4096 Apr 25 17:34 man
drwxr-xr-x 4 root root 4096 Apr 25 17:36 objs
-rw-r--r-- 1 1001 1001 49 Oct 17 2017 README
drwxr-xr-x 9 1001 1001 4096 Apr 25 17:34 src
[root@VM_168_52_centos nginx-1.12.2]# cd objs/
[root@VM_168_52_centos objs]# ll
total 11532
drwxr-xr-x 3 root root 4096 Apr 25 17:35 addon
-rw-r--r-- 1 root root 25441 Apr 25 17:35 autoconf.err
-rw-r--r-- 1 root root 73218 Apr 25 17:35 Makefile
-rwxr-xr-x 1 root root 8412742 Apr 25 17:36 nginx
-rw-r--r-- 1 root root 5306 Apr 25 17:36 nginx.8
-rw-r--r-- 1 root root 9468 Apr 25 17:35 ngx_auto_config.h
-rw-r--r-- 1 root root 657 Apr 25 17:35 ngx_auto_headers.h
-rw-r--r-- 1 root root 1273 Apr 25 17:35 ngx_http_geoip_module_modules.c
-rw-r--r-- 1 root root 78472 Apr 25 17:36 ngx_http_geoip_module_modules.o
-rwxr-xr-x 1 root root 138969 Apr 25 17:36 ngx_http_geoip_module.so
-rw-r--r-- 1 root root 1294 Apr 25 17:35 ngx_http_image_filter_module_modules.c
-rw-r--r-- 1 root root 78480 Apr 25 17:36 ngx_http_image_filter_module_modules.o
-rwxr-xr-x 1 root root 155799 Apr 25 17:36 ngx_http_image_filter_module.so
-rw-r--r-- 1 root root 1270 Apr 25 17:35 ngx_http_perl_module_modules.c
-rw-r--r-- 1 root root 78472 Apr 25 17:36 ngx_http_perl_module_modules.o
-rwxr-xr-x 1 root root 185012 Apr 25 17:36 ngx_http_perl_module.so
-rw-r--r-- 1 root root 1291 Apr 25 17:35 ngx_http_xslt_filter_module_modules.c
-rw-r--r-- 1 root root 78472 Apr 25 17:36 ngx_http_xslt_filter_module_modules.o
-rwxr-xr-x 1 root root 167928 Apr 25 17:36 ngx_http_xslt_filter_module.so
-rw-r--r-- 1 root root 962 Apr 25 17:35 ngx_mail_module_modules.c
-rw-r--r-- 1 root root 77944 Apr 25 17:36 ngx_mail_module_modules.o
-rwxr-xr-x 1 root root 739709 Apr 25 17:36 ngx_mail_module.so
-rw-r--r-- 1 root root 7720 Apr 25 17:35 ngx_modules.c
-rw-r--r-- 1 root root 89984 Apr 25 17:36 ngx_modules.o
-rw-r--r-- 1 root root 2010 Apr 25 17:35 ngx_stream_module_modules.c
-rw-r--r-- 1 root root 79808 Apr 25 17:36 ngx_stream_module_modules.o
-rwxr-xr-x 1 root root 1177686 Apr 25 17:36 ngx_stream_module.so
drwxr-xr-x 9 root root 4096 Apr 25 17:35 src
[root@VM_168_52_centos objs]# cp nginx -f /usr/sbin/nginx
cp: overwrite ‘/usr/sbin/nginx’? y
[root@VM_168_52_centos objs]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@VM_168_52_centos objs]#
- 配置nginx.config
[root@VM_168_52_centos ~]# vim /etc/nginx/nginx.conf
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
echo 123;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
- 测试echo
[root@VM_168_52_centos objs]# service nginx start
Redirecting to /bin/systemctl start nginx.service
[root@VM_168_52_centos objs]# curl http://127.0.0.1
123
如果操作出现错误请参考nginx升级异常处理的错误解决部分