Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux技術

httpd.conf 配置詳解:三大區塊與核心指令全解析

Apache 的 httpd.conf 看起來嚇人,其實結構很清楚:整份檔案被切成三個部分——全局環境參數、「主」伺服器設定、虛擬主機。把這三層關係搞懂,剩下的就是查指令。

先記一個規則:設定檔裡的路徑若以「/」開頭(Windows 下如「d:/」)就是絕對路徑;否則會被視為相對於 ServerRoot。例如 ServerRoot 是 /usr/local/apache2 時,logs/foo.log 實際上是 /usr/local/apache2/logs/foo.log。

第一區:全局環境參數

這一區的設定影響整個 Apache 的行為,例如能處理多少並發請求。

# ServerRoot 指出伺服器保存設定、錯誤與日誌的根目錄
ServerRoot "/usr/local/apache2"

# 序列訪問的鎖檔必須放在本機磁碟
<IfModule !mpm_winnt.c>
<IfModule !mpm_netware.c>
#LockFile logs/accept.lock
</IfModule>
</IfModule>

# PidFile 記錄伺服器啟動行程號
PidFile logs/httpd.pid

# Timeout 收發前的逾時秒數
Timeout 300

# KeepAlive 是否允許持穩連接(每個連接多個請求)
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 15

若要把 ServerRoot 設在 NFS 或其他網路位置,務必先讀 LockFile 相關文件,否則會踩到很多問題。

MPM 與行程池大小

Apache 依平台與需求提供多種 MPM,各自的參數意義如下。

# prefork MPM
<IfModule prefork.c>
StartServers 5           # 啟動時的行程數
MinSpareServers 5        # 最少備用行程
MaxSpareServers 10       # 最多備用行程
MaxClients 150           # 允許的最大行程數
MaxRequestPerChild 0     # 每個行程的最大請求數
</IfModule>

# worker MPM
<IfModule worker.c>
StartServers 2
MaxClients 150
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestsPerChild 0
</IfModule>

# perchild MPM
<IfModule perchild.c>
NumServers 5
StartThreads 5
MinSpareThreads 5
MaxSpareThreads 10
MaxThreadsPerChild 20
MaxRequestsPerChild 0
</IfModule>

# WinNT MPM
<IfModule mpm_winnt.c>
ThreadsPerChild 250
MaxRequestsPerChild 0
</IfModule>

NetWare、OS/2、BeOS 的 MPM 參數大同小異,都是圍繞「行程數 × 每行程線程數」在調整。

Listen 與 DSO

# 讓 Apache 只在指定 IP 上監聽,避免在 0.0.0.0 上暴露
# Listen 12.34.56.78:80
Listen 80

# 以 DSO 模式編譯的模組需要相對應的 LoadModule 才會生效
LoadModule foo_module modules/mod_foo.so

# 呼叫 server-status 時是否產生完整狀態資訊
ExtendedStatus On

第二區:「主」伺服器配置

這一區的值回應所有未被 <VirtualHost> 處理的請求,同時作為後續虛擬主機的預設值;虛擬主機內若有定義則會覆蓋這裡。

# 若以 root 啟動,需在此指定要切換到的使用者與群組
User nobody
Group #-1

ServerAdmin kreny@sina.com
# ServerName 用於識別自身,建議顯式指定,否則重新導向可能失效
ServerName www.dalouis.com
UseCanonicalName Off

DocumentRoot "/home/redhat/public_html"

目錄權限:先關再開

Apache 的建議做法是先把整個檔案系統關掉,再逐一放行你真正要開的目錄:

<Directory />
  Order Deny,Allow
  Deny from all
</Directory>

<Directory "/home/redhat/public_html">
  # Options 可為 None、All,或下列組合:
  # Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
  # 注意 MultiViews 必須顯式指定,Options All 不含它
  Options FollowSymLinks

  # AllowOverride 控制 .htaccess 中可用的指令:All、None,
  # 或 Options FileInfo AuthConfig Limit 的組合
  AllowOverride None

  Order allow,deny
  Allow from all
</Directory>

如果 Apache 沒有如你預期地運作,第一個該檢查的就是這裡有沒有明確放行。

UserDir、DirectoryIndex 與 .htaccess

UserDir public_html
UserDir disabled root

DirectoryIndex index.php index.html index.html.var
AccessFileName .htaccess

# 防止 .htaccess 與 .htpasswd 被瀏覽器下載
<Files ~ "^\.ht">
  Order allow,deny
  Deny from all
</Files>

MIME 與主機名解析

TypeConfig conf/mime.types
DefaultType text/plain

<IfModule mod_mime_magic.c>
MIMEMagicFile conf/magic
</IfModule>

