more_clear_input_headers

简介:

官网https://github.com/openresty/headers-more-nginx-module#more_clear_input_headers

一. 介绍ngx_headers_more

ngx_headers_more 用于添加、设置和清除输入和输出的头信息。nginx源码没有包含该模块,需要另行添加。

该模块是ngx_http_headers_module模块的增强版,提供了更多的实用工具,比如复位或清除内置头信息,如Content-Type, Content-Length, 和Server。

可以允许你使用-s选项指定HTTP状态码,使用-t选项指定内容类型,通过more_set_headers 和 more_clear_headers 指令来修改输出头信息。如:

more_set_headers -s 404 -t 'text/html' 'X-Foo: Bar';

输入头信息也可以这么修改,如:

location /foo {

    more_set_input_headers 'Host: foo' 'User-Agent: faked';

    # now $host, $http_host, $user_agent, and

    #   $http_user_agent all have their new values.

}

-t选项也可以在more_set_input_headers和more_clear_input_headers指令中使用。

不像标准头模块,该模块的指示适用于所有的状态码,包括4xx和5xx的。 add_header只适用于200,201,204,206,301,302,303,304,或307。

标准头模块ngx_http_headers_module参见:《ngx_http_headers_module模块add_header和expires指令》

二. 安装ngx_headers_more

wget 'http://nginx.org/download/nginx-1.5.8.tar.gz'

tar -xzvf nginx-1.5.8.tar.gz

cd nginx-1.5.8/

# Here we assume you would install you nginx under /opt/nginx/.

./configure --prefix=/opt/nginx \

--add-module=/path/to/headers-more-nginx-module

make

make install

ngx_headers_more 包下载地址:http://github.com/agentzh/headers-more-nginx-module/tags

ngx_openresty包含该模块。

三. 指令说明

more_set_headers

语法:more_set_headers [-t ]… [-s ]… …

默认值:no

配置段:http, server, location, location if

阶段:输出报头过滤器

替换(如有)或增加(如果不是所有)指定的输出头时响应状态代码与-s选项相匹配和响应的内容类型的-t选项指定的类型相匹配的。

如果没有指定-s或-t,或有一个空表值,无需匹配。因此,对于下面的指定,任何状态码和任何内容类型都讲设置。

more_set_headers    "Server: my_server";

具有相同名称的响应头总是覆盖。如果要添加头,可以使用标准的add_header指令代替。

单个指令可以设置/添加多个输出头。如:

more_set_headers 'Foo: bar' 'Baz: bah';

在单一指令中,选项可以多次出现,如:

more_set_headers -s 404 -s '500 503' 'Foo: bar';

等同于:

more_set_headers -s '404 500 503' 'Foo: bar';

新的头是下面形式之一:

Name: Value

Name:

Name

最后两个有效清除的头名称的值。Nginx的变量允许是头值,如:

set $my_var "dog";

more_set_headers "Server: $my_var";

注意:more_set_headers允许在location的if块中,但不允许在server的if块中。下面的配置就报语法错误:

# This is NOT allowed!

 server {

       if ($args ~ 'download') {

          more_set_headers 'Foo: Bar';

       }

      ...

  }

more_clear_headers

语法:more_clear_headers [-t ]… [-s ]… …

默认值:no

配置段:http, server, location, location if

阶段:输出报头过滤器

清除指定的输出头

more_clear_headers -s 404 -t 'text/plain' Foo Baz;

等同于

more_set_headers -s 404 -t 'text/plain' "Foo: " "Baz: ";

more_set_headers -s 404 -t 'text/plain' Foo Baz

也可以使用通配符*,如:

more_clear_headers 'X-Hidden-*';

清除开始由“X-Hidden-”任何输出头。

more_set_input_headers

语法:more_set_input_headers [-r] [-t ]… …

默认值:no

配置段:http, server, location, location if

阶段: rewrite tail

非常类似more_set_headers,不同的是它工作在输入头(或请求头),它仅支持-t选项。

注意:使用-t选项的是过滤请求头的Content-Type,而不是响应头的。

去掉请求头

location / {

more_clear_input_headers "Accept-Encoding";
}

more_clear_input_headers

语法:more_clear_input_headers [-t ]… …

默认值:no

配置段:http, server, location, location if

阶段: rewrite tail

清除指定输入头。如:

more_clear_input_headers -s 404 -t 'text/plain' Foo Baz;

等同于

more_set_input_headers -s 404 -t 'text/plain' "Foo: " "Baz: ";

more_set_input_headers -s 404 -t 'text/plain' Foo Baz

四. ngx_headers_more局限性

1. 不同于标准头模块,该模块不会对下面头有效: Expires, Cache-Control, 和Last-Modified。

