昨天老大说:“看一个网站的管理员专业不专业就看header返回的信息”,当时我一头雾水。原来当客户端连接到Apache服务器的时候,Apache一般会返回服务器版本、非缺省模块等信息,会被***利用,例如:

 
  
  1. Connection  close 
  2. Content-Type    text/html; charset=UTF-8 
  3. Date    Wed, 29 Feb 2012 07:51:08 GMT 
  4. P3P CP="CAO PSA OUR" 
  5. Server  Apache/2.2.21 (Unix) 
  6. Transfer-Encoding   chunked 
  7. X-Powered-By    PHP/5.3.9 
解决方法:
你可以在Apache的配置文件里面作如下设置让它返回的关于服务器的信息减少到最少:
#修改httpd-default.conf 的如下内容,修改后header将取消X-Powered-By    PHP/5.3.9的显示 
  1. ServerTokens Prod    
  2. ServerSignature Off 

注意:这样设置以后Apache还会返回一定的服务器信息,比如:
Server: Apache
但是这个不会对服务器安全产生太多的影响,因为很多扫描软件是扫描的时候是不顾你服务器返回的头部信息的。你如果想把服务器返回的相关信息变成百度的一样:
Server    BWS/1.0
那么你就要去修改源码了。
具体方法如下:
一、修改Apache的几个源代码文件

 
  
  1. 修改: httpd-2.2.21/include/ap_release.h 
  2.  
  3. #define AP_SERVER_BASEVENDOR"这里填写开发组织名,例如:Microsoft Corp." 
  4.  
  5. #defineAP_SERVER_BASEPRODUCT"这里填写服务器软件名,例如:Microsoft-IIS" 
  6.  
  7. #defineAP_SERVER_MAJORVERSION "主版本,例如:5" 
  8.  
  9. #defineAP_SERVER_MINORVERSION "次版本,例如:0" 
  10.  
  11. #defineAP_SERVER_PATCHLEVEL "修正版本,例如:1" 
  12. 修改: httpd-2.2.21/os/os2/os.h 
  13. #define PLATFORM "这里填写操作系统的名称,例如:Win32" 

二、重新编译apache,添加压缩模块

 
  
  1. cd httpd-2.2.21 
  2. ./configure --prefix=/usr/local/apache --enable-so --enable-expires --enable-mime-magic --enable-threads --enable-rewrite --disable-env --disable-actions --disable-asis --disable-setenvif --disable-version --disable-userdir --disable-authz-groupfile --disable-authn-file --disable-authz-user --disable-include --disable-filter --disable-cgid --disable-cgi --enable-ssl --with-ssl --enable-setenvif --with-mpm=prefork --enable-headers=shared --enable-deflate=shared 
  3. make&&make install 

编辑httpd.conf加入如下内容

 
  
  1. LoadModule deflate_module modules/mod_deflate.so 
  2. LoadModule headers_module modules/mod_headers.so 
  3.  
  4. <IfModule mod_deflate.c> 
  5.     SetOutputFilter DEFLATE #开启压缩
  6. #不压缩的文件类型
  7.     SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary
  8.     SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
  9.     SetEnvIfNoCase Request_URI .(?:pdf|mov|avi|mp3|mp4|rm)$ no-gzip dont-vary 
  10.   #要压缩的文件类型
  11.     AddOutputFilterByType DEFLATE text/* 
  12.     AddOutputFilterByType DEFLATE application/x-httpd-php application/x-httpd-fastphp 
  13.  
  14.     # Netscape 4.x has some problems... 
  15.     BrowserMatch ^Mozilla/4 gzip-only-text/html 
  16.     # Netscape 4.06-4.08 have some more problems 
  17.     BrowserMatch ^Mozilla/4\.0[678] no-gzip 
  18.     # MSIE masquerades as Netscape, but it is fine 
  19.     BrowserMatch \bMSIE !no-gzip !gzip-only-text/html 
  20.     # Don't compress p_w_picpaths 
  21.     SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary 
  22.     # Make sure proxies don't deliver the wrong content 
  23.     Header append Vary User-Agent env=!dont-vary 
  24. </IfModule> 
  25. #以下内容控制浏览器缓存时间及header信息,
  26. <IfModule mod_expires.c> 
  27.     ExpiresActive on #浏览器缓存开启
  28.     #ExpiresDefault "access plus 1 month" #设置默认过期时间为1个月
  29.     #ExpiresByType text/html "access plus 1 months" #动态网站不推荐开启此项
  30. ExpiresByType text/css "access plus 1 months" 
  31.     ExpiresByType p_w_picpath/gif "access plus 1 months" 
  32.     ExpiresByType p_w_picpath/x-icon "access plus 1 month" 
  33.     ExpiresByType p_w_picpath/jpeg "access plus 1 months" 
  34.     ExpiresByType p_w_picpath/jpg "access plus 1 months" 
  35.     ExpiresByType p_w_picpath/png "access plus 1 months" 
  36.     EXpiresByType application/x-shockwave-flash "access plus 1 months" 
  37.     EXpiresByType application/javascript "access plus 1 months" 
  38.  
  39.     Header unset Pragma 
  40.     FileETag None 
  41.     Header unset ETag 
  42.     Header set Cache-Control "private" 
  43.     <FilesMatch "\.(js|css|ico|pdf|flv|jpg|jpeg|png|gif|mp3|mp4|swf)$"
  44.     #Header set Expires "Wen, 29 Feb 2012 14:14:00 GMT"
  45.     #Header set Cache-Control must-revalidate,post-check=0,pre-check=0 
  46.     Header unset Last-Modified 
  47.     </FilesMatch> 
  48. </IfModule> 

效果:

 
  
  1. Cache-Control   private 
  2. Connection  close 
  3. Content-Encoding    gzip 
  4. Content-Type    text/html; charset=UTF-8 
  5. Date    Wed, 29 Feb 2012 07:04:23 GMT 
  6. P3P CP="CAO PSA OUR" 
  7. Server  FBS 
  8. Transfer-Encoding   chunked 
  9. Vary    Accept-Encoding,User-Agent 

参考网址:

 
  
  • http://yolcy.blog.163.com/blog/static/105307937201022471913971/ 
  • http://hi.baidu.com/%C8%FD%BE%D6%CE%AA%B6%FE/blog/item/30dae1325363ed92a8018e5c.html
 
  
  •  
 
  
  •