Nginx Header,实现对HTTP/S请求、响应进行添加、修改、删除等操作

通过Nginx内置

文档地址:

http://nginx.org/en/docs/http/ngx_http_headers_module.html

http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_hide_header

http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#fastcgi_hide_header

[可选]支持允许下划线Header

underscores_in_headers on;

添加Header

来源库:http_headers_module

add_header 'Key' 'values';

Syntax:

add_header name value [always];

Default:

Context:

http, server, location, if in location

例如:add_header 'Content-Type' 'text/html;charset=utf-8';

关于 add_header 无效不起作用,一般是没有在最后一条匹配规则中进行操作,需要将其加入到最深层的匹配规则中,例如location 比 server 深,if 比 location 深。

删除Header

来源库:ngx_http_fastcgi_module、ngx_http_proxy_module

fastcgi_hide_header 'Key';

Syntax:

fastcgi_hide_header field;

Default:

Context:

http, server, location

proxy_hide_header 'Key';

Syntax:

proxy_hide_header field;

Default:

Context:

http, server, location

例如:反向代理和fastcgi区分不同的场景使用。

fastcgi_hide_header X-Powered-By;

proxy_hide_header X-Powered-By;

修改Header

通过内置的操作,修改header分为两步,先将其删除再增加。

例如:

fastcgi_hide_header Content-Type;

proxy_hide_header Content-Type;

add_header 'Content-Type' 'text/css';

添加请求Header

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

fastcgi_param  'HTTP-X-Forwarded-For' $remote_addr;

删除请求Header

proxy_set_header X-Forwarded-For '';

fastcgi_param  'HTTP-X-Forwarded-For' '';

通过第三方模块

headers-more-nginx-module

添加/设置Header

syntax: more_set_headers [-t ]... [-s ]... ...

default: no

context: http, server, location, location if

phase: output-header-filter

more_set_headers "Server: yunjiasu-nginx";

清除Header

syntax: more_clear_headers [-t ]... [-s ]... ...

default: no

context: http, server, location, location if

phase: output-header-filter

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

more_clear_headers 'X-Hidden-*';

添加请求Header

more_set_input_headers

syntax: more_set_input_headers [-r] [-t ]... ...

default: no

context: http, server, location, location if

phase: rewrite tail

删除请求Header

more_clear_input_headers

syntax: more_clear_input_headers [-t ]... ...

default: no

context: http, server, location, location if

phase: rewrite tail

例子:

more_clear_input_headers -t 'text/plain' Foo Baz;

more_clear_input_headers "Foo" "Baz";

more_clear_input_headers 'X-Hidden-*';

综合案例

例如:

# 根据请求文件名,返回对应的 Content-Type

if ( $request_uri ~ .*\.(css)$ ) {

add_header 'Content-Type' 'text/css';

}

if ( $request_uri ~ .*\.(html|htm|php|php5)$ ) {

add_header 'Content-Type' 'text/html;charset=utf-8';

}

if ( $request_uri ~ .*\.(js)$ ) {

add_header 'Content-Type' 'application/javascript;application/x-javascript';

}

# 隐藏脚本版本及服务器版本信息

fastcgi_hide_header X-Powered-By;

fastcgi_hide_header Server;

# 伪造服务器应用版本信息

more_set_headers 'Server: yunjiasu-nginx/1.0';

# 允许跨域请求

add_header Access-Control-Allow-Origin *;

add_header Access-Control-Allow-Headers X-Requested-With;

add_header Access-Control-Allow-Methods GET,POST,OPTIONS;

