標籤:
http://blog.csdn.net/btbtd/article/details/288027#2
# 分類:
# 01.常規設定
# 02.虛擬機器主機
# 03.<Directory> + Alias
# 04..htaccess
# 05.Log
# 06.URL Rewrite
# 07.其他
########01.常規設定
# Listen
# 監聽連接埠, 例: Listen 80
# ServerRoot
# Apache 所在目錄, 例: ServerRoot "C:/Program Files/Apache Group/Apache2" (WINDOWS)
# ServerAdmin
# 錯誤報表郵箱, 例: ServerAdmin [email protected]
# DocumentRoot
# 網站根目錄, 例: DocumentRoot "I:/gi-2288"
# DirectoryIndex
# 配置目錄索引檔案,
# 例: DirectoryIndex index.php index.php3 index.html index.htm
# ServerName
# 伺服器網域名稱/IP地址, 例: ServerName gi.2288.org
# DefaultLanguage
# 為所有指令範圍範圍內的檔案設定某一特定的預設語言
# 文法: DefaultLanguage MIME-lang, 例: DefaultLanguage zh-CN
# AddDefaultCharset
# 在沒有進行指定字元集的情況下向回應中加入的預設字元集, 預設值: AddDefaultCharset Off
# 例: AddDefaultCharset utf-8, 文法: 文法: AddDefaultCharset On|Off|字元集
# AddHandler
# 在副檔名與特定的處理器間建立映射, 例: AddHandler cgi-script .cgi .pl
# 文法: AddHandler handler-name extension
# AccessFileName
# 定義設定檔, 例: AccessFileName .htaccess
# 當向用戶端返迴文檔時,如果設定了此目錄的發行設定檔功能。
# 伺服器將在這個文檔的各個路徑中從名字列表中尋找第一個存在的設定檔。
# Timeout
# 逾時時間, 例: Timeout 300
# TypesConfig
# 指定mime.types檔案的位置, 預設: TypesConfig conf/mime.types
# DefaultType
# DefaultType MIME類型,
# 有時會發生這樣的事:伺服器會被要求提供一個文檔,而這個文檔的類型無法由它的MIME類型映射所決定。
# 伺服器必須通知用戶端其文檔的內容類型。
# 所以當一個未知類型出現時,將會使用DefaultType。
# 例: DefaultType image/gif
# HostnameLookups
# 啟用對用戶端IP的DNS尋找, 文法: HostnameLookups on|off|double
# ErrorLog
# 定位服務器存放錯誤記錄檔的位置, 例: ErrorLog "G:/Apache/dummy-gi.2288.org.80-error_log"
# LogLevel
# 控制錯誤記錄檔的層級, 預設值: LogLevel warn
# emerg 緊急 - 系統無法使用。
# alert 必須立即採取措施。
# crit 致命情況。
# error 錯誤情況。
# warn 警告情況。
# notice 一般重要情況。
# info 普通訊息。
# debug 出錯層級資訊
# CustomLog
# 設定日誌的檔案名稱和格式, 預設: CustomLog logs/access.log common
# ServerTokens
# 設定管理員HTTP回應頭, 預設: ServerTokens Full
# ServerTokens Prod[uctOnly], 伺服器會發送(比如說):Server:Apache
# ServerTokens Major, 伺服器會發送(比如說):Server:Apache/2
# ServerTokens Minor, 伺服器會發送(比如說):Server:Apache/2.0
# ServerTokens Min[imal], 伺服器會發送(比如說):Server:Apache/2.0.41
# ServerTokens OS, 伺服器會發送(比如說):Server: Apache/2.0.41 (Unix)
# ServerTokens Full (or not specified),
# 伺服器會發送(比如說):Server: Apache/2.0.41 (Unix) PHP/4.2.2 MyMod/1.2
# MaxSpareServers
# 設定apache的最大空閑進程數
# 文法: MaxSpareServers number
# 預設: MaxSpareServers 100
# 例: MaxSpareServers 1000
# MaxKeepAliveRequests
# 設定每個串連的最大請求數
# 文法: MaxKeepAliveRequests number
# 預設值: MaxKeepAliveRequests 100
# ServerSignature
# 設定管理員產生頁面的頁尾, 預設值: ServerSignature Off
# 文法: ServerSignature On|Off|EMail
# 應用:
# Listen 80
# ServerRoot "C:/Program Files/Apache Group/Apache2"
# ServerAdmin [email protected]
# DocumentRoot "I:/gi-2288"
# DirectoryIndex index.php index.php3 index.html index.htm
# ServerName gi.2288.org
# DefaultLanguage zh-CN
# AddDefaultCharset utf-8
# AddDefaultCharset Off
# AddHandler cgi-script .cgi .pl
# AccessFileName .htaccess
# Timeout 300
# TypesConfig conf/mime.types
# ErrorLog "G:/Apache/dummy-gi.2288.org.80-error_log"
# LogLevel warn
# CustomLog logs/access.log common
# ServerTokens Prod[uctOnly]
# ServerSignature Off
########02. <VirtualHost *:80> 虛擬機器主機
# Listen 80
# 設定監聽連接埠
# NameVirtualHost 192.168.1.2:80
# 名字/連接埠型 虛擬機器主機
# <VirtualHost *:88>
# </VirtualHost>
# 虛擬機器主機段
# ServerAdmin [email protected]
# 聯絡管理會用的信箱
# DocumentRoot "I:/gi-2288"
# 網站根目錄
# ServerName gi.2288.org
# 網域名稱
# ErrorLog "G:/Apache/dummy-gi.2288.org.80-error_log"
# 錯誤記錄檔 # 日誌記錄是好東西,注意查看日誌記錄
# ------------以上是虛擬機器主機內容
# 完整應用:
# Listen 80
# NameVirtualHost 192.168.1.2:80
# <VirtualHost *:88>
# ServerAdmin [email protected]
# DocumentRoot "I:/gi-2288"
# ServerName gi.2288.org
# </VirtualHost>
########03. <Directory> + Alias
# 文法: <Directory 目錄路徑> ... </Directory>
# 封裝一組指令,使之僅對具有某個名字的檔案系統目錄及其子目錄起作用。
# 不允許瀏覽目錄, 例:
# Alias /document "J:/Document/"
# <Directory "J:/Document/">
# Options FollowSymLinks
# AllowOverride none
# Order allow,deny
# Allow from all
# </Directory>
# 允許瀏覽目錄, 例:
# Alias /document "J:/Document/"
# <Directory "J:/Document/">
# Options Indexes
# AllowOverride none
# Order allow,deny
# Allow from all
# </Directory>
# Allow & Deny
# Allow和Deny指令可以允許或拒絕來自特定主機名稱或主機地址的訪問,
# 同時,Order指令告訴Apache處理這兩個指令的順序, 以改變過濾器。
# Allow
# 允許所有地址的訪問, 例: Allow from all
# 只允許特定IP地址訪問目錄, 例: Allow from 205.252.46.165
# 只允許特定網域名稱地址訪問目錄, 例: Allow from www.google.com
# 只允許特定IP 段訪問, 例: Allow from 10.10.10.0/255.255.0.0
# Deny
# 拒絕所有地址訪問目錄, 例: Deny from all
# 拒絕特定網域名稱訪問目錄, 例: Deny from www.google.com
# 拒絕特定IP訪問目錄, 例: Deny from 218.15.84.152
# 拒絕某個IP段訪問目錄, 例: Deny from 10.10.10.0/255.255.0.0
# Allow & Deny 混合使用
# 拒絕所有地址的訪問, 但接受 Google 的訪問, 例:
# <Directory "J:/Document/">
# Options FollowSymLinks
# Order Deny, Allow
# Deny from all
# Allow from www.google.com
# </Directory>
# 接受所有地址的訪問, 但拒絕 Google 的訪問, 例:
# <Directory "J:/Document/">
# Options FollowSymLinks
# Order Deny, Allow
# Deny from www.google.com
# Allow from all
# </Directory>
# Options
# 文法: Options [+|-]可選項 [[+|-]可選項] ...
# 配置在特定目錄使用哪些特性,
# Options指令控制了在特定目錄中將使用哪些伺服器特性。
# 可選項能設定為None,在這種情況下,將不啟用任何額外特性。
# 或設定為以下選項中的一個或多個:
# All, 除MultiViews之外的所有特性。這是預設設定。
# ExecCGI, 允許執行CGI指令碼.
# FollowSymLinks, 伺服器會在此目錄中使用符號串連。
# 注意:即便伺服器會使用符號串連,但它不會改變用於匹配<Directory>配置段的路徑名。
# 注意:如果此配置位於<Location>配置段中,則此設定會被忽略。
# Includes, 允許伺服器端包含。
# IncludesNOEXEC,允許伺服器端包含,但禁用#exec命令和#exec CGI。
# 但仍可以從ScriptAliase目錄使用#include 虛擬CGI指令碼。
# Indexes, 如果一個映射到目錄的URL被請求,
# 而此目錄中又沒有DirectoryIndex(例如:index.html.zh-cn.gb2312),
# 那麼伺服器會返回一個格式化後的目錄列表。
# MultiViews,允許內容協商的多重視圖。
# SymLinksIfOwnerMatch, 伺服器僅在符號串連與其目的目錄或檔案擁有者具有同樣的使用者id時才使用它。
# 注意:如果此配置出現在<Location>配置段中,此選項將被忽略。
# AllowOverride
# 文法: AllowOverride All|None|指令類型 [指令類型] ...
# 說明: 允許存在於.htaccess檔案中的指令類型
# 當伺服器發現了一個.htaccess檔案(由AccessFileName指定)時,
# 它需要知道在這個檔案中聲明的哪些指令能覆蓋在此之前指定的訪問資訊。
# AllowOverride僅在<Directory>小節中才是有效。在<Location>或<Files>小節中都是無效的。
# 如果此指令設定為None,那麼.htaccess檔案將被完全忽略。
# 在這種情況下,伺服器甚至都不會試著從檔案系統讀取.htaccess檔案。
# 當此指令設定為All時,所有具有.htaccess 內容相關的指令都允許出現在.htaccess檔案中。
########4. .htaccess
# 提示:允許使用 .htaccess 檔案將會導致伺服器效能的下降。
# 另外,每次請求一個頁面時,都需要讀取.htaccess檔案。
# 開啟 .htaccess 支援必須 這樣設定 AllowOverride all, 例:
# Alias /v "J:/pwd/"
# <Directory "J:/pwd/">
# Options Indexes
# AllowOverride all
# Order allow,deny
# Allow from all
# </Directory>
# 如果網路站台上有些敏感資訊或只希望為一個小群體所訪問,
# 這裡介紹幾個方法確保使使用者只能訪問允許被訪問的資源。
# 通常在 <Directory> 段或針對單個目錄的設定檔(.htaccess files)。
# 如果希望使用 .htaccess 檔案, 則必須設定伺服器以允許在這些檔案中使用認證指令,
# 即,用 AllowOverride 指令 指定哪些指令在針對單個目錄的設定檔中有效。
# 用密碼來保護伺服器上的目錄, 首先需要建立一個密碼檔案。
# 這個檔案可以放在不能被網路訪問的位置,以避免被下載,
# 例如: G:/Apache 以外的空間不能被網路訪問,
# 那麼可以考慮把密碼檔案放在 G:/Apached 目錄中。
# Apache在其安裝目錄的 bin 子目錄中提供了叫 htpasswd 的工具,
# 以建立密碼檔案,可以這樣使用:
# (先切換到Apache 安裝目錄, 例: C:/Program Files/Apache Group/Apache2/bin)
# 在命令列輸入: htpasswd -c G:/Apache/passwords E
# (這樣就建立了一個使用者 E, 具體按自己需要設定使用者和密碼)
# 接著會要求你輸入密碼, 然後再輸入一次,檢驗密碼.
# 這樣在 G:/Apache/ 就多了一個 passwords 檔案(沒有尾碼名)
# Windows 下的話會用 MD5 加密密碼.
# 然後在要 設定許可權的目錄裡建立一個 .htaccess 檔案,
# .htaccess 檔案的內容是:
# AuthType Basic
# AuthName "Restricted Files"
# AuthUserFile G:/Apache/passwords
# Require user E
# 示範頁面: http://gi.2288.org:88/v
# 使用者: E 密碼:testhtpasswd
# 注: 目錄必須允許 AllowOverride all ,這樣 .htaccess 檔案才會生效.
########5. Log
# Apache 的記錄檔可以提供非常多的資訊, 很有必要瞭解這些日誌
# C:/Program Files/Apache Group/Apache2/logs (Windows 中的記錄檔存放處)
# access.log(訪問日誌,瞭解用戶端所訪問的內容) & error.log(錯誤記錄檔)
# 自訂日誌格式
# mod_log_config
# 狀態: Base
# 說明: 將對伺服器發起的請求記錄到日誌
# 預設: CustomLog logs/access.log common
# 推薦設定:
# CustomLog logs/access_log "%h %l %u %t "%r" %s %b "%{Referer}i" "%{User-Agent}i""
# LogFormat
# 預設值: LogFormat "%h %l %u %t /"%r/" %>s %b"
# 文法: LogFormat format|nickname [nickname]
# 說明: 定義記錄檔裡的記錄格式
# 例: LogFormat "%v %h %l %u %t /"%r/" %>s %b" vhost_common
# CustomLog
# 狀態: Base
# 文法: CustomLog file|pipe format|nickname [env=[!]environment-variable]
# 說明: 設定日誌的檔案名稱和格式
# 例: CustomLog logs/access_log "%h %l %u %t /"%r/" %>s %b"
# ErrorLog
# 文法: ErrorLog 檔案路徑|syslog[:facility]
# 預設值: ErrorLog logs/error_log (Unix) ErrorLog logs/error.log (Windows and OS/2)
# 說明: 定位服務器存放錯誤記錄檔的位置
# 例: ErrorLog "G:/Apache/dummy-gi.2288.org.88-error_log"
# CookieLog
# 文法: CookieLog filename
# 說明: 設定針對cookies的記錄檔名
# 模組: mod_log_config
# RewriteLog
# 文法: RewriteLog file-path
# 說明: 設定重寫引擎日誌的檔案名稱
# 模組: mod_rewrite
# 例: RewriteLog "/usr/local/var/apache/logs/rewrite.log" (Unix/Linux)
# RewriteLog "G:/Apache/rewrite.log" (Windows)
# 一些常見的格式串如下所示:
# 能用日誌格式(CLF): "%h %l %u %t /"%r/" %>s %b"
# 帶虛擬機器主機的通用日誌格式: "%v %h %l %u %t /"%r/" %>s %b"
# NCSA擴充/組合日誌格式: "%h %l %u %t /"%r/" %>s %b /"%{Referer}i/" /"%{User-agent}i/""
# Referer日誌格式: "%{Referer}i -> %U"
# Agent (Browser)日誌格式: "%{User-agent}i"
########6.URL Rewrite
# 注意:根據你的伺服器配置,可能有必要對例子作些微修改,
# 比如,新啟用 mod_alias 和 mod_userdir 時要增加[PT]標誌,
# 或者重寫 .htaccess 而不是單個伺服器中的規則集。
# 對一個特定的規則集應該首先去理解而後再去用以避免出問題。
# 重寫文法
# RewriteEngine on
# RewriteRule ^/$ /www/ [R]
# 一般重寫方法
# 重寫頁面:http://gi.2288.org:88/modules/news/index.php
# RewriteRule /modules/news/index.htm$ /modules/news/index.php
# 完成頁面: http://gi.2288.org:88/modules/news/index.htm
# 重寫頁面: http://gi.2288.org:88/modules/news/index.php?storytopic=2
# RewriteRule /modules/news/topic_(.*)/.htm$ /modules/news/index.php?storytopic=$1
# 完成頁面: http://gi.2288.org:88/modules/news/topic_2.htm
# 重寫頁面:http://gi.2288.org:88/index.php?storytopic=2start=10
# RewriteRule /topic(.*)-(.*)/.htm$ /index.php?storytopic=$1&start=$2
# 完成頁面: http://gi.2288.org:88/topic2-2.htm
# 注: 每增加一個重寫ID,必須累加$1
# 比如頁面: http://gi.2288.org:88/ct=2x=10y=20
# 可以這樣重寫:
# RewriteRule /ct(.*)-(.*)-(.*)/.htm$ /ct=$1x=$2y=$3
# 效果: http://gi.2288.org:88/ct2-10-20.htm
# 移動宿主目錄到不同的網站伺服器
# 說明:
# 通常,許多網管在建立一個新的網站伺服器時,都會有這樣的要求:
# 重新導向一個網站伺服器上的所有宿主目錄到另一個網站伺服器
# 方案:
# 很簡單,用mod_rewrite。在老的網站伺服器上重新導向所有的URL
# /~user/anypath到http://newserver/~user/anypath。
# RewriteEngine on
# RewriteRule ^/~(.+) http://newserver/~$1 [R,L]
# 依賴於瀏覽器的內容
# 說明:
# 至少對重要的頂級頁面,有時候有必要提供依賴於瀏覽器的最佳的內容,
# 即對最新的Netscape提供最大化的版本,對Lynx提供最小化的版本,
# 而對其他的瀏覽器則提供一個功能一般的版本。
# 方案:
# 對此,內容協商無能為力,因為瀏覽器不提供其那種形式的類型,
# 所以只能在HTTP頭"User-Agent"上想辦法。
# 以下規則集可以完成這個操作:
# 如果HTTP頭"User-Agent"以"Mozilla/3"開頭,
# 則頁面foo.html 被重寫為foo.NS.html ,而後重寫操作終止;
# 如果是"Lynx"或者版本號碼為1和2的"Mozilla",則重寫為foo.20.html ;
# 而其他所有的瀏覽器收到的頁面則是foo.32.html :
# RewriteCond %{HTTP_USER_AGENT} ^Mozilla/3.*
# RewriteRule ^foo/.html$ foo.NS [L]
# RewriteCond %{HTTP_USER_AGENT} ^Lynx/.* [OR]
# RewriteCond %{HTTP_USER_AGENT} ^Mozilla/[12].*
# RewriteRule ^foo/ $ foo.20 [L]
# RewriteRule ^foo/ $ foo.32 [L]
# 阻止Robots
# 說明:
# 如何阻止一個完全匿名的robot取得特定網路地區的頁面?
# 一個/robots.txt檔案可以包含若干"Robot Exclusion Protocol(robot排除協議)"的行,
# 但不足以阻止此類robot。
# 方案:
# 可以用一個規則集以拒絕對網路地區/~quux/foo/arc/
# (對一個很深的目錄地區進行列表可能會使伺服器產生很大的負載)的訪問。
# 還必須確保僅阻止特定的robot,就是說,僅僅阻止robot訪問主機是不夠的,
# 這樣會同時也阻止了使用者訪問該主機。為此,就需要對HTTP頭的User-Agent資訊作匹配。
# RewriteCond %{HTTP_USER_AGENT} ^NameOfBadRobot.*
# RewriteCond %{REMOTE_ADDR} ^123/.45/.67/.[8-9]$
# RewriteRule ^/~quux/foo/arc/.+ - [F]
# 防止盜鏈圖片
# 說明:
# 假設,http://gi.2288.org:88/myalbum/有一些內嵌圖片的頁面,
# 這些圖片很好,所以就有人用超鏈連到他們自己的頁面中了。
# 由於這樣徒然增加了我們的伺服器的流量,因此,我們不願意這種事情發生。
# 方案:
# 雖然,我們不能100%地保護這些圖片不被寫入別人的頁面,
# 但至少可以對發出HTTP Referer頭的瀏覽器加以限制。
# RewriteCond %{HTTP_REFERER} !^$
# RewriteCond %{HTTP_REFERER} !^http://gi.2288.org:88/myalbum/.*$ [NC]
# RewriteRule .*/.gif$ - [F]
# RewriteCond %{HTTP_REFERER} !^$
# RewriteCond %{HTTP_REFERER} !.*/foo-with-gif/.html$
# RewriteRule ^inlined-in-foo/.gif$ - [F]
########7.其他
# 禁止盜鏈
# SetEnvIfNoCase Referer "^http://gi.2288.org:88/" local_ref=1
# <FilesMatch ".(gif|jpg|png|jpeg|zip|rar|exe|swf|txt)">
# Order Allow,Deny
# Allow from env=local_ref
# </FilesMatch>
# 載入 PHP 5
# LoadModule php5_module c:/php/php5apache2.dll
# #AddModule mod_php4.c
# AddType application/x-httpd-php .php
# ScriptAlias /php/ "c:/php/"
# AddType application/x-httpd-php .php
# Action application/x-httpd-php "/php/php-cgi.exe"
# 以服務方式運行Apache for Windows
# 你可以像下面這樣將Apache安裝為Windows NT服務:apache -i -n "服務名"
# 要安裝一個使用特定配置的服務,安裝時指定設定檔:
# apache -i -n "服務名" -f "/my server/conf/my.conf"
# 要移除一個Apache服務,使用: apache -u -n "服務名"
# 注: 如果沒有指定服務名,預設地,將使用"Apache"。
# 一旦服務被安裝,你可以使用-n選項和其他選項一起訪問該服務的設定檔。
# 例如:測試一個服務的設定檔:apache -n "服務名" -t
# 啟動一個使用某服務組態檔的控制台Apache:apache -n "服務名"
# 控制服務方式的Apache
# 啟動、重啟及關閉/停止Apache服務:
# apache -n "服務名" -k start
# apache -n "服務名" -k restart
# apache -n "服務名" -k shutdown
# apache -n "服務名" -k stop
# 或使用NT本機命令NET來啟動和停止Apache服務,像這樣:
# NET START "服務名"
# NET STOP "服務名"
Apache 學習筆記(心得)