• Nginx隐藏主机信息,proxy_hide_header 与fastcgi_hide_header


    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 的下载地址请点这里

     

  • 相关阅读:
    043_生成随机密码
    042_提示用户输入年份后测试判断是否为闰年
    041_查找 Linux 系统中的僵尸进程
    040_删除某个目录下大小为 0 的文件
    039_显示 CPU 厂商信息
    038_使用脚本自动创建逻辑卷
    037_自动添加防火墙规则,开启某些服务或端口(适用于 RHEL7)
    bzoj1537
    bzoj2466
    bzoj1047
  • 原文地址:https://www.cnblogs.com/linkenpark/p/7283177.html
Copyright © 2020-2023  润新知