nginx过滤post请求头_Nginx Header,实现对HTTP/S请求、响应进行添加、修改、删除等操作...相关推荐

  1. Chrome浏览器设置header请求 响应头 使用 Chrome ModHeader插件,添加/修改/删除HTTP请求标头和响应标头

    ModHeader,是一款可以添加/修改/删除请求标头和响应标头的浏览器插件 ModHeader插件功能 ModHeader插件支持添加/修改/删除请求标头和响应标头,并可以启用基于URL /资源类型 ...

  2. springcloud- FeginClient 调用统一拦截添加请求头 RequestInterceptor ,被调用服务获取请求头...

    使用场景: 在springcloud中通过Fegin调用远端RestApi的时候,经常需要传递一些参数信息到被调用服务中去,比如从A服务调用B服务的时候, 需要将当前用户信息传递到B调用的服务中去,我 ...

  3. 通过servlet来实现对Mysql进行连接、插入、修改、删除操作

    通过servlet来实现对Mysql进行连接.插入.修改.删除操作 来自:http://blog.sina.com.cn/s/blog_4bea2fb10100f3w3.html package Se ...

  4. OKHTTP系列(九)---http请求头(header)作用

    前言 在项目开发中,网络请求是必不可少的 ,在http方面的知识学习也是不能拉下的,这里就做一波http请求头的记录. Header:请求头个别参数和描述 Header 解释 示例 Accept 指定 ...

  5. 如何用谷歌(Chorme) 浏览器查看请求头(header)、响应头?

    目录 第一步:打开谷歌浏览器控制台 第二步:点击 Network 第三步:重新加载页面 第四步:查看请求头信息 第一步:打开谷歌浏览器控制台 右键点击页面,点击检查,控制台界面如下: 第二步:点击 N ...

  6. ajax jq 图片上传请求头_全面分析前端的网络请求方式:Ajax ,jQuery ,axios,fetch

    链接:https://juejin.im/post/5c9ac607f265da6103588b31 一.前端进行网络请求的关注点 大多数情况下,在前端发起一个网络请求我们只需关注下面几点: 传入基本 ...

  7. 添加请求头 retrofit_Python爬虫偷懒神器 — 快速构造请求头!

    我们在写爬虫构建请求的时候,不可避免地要添加请求头( headers ),一般来说,我们只要添加 user-agent 就能满足绝大部分需求了 但这并不是绝对的,有些请求单单添加一个 user-age ...

  8. 并发请求数_nginx如何限制并发连接和请求数?

    简介 限制并发连接数的模块为:http_limit_conn_module,地址:http://nginx.org/en/docs/http/ngx_http_limit_conn_module.ht ...

  9. python中scrapy加请求头_Python爬虫之scrapy框架随机请求头中间件的设置

    方法一,定义一个存放请求头的列表,并从中随机获取请求头: 获取请求头的网址http://www.useragentstring.com/pages/useragentstring.php?name=A ...

最新文章

  1. ORM对mysql数据库中数据进行操作报错解决
  2. 在Linux上取得Windows上的文件
  3. ZOC7 for Mac连接CentOS7无法输入中文问题
  4. php curl伪装cookies,php curl 添加cookie伪造登陆抓取数据
  5. linux可用机场客户端,Linux系统可用的6款Bittorrent客户端
  6. 机器学习 | 模型选择
  7. Git科普文,Git基本原理各种骚操作
  8. Go内存管理之代码的逃逸分析
  9. Markdown完整语法说明
  10. 答题获得思科T-shirt
  11. python 画风场 scipy_Python库之SciPy教程
  12. php 时间戳 24小时制,如何在php中添加24小时的unix时间戳?(How do I add 24 hours to a unix timestamp in php?)...
  13. 终于把所有的Python库,都整理出来啦!
  14. 计算机控制系统编程语言,可编程控制器常用的几种编程语言
  15. 自抗扰控制matlab仿真,自抗扰控制器的simulink建模与仿真.pdf
  16. 为“烦人的“QuickTime播放器设置快进,快退以及加速播放快捷键!!!
  17. 阿里云要引领数据库市场?这话没毛病
  18. MSP430G2-LaunchPad简明教程03[创建一个MSP430工程]
  19. combo接口(combo接口)
  20. 国科大学习资料--人工智能原理与算法-第四次作业解析(学长整理)

热门文章

  1. 如何将商业策略与项目管理相关联
  2. android安全攻防实践_Android安全攻防实战 PDF 下载
  3. RSA加解密用途简介及java示例
  4. 红米3国际稳定版刷机+完美ROOT
  5. 匹配率 php,PHP 正则后瞻 超强匹配是否是域名,准确率99%
  6. java线程池需要shutdown吗_公用线程池要不要shutdown?
  7. 线程池的使用与没使用的区别
  8. Linux中的Java类,Java基础入门学习-Java中类的属性
  9. linux下Oracle 相关命令
  10. linux7ip路由,Centos7源地址访问路由(双IP双网关配置)