提醒

您正在访问搜索引擎投递页面,强烈建议您:回到主站 查看原文 - headers-more-nginx-module

memo 编辑 最后修改时间 2023/07/26 17:42

headers-more-nginx-module

nginx动态编译新模块 --添加 headers-more-nginx-module 模块 --在 nginx 已经安装的基础上,编译安装新的模块

安装

查看当前nginx编译信息

$ nginx -V
nginx version: nginx/1.20.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
built with OpenSSL 1.1.1k  25 Mar 2021
TLS SNI support enabled
configure arguments: --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_sub_module --with-stream --with-stream_ssl_module --with-openssl=/root/lnmp1.8/src/openssl-1.1.1k --with-openssl-opt='enable-weak-ssl-ciphers'

进入 nginx 所在目录,创建 module目录并拉取 headers-more-nginx-module 模块仓库

$ cd /usr/local/nginx/
$ mkdir module
$ cd module/
$ git clone https://github.com/openresty/headers-more-nginx-module.git
$ cd headers-more-nginx-module/
$ pwd
/usr/local/nginx/module/headers-more-nginx-module

进入之前安装 nginx 的源码目录,我们用的lnmp集成,所以进入 lnmp 当时的 nginx 目录

$ cd /root/lnmp1.8/src
$ tar -zxf openssl-1.1.1k.tar.gz
$ tar -zxf nginx-1.20.1.tar.gz
$ cd nginx-1.20.1

在之前 编译安装的相关参数后追加需要动态编译新的模块 --add-dynamic-module=/usr/local/nginx/module/headers-more-nginx-module ,并进行编译

$ ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module \
$ --with-http_gzip_static_module --with-http_sub_module --with-stream --with-stream_ssl_module \
$ --with-openssl=/root/lnmp1.8/src/openssl-1.1.1k --with-openssl-opt='enable-weak-ssl-ciphers' \
$ --add-dynamic-module=/usr/local/nginx/module/headers-more-nginx-module

$ make
// 注意!!! 执行完毕 make 命令后, 千万不要执行 make install 否则就真的把原先安装的 nginx 覆盖了!包括之前的配置信息等等,会全部覆盖!!!

替换已存在的nginx二进制可执行文件 & 复制已经编译好的 ngx_http_headers_more_filter_module.so 至 nginx/module 目录

$ cd objs
$ mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.d
$ cp ./nginx /usr/local/nginx/sbin/nginx
$ cp ./ngx_http_headers_more_filter_module.so /usr/local/nginx/module/headers-more-nginx-module/
// 验证
$ nginx -t

修改 nginx.conf 配置文件,加载新的模块

$ cd /usr/local/nginx/conf/
$ vim nginx.conf
// 在 nginx.conf 配置文件中写入内容
# 第11行: worker_rlimit_nofile 51200; 后面添加:
# 加载headers-more-nginx-module模块
load_module /usr/local/nginx/module/headers-more-nginx-module/ngx_http_headers_more_filter_module.so;

修改响应头&链路TraceID

$ vim nginx.conf
// 找到http配置行,编辑内容:
#-----------------------------------------------begin
http
    {
        more_set_headers 'X-Node:11.61' 'Server:JSX/1.0' 'X-Powered-By:ASP.NET' 'X-Trace-Id:$request_id';
        more_set_input_headers 'X-Trace-Id:$request_id';
        map $http_token $token_code {
            default -;
            "~*(.*) ." $1;
        }
        map $request_method $normal {
            OPTIONS 0;
            default 1;
        }
        log_format  xeslog  '{"trace_id":"$request_id",'
                            '"token_code":"$token_code",'
                            '"@timestamp":"$time_iso8601",'
                            '"host":"$server_addr",'
                            '"remote_addr":"$remote_addr",'
                            '"remote_user":"$remote_user",'
                            '"request":"$request",'
                            '"status":"$status",'
                            '"request_method": "$request_method",'
                            '"size":"$body_bytes_sent",'
                            '"request_time":"$request_time",'
                            '"upstreamtime":"$upstream_response_time",'
                            '"upstreamhost":"$upstream_addr",'
                            '"http_host":"$host",'
                            '"url":"$uri",'
                            '"http_forward":"$http_x_forwarded_for",'
                            '"referer":"$http_referer",'
                            '"agent":"$http_user_agent"}';
#-----------------------------------------------end
#在渔民解析配置中使用
#----------------------------------------------begin
server {
    listen       443 ssl;
    server_name  wiki.jsx6.com;
    ...
    error_log  /var/log/xeslog/wiki.error.log;
    access_log  /var/log/xeslog/wiki.access.log xeslog if=$normal;
    ...
}
#----------------------------------------------end
2 条评论
未登录用户
支持 Markdown 语法
@sxin0
sxin0发表于将近 2 年前

six six six

@acane77
acane77发表于超过 1 年前

打卡