Nginx中proxy_hide_header 与fastcgi_hide_header都可以隐藏主机头信息,两者在具体使用时还是有着一定的区别的。刚好业务使用的nginx反向代理在显示响应头时将后端机器的PHP版本显示了出来,虽说在用户体验和安全上并无大的影响,但是将PHP版本信息暴漏出来总是不妥的。借此机会,可以复习下proxy_hide_header 与fastcgi_hide_header的使用。

proxy_hide_header在ngx_http_proxy_module下,fastcgi_hide_header在ngx_http_fastcgi_module模块下,作用相同的但是作用的地方有一些区别。

当nginx作为反向代理时,也就是nginx转发请求后端其他webserver(例如nginx+apache)时,当我们想要隐藏后端webserver主机信息的时候,我们使用proxy_hide_header来屏蔽后端主机信息。

当nginx作为webserver时,也就是nginx直接在服务器上提供web服务(例如nginx+php/php-fpm)处理用户请求,当我们想要隐藏webserver主机信息的时候,我们使用fastcgi_hide_header来屏蔽当前主机信息(尤其是php中相关信息)。

我们当前nginx是作为反向代理来使用,在配置proxy_hide_header前,通过浏览器我们可以看到主机响应头中包含php版本信息(X-Powered-By: PHP/5.4.43),我们的目的就是将这个显示内容从响应头中去掉。

请求头:

Host: v.l.qq.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:43.0) Gecko/20100101 Firefox/43.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Cookie: cm_cookie=V1,110015&1&AQEB6fOaVNNwm3PnpE9-5fO6F1GuuckO58xf&151023&151023,10008&mVXmnXsXpWqtoVVosntqqmsWsnsZYqql=&AQEBOy5mIcgjMROV-G8UDto2xjn787qAVk0u&160130&160130,110069&60ccd2e7aab778&AQEBUrmfm9YUuR9a-uIl2zxzHICDkArByOTr&160130&160130; appuser=513E20192260A681; M_D=1; psessionid=b499db0e_1454552430_0_52917; psessiontime=1454552430
Connection: keep-alive
Cache-Control: max-age=0

响应头:

Connection: keep-alive
Content-Encoding: gzip
Content-Type: text/html; charset=UTF-8
Date: Thu, 04 Feb 2016 02:20:36 GMT
Transfer-Encoding: chunked
Vary: Accept-Encoding
X-Powered-By: PHP/5.4.43
 
根据官网说明,proxy_hide_header 可在http, server, location区段使用。

语法: proxy_hide_header field;
默认值: —
上下文: http, server, location
nginx默认不会将“Date”、“Server”、“X-Pad”,和“X-Accel-...”响应头发送给客户端。proxy_hide_header指令则可以设置额外的响应头,这些响应头也不会发送给客户端。相反的,如果希望允许传递某些响应头给客户端,可以使用proxy_pass_header指令。

一般nginx反向代理会配置很多站点,每个站点配置费时费力而且少有遗漏,主机信息还是会被泄露的。根据上面的说明,我们将proxy_hide_header 配置在http区段,如下所示:

