Apache 及開啓壓縮及Header信息隱藏

      昨天老大說:“看一個網站的管理員專業不專業就看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 images 
  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 image/gif "access plus 1 months" 
  32.     ExpiresByType image/x-icon "access plus 1 month" 
  33.     ExpiresByType image/jpeg "access plus 1 months" 
  34.     ExpiresByType image/jpg "access plus 1 months" 
  35.     ExpiresByType image/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
  •  
  •  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章