# 設為 Off 較好;設 On 每個請求都會多一次 nameserver 查詢
HostnameLookups Off

# 檔案系統在 NFS 上時建議關閉
EnableMMAP off
EnableSendfile off

日誌

ErrorLog logs/error_log
LogLevel warn

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
CustomLog logs/access_log common

# 需 mod_logio 才能使用 %I 與 %O
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio

日誌等級由輕到重為 debug、info、notice、warn、error、crit、alert、emerg。

ServerTokens 與 ServerSignature

# Full | OS | Minor | Minimal | Major | Prod,Full 資訊最多、Prod 最少
ServerTokens Full
ServerSignature On

Alias 與 ScriptAlias

格式為「Alias 別名 真實名」,別名結尾有「/」時真實名也必須有。

Alias /icons/ "/usr/local/apache2/icons/"
<Directory "/usr/local/apache2/icons">
  Options Indexes MultiViews
  AllowOverride None
  Order allow,deny
  Allow from all
</Directory>

Alias /manual "/usr/local/apache2/manual"
ScriptAlias /cgi-bin/ "/usr/local/apache2/cgi-bin/"

<Directory "/usr/local/apache2/cgi-bin">
  AllowOverride None
  Options None
  Order allow,deny
  Allow from all
</Directory>

PHP 支援的掛載方式:

AddType application/x-httpd-php .php
LoadModule php4_module modules/libphp4.so

目錄列表與 AddIcon

IndexOptions FancyIndexing VersionSort
AddIconByType (TXT,/icons/text.gif) text/*
AddIcon /icons/binary.gif .bin .exe
AddIcon /icons/folder.gif ^^DIRECTORY^^
DefaultIcon /icons/unknown.gif
ReadmeName README.html
HeaderName HEADER.html
IndexIgnore .??* *~ *# HEADER* README* RCS CVS *,v *,t
AddEncoding x-compress Z
AddEncoding x-gzip gz tgz

語言與字元集

DefaultLanguage 設定預設語言,AddLanguage 以「語言關鍵字 + 副檔名」對應;LanguagePriority 決定多選一時的優先順序,ForceLanguagePriority 則處理 Prefer 與 Fallback。

AddLanguage da .dk
AddLanguage en .en
AddLanguage zh-tw .tw
LanguagePriority en da nl et fr de el it ja ko no pl pt pt-br ltz ca es sv tw
ForceLanguagePriority Prefer Fallback

AddDefaultCharset GB2312
AddCharset UTF-8 .utf8

指定預設字元集永遠是好主意,它為網站的國際化打開了門;但也可能讓以 iso-8859-1 撰寫的頁面顯示異常,因此要留意。

AddType、AddHandler 與自訂錯誤頁

AddType application/x-tar .tgz
AddType image/x-icon .ico
AddHandler cgi-script .cgi
AddHandler type-map var

ErrorDocument 500 "The server made a boo boo."
ErrorDocument 404 /missing.html
ErrorDocument 404 "/cgi-bin/missing_handler.pl"

國際化錯誤頁需要 mod_alias、mod_include 與 mod_negotiation 三個模組:

Alias /error/ "/usr/local/apache2/error/"
<Directory "/usr/local/apache2/error">
  AllowOverride None
  Options IncludesNoExec
  AddOutputFilter Includes html
  AddHandler type-map var
  Order allow,deny
  Allow from all
  LanguagePriority en de es fr it nl sv
  ForceLanguagePriority Prefer Fallback
</Directory>

瀏覽器相容性與狀態頁

BrowserMatch "Mozilla/2" nokeepalive
BrowserMatch "MSIE 4\.0b2;" nokeepalive downgrade-1.0 force-response-1.0
BrowserMatch "RealPlayer 4\.0" force-response-1.0

<Location /server-status>
  SetHandler server-status
  Order deny,allow
  Deny from all
  Allow from .example.com
</Location>

<Location /server-info>
  SetHandler server-info
  Order deny,allow
  Deny from all
  Allow from .example.com
</Location>

第三區:虛擬主機

用 <VirtualHost> 容器可以在同一台主機上持有多個網域。多數指令都能寫在容器裡,而伺服器不必擔心 IP 的問題,因此常用星號代替:

NameVirtualHost *

<VirtualHost *>
  ServerAdmin webmaster@dummy-host.example.com
  DocumentRoot /www/docs/dummy-host.example.com
  ServerName dummy-host.example.com
  ErrorLog logs/dummy-host.example.com-error_log
  CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>

第一個虛擬主機區塊是用來回應「服務名未知」的請求,因此通常放預設站點。設定完成後,可用 httpd -S 檢查虛擬主機設定是否正確。

Copyright © Linux教程網 All Rights Reserved