http {
        server_tokens off;
        server_tag off;
        autoindex off;
        access_log off;
        include mime.types;
        default_type application/octet-stream;
        proxy_hide_header X-Powered-By;

server_names_hash_bucket_size 128;
        client_header_buffer_size 32k;
        large_client_header_buffers 4 32k;
        client_max_body_size 1000m;
        client_body_buffer_size 256k;

检查nginx配置文件语法:
/usr/local/nginx/sbin/nginx -t 或/etc/init.d/nginx check
重启nginx服务:
/etc/init.d/nginx restart

配置后的主机响应头信息:

Connection: keep-alive
Content-Encoding: gzip
Content-Type: text/html; charset=UTF-8
Date: Thu, 04 Feb 2016 02:50:16 GMT
Transfer-Encoding: chunked
Vary: Accept-Encoding
 
通过查看官网信息,如下所示

syntax: fastcgi_hide_header field;
default: —
context: http, server, location
By default, nginx does not pass the header fields “Status” and “X-Accel-...” from the response of the FastCGI server to a client. The fastcgi_hide_header directive sets additional fields that will not be passed. If, on the contrary, the passing of fields needs to be permitted, the fastcgi_pass_header directive can be used.
可以看到fastcgi_hide_header 和proxy_hide_header的用途是一样的,作用在FastCGI server模式中,而不是Proxy模式下。

总结:
fastcgi_hide_header 和proxy_hide_header的都可以用来隐藏主机信息,fastcgi_hide_header 在fastcgi模式下起作用,proxy_hide_header在proxy模式下起作用。同样,我们会发现ngx_http_proxy_module和ngx_http_fastcgi_module模块中有很多作用相同的模块。

更多Nginx相关教程见以下内容

CentOS 6.2实战部署Nginx+MySQL+PHP http://www.linuxidc.com/Linux/2013-09/90020.htm

使用Nginx搭建WEB服务器 http://www.linuxidc.com/Linux/2013-09/89768.htm

搭建基于Linux6.3+Nginx1.2+PHP5+MySQL5.5的Web服务器全过程 http://www.linuxidc.com/Linux/2013-09/89692.htm

CentOS 6.3下Nginx性能调优 http://www.linuxidc.com/Linux/2013-09/89656.htm

CentOS 6.3下配置Nginx加载ngx_pagespeed模块 http://www.linuxidc.com/Linux/2013-09/89657.htm

CentOS 6.4安装配置Nginx+Pcre+php-fpm http://www.linuxidc.com/Linux/2013-08/88984.htm

Nginx安装配置使用详细笔记 http://www.linuxidc.com/Linux/2014-07/104499.htm

Nginx日志过滤 使用ngx_log_if不记录特定日志 http://www.linuxidc.com/Linux/2014-07/104686.htm

Nginx 的详细介绍请点这里
Nginx 的下载地址请点这里

 

Nginx隐藏主机信息,proxy_hide_header 与fastcgi_hide_header的更多相关文章

  1. nginx隐藏server信息和版本信息

    1.隐藏版本信息 在nginx.conf里面添加 server_tokens off; 2.隐藏server信息 需要重新编译ngnix进入解压出来的nginx 源码目录 vi src/http/ng ...

  2. nginx 隐藏版本信息

    隐藏nginx头部  修改后的src/core/nginx.h ,代码如下:   /*  * Copyright (C) Igor Sysoev  * Copyright (C) Nginx, Inc ...

  3. nginx 隐藏版本号与WEB服务器信息

    nginx不仅可以隐藏版本信息,还支持自定义web服务器信息 先看看最终的隐藏结果吧 具体怎么实现呢,其实也很简单,请往下看 1 官网下载最新稳定版 wget http://nginx.org/dow ...

  4. 查看nginx | apache | php | tengine | tomcat版本的信息以及如何隐藏版本信息【转】

    转自: 查看nginx | apache | php | tengine | tomcat版本的信息以及如何隐藏版本信息 - 追马 - 51CTO技术博客http://lovelace.blog.51 ...

  5. Nginx修改版本信息或隐藏版本号

    一,隐藏版本号.首先说明,这个是某一方面隐藏,不是彻底隐藏.未隐藏之前查看nginx信息: 隐藏方法: 修改nginx.conf配置文件,在http { } 标签里边加入字段: server_toke ...

  6. Nginx优化之基本安全优化 (隐藏Nginx软件版本号信息,更改源码隐藏Nginx软件名及版本号,更改Nginx服务的默认用户)

    一,隐藏Nginx软件版本号信息 查看版本号 curl -I 192.168.0.220 HTTP/1.1 200 OK Server: nginx/1.6.2 #这里清晰的暴露了Web版本号(1.6 ...

  7. Linux(7)- Nginx.conf主配置文件、Nginx虚拟主机/访问日志/限制访问IP/错误页面优化、Nginx反向代理、Nginx负载均衡

    一.Nginx.conf主配置文件 Nginx主配置文件conf/nginx.conf是一个纯文本类型的文件,整个配置文件是以区块的形式组织的.一般,每个区块以一对大括号{}来表示开始与结束. 核心模 ...

  8. apache、php隐藏头信息的方法

    本文介绍下,在apache与php中隐藏头部信息的方法,有需要的朋友参考下. 一.apache隐藏头部信息 apache 的 httpd.conf 有两个配置可以控制是否显示服务器信息给用户.Serv ...

  9. nginx虚拟主机的配置

    nginx虚拟主机的配置 server { listen ; server_name 127.0.0.1; access_log off; root /var/www/html/; location ...

随机推荐

  1. Leetcode 94

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  2. 使用路径arc-七彩

    <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>    < ...

  3. IOS-底层数据结构

      Objective-C底层数据结构 类的数据结构 Class(指针) typedef struct objc_class *Class; /* 这是由编译器为每个类产生的数据结构,这个结构定义了一 ...

  4. OC MRC之autorelease问题(代码分析)

    // // main.m // 08-autorelease // // Created by apple on 13-8-9. // Copyright (c) 2013年 itcast. All ...

  5. O(logn)二叉树中的意义----高性能(四)

    转载地址:https://zhidao.baidu.com/question/239708227508660244.html?qbl=relate_question_2&word=%CA%B1 ...

  6. HashTable vs HashMap(三)

    HashTable的应用非常广泛,HashMap是新框架中用来代替HashTable的类,也就是说建议使用HashMap,不要使用HashTable. 可能你觉得HashTable很好用,为什么不用呢 ...

  7. STM32F103各PIN脚封装图

    1.36PIN 2.48PIN 3.64PIN 4.100PIN STM32ZET6详细pin脚图

  8. bzoj1602

    题解: 简单lca 然而我调了半小时QAQ lca的时候要判断0 代码: #include<bits/stdc++.h> using namespace std; ; ][N],num[N ...

  9. (C/C++学习笔记) 十六. 预处理

    十六. 预处理 ● 关键字typeof 作用: 为一个已有的数据类型起一个或多个别名(alias), 从而增加了代码的可读性. typedef known_type_name new_type_nam ...

  10. Linux IO模式-阻塞io、非阻塞io、多路复用io

    一 概念说明 在进行解释之前,首先要说明几个概念: - 用户空间和内核空间 - 进程切换 - 进程的阻塞 - 文件描述符 - 缓存 I/O 用户空间与内核空间 现在操作系统都是采用虚拟存储器,那么对3 ...