最近需要修改反向代理的标头,于是整理ngx_headers_more模块的使用方法。
ngx_headers_more模块用于设置和清除输入和输出标头
1 |
一、编译加载ngx_headers_more模块
1.1、准备Nginx安装包
1 2 |
wget http://nginx.org/download/nginx-1.16.1.tar.gz tar -xf nginx-1.16.1.tar.gz |
1.2、准备模块的编译安装包
1 2 3 4 5 6 |
mkdir ~/nginx-1.16.1/modules cd ~/nginx-1.16.1/modules wget https://codeload.github.com/openresty/headers-more-nginx-module/tar.gz/refs/tags/v0.34 -O headers-more-nginx-module-0.34.tar.gz tar -xf headers-more-nginx-module-0.34.tar.gz |
1.3、创建www用户
1 2 |
groupadd www useradd -g www -s /sbin/nologin -M www |
1.4、重新编译nginx
1 2 3 4 5 6 7 8 9 10 11 12 13 |
cd ~/nginx-1.22.1/ ./configure --prefix=/usr/local/nginx --user=www --group=www --prefix=/etc/nginx \ ##注意这里的目录和docker内的配置文件路径最好一致 --with-cc-opt=-O2 \ --with-http_realip_module \ --with-http_v2_module \ --with-http_stub_status_module \ --with-http_ssl_module \ --with-http_gzip_static_module \ --with-stream \ --with-stream_ssl_module \ --with-http_sub_module \ --add-module=./modules/headers-more-nginx-module-0.34 |
1.5、编译
1 |
make && make install |
1.6、测试查看模块是否安装成功
1 2 3 |
cd /usr/local/nginx ./sbin/nginx -t ./sbin/nginx -V |

1.7、使用范例
1 2 3 4 5 6 7 8 9 10 11 |
more_set_input_headers "Foo: bar"; #以上为设置“Foo”输入头的值为“bar”,如果需要设置多个输入头,可以使用如下指令, more_set_input_headers 'Foo: bar' 'Baz: bah'; #指定设置条件的状态码的方式有两种,以下两种方式是等价的 more_set_input_headers -t 'text/html' -t 'text/plain' 'Foo: bar'; more_set_input_headers -t 'text/html text/plain' 'Foo: bar'; #如果需要使用变量设置,请参阅如下范例, set $my_var "bar"; more_set_input_headers "Foo: $my_var"; |
- 本文固定链接: https://www.yoyoask.com/?p=12147
- 转载请注明: shooter 于 SHOOTER 发表