2. 使用此模块无法删除Connection的响应报头。唯一方法是更改src/ HTTP/ ngx_http_header_filter_module.c文件。

五. 使用ngx_headers_more

# set the Server output header

more_set_headers 'Server: my-server';

# set and clear output headers

location /bar {

    more_set_headers 'X-MyHeader: blah' 'X-MyHeader2: foo';

    more_set_headers -t 'text/plain text/css' 'Content-Type: text/foo';

    more_set_headers -s '400 404 500 503' -s 413 'Foo: Bar';

    more_clear_headers 'Content-Type';

# your proxy_pass/memcached_pass/or any other config goes here...

}

# set output headers

location /type {

    more_set_headers 'Content-Type: text/plain';  

# ...

}

# set input headers

location /foo {

    set $my_host 'my dog';

    more_set_input_headers 'Host: $my_host';

    more_set_input_headers -t 'text/plain' 'X-Foo: bah';

# now $host and $http_host have their new values...

# ...

}

# replace input header X-Foo *only* if it already exists

more_set_input_headers -r 'X-Foo: howdy';

六. 应用ngx_headers_more

修改web服务器是什么软件,什么版本,同时隐藏Centent-Type、Accept-Range、Content-Length头信息。

more_set_headers "Server: ttlsa.com Web Server";

more_clear_headers "Content-Type:";

more_clear_headers "Accept-Ranges: ";

more_clear_headers "Content-Length: ";

headers-more-nginx-module

404状态码添加header

配置如下:

more_set_headers "Server: ttlsa.com Web Server";

more_set_headers -s 404 "Error: Not found";

more_clear_headers "Content-Type:";

more_clear_headers "Accept-Ranges: ";

more_clear_headers "Content-Length: ";

headers-more-nginx-module


参考:

http://blog.sina.com.cn/s/blog_40e9ab360102x8qq.html

http://www.ttlsa.com/nginx/nginx-custom-header-to-return-information-module-ngx_headers_more/

https://github.com/openresty/headers-more-nginx-module#more_clear_input_headers



本文转自 Tenderrain 51CTO博客,原文链接:http://blog.51cto.com/tenderrain/1980030

相关文章
|
2月前
|
人工智能 弹性计算 JavaScript
【最新】OpenClaw多Agent实战指南:分工设计+阿里云/本地部署+百炼API配置+避坑指南
“模型越强大,系统越混乱”——这是2026年很多OpenClaw用户的共同痛点。随着AI模型能力的提升,越来越多的用户试图让单个Agent包揽写作、分析、配置、排障等所有任务,结果却陷入上下文混杂、职责失焦、输出风格漂移的困境:写作文案里掺着工程术语,技术排障报告夹杂文案表达,复杂任务的上下文越积越重,效率不升反降。
978 1
|
监控 Kubernetes Go
日志采集效能跃迁:iLogtail 到 LoongCollector 的全面升级
LoongCollector 在日志场景中实现了全面的重磅升级,从功能、性能、稳定性等各个方面均进行了深度优化和提升,本文我们将对 LoongCollector 的升级进行详细介绍。
836 87
|
11月前
|
JSON 算法 Java
打造终端里的下载利器:Python实现可恢复式多线程下载器
在数字时代,大文件下载已成为日常需求。本文教你用Python打造专业级下载器,支持断点续传、多线程加速、速度限制等功能,显著提升终端下载体验。内容涵盖智能续传、多线程分块下载、限速控制及Rich库构建现代终端界面,助你从零构建高效下载工具。
699 1
|
监控 NoSQL 算法
手把手教你如何搭建redis集群(二)
手把手教你如何搭建redis集群(二)
923 1
|
安全 Java API
阿里云短信介绍和购买流程和使用流程
联网时代短信的应用无处不在,如APP的注册,平时的短信通知等。 下面就由小编系统的讲解一下阿里云短信的购买和使用流程
|
Prometheus 监控 Cloud Native
prometheus学习笔记之cAdvisor
prometheus学习笔记之cAdvisor
|
缓存 Java 数据库连接
使用MyBatis缓存的简单案例
MyBatis 是一种流行的持久层框架,支持自定义 SQL 执行、映射及复杂查询。本文介绍了如何在 Spring Boot 项目中集成 MyBatis 并实现一级和二级缓存,以提高查询性能,减少数据库访问。通过具体的电商系统案例,详细讲解了项目搭建、缓存配置、实体类创建、Mapper 编写、Service 层实现及缓存测试等步骤。
247 2
|
存储 安全 算法
加盐哈希的科学原理及其重要性
【8月更文挑战第31天】
1226 0
|
机器学习/深度学习 人工智能 搜索推荐
详细探讨AI在个性化教育平台中学习路径推荐的应用
详细探讨AI在个性化教育平台中学习路径推荐的应用