萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 作業系統 >  >> Linux 教學

Apache HTTP Server 核心配置檔 httpd.conf 詳細解析與效能調優

Apache 2.4 將所有配置集中於 httpd.conf (或 apache2.conf),採用模組化設計。理解核心指令與 MPM 選擇是效能調優關鍵。

核心配置區塊結構


# 全域環境
ServerRoot "/usr/local/apache2"
ServerAdmin admin@example.com
ServerName www.example.com:80
Listen 80
Listen 443

# MPM 模組 (僅能啟用一種)
# LoadModule mpm_prefork_module modules/mod_mpm_prefork.so
# LoadModule mpm_worker_module modules/mod_mpm_worker.so
LoadModule mpm_event_module modules/mod_mpm_event.so  # 推薦 2.4+

# 模組載入 (動態共享物件 DSO)
LoadModule authz_core_module modules/mod_authz_core.so
LoadModule ssl_module modules/mod_ssl.so
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule headers_module modules/mod_headers.so
LoadModule deflate_module modules/mod_deflate.so
LoadModule expires_module modules/mod_expires.so
LoadModule http2_module modules/mod_http2.so

# 主伺服器配置
User www-data
Group www-data
DocumentRoot "/var/www/html"

    AllowOverride None
    Require all denied


    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted


# 虛擬主機
IncludeOptional sites-enabled/*.conf

MPM 模型選擇與調優

MPM架構適用場景關鍵參數
prefork多進程、單執行緒非執行緒安全模組 (舊 PHP mod_php)StartServers, Min/MaxSpareServers, MaxRequestWorkers
worker多進程、多執行緒高併發、執行緒安全應用ThreadsPerChild, MaxRequestWorkers
event多進程、多執行緒 + 非同步事件現代 Web、Keep-Alive 密集、HTTP/2AsyncRequestWorkerFactor, ThreadsPerChild

Event MPM 推薦配置 (高併發)



    StartServers             4
    MinSpareThreads          25
    MaxSpareThreads          75
    ThreadsPerChild          25
    MaxRequestWorkers        400
    MaxConnectionsPerChild   10000
    AsyncRequestWorkerFactor 2
    ServerLimit              16

效能關鍵指令

  • KeepAlive On + MaxKeepAliveRequests 100 + KeepAliveTimeout 5
  • EnableSendfile On、EnableMMAP On (NFS 掛載需關閉)
  • HostnameLookups Off (避免 DNS 反查延遲)
  • DirectoryIndex index.html index.php
  • FileETag MTime Size (叢集需一致)

壓縮與快取 (mod_deflate, mod_expires, mod_headers)


# GZIP 壓縮

    AddOutputFilterByType DEFLATE text/html text/css application/javascript application/json application/xml
    DeflateCompressionLevel 6
    SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|woff2?)$ no-gzip


# 瀏覽器快取

    ExpiresActive On
    ExpiresByType image/* "access plus 1 year"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"


# 安全標頭

    Header always set X-Content-Type-Options nosniff
    Header always set X-Frame-Options SAMEORIGIN
    Header always set Referrer-Policy strict-origin-when-cross-origin
    Header always set Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline';"

SSL/TLS 現代化配置



    ServerName www.example.com
    SSLEngine on
    SSLCertificateFile /etc/ssl/certs/example.com.crt
    SSLCertificateKeyFile /etc/ssl/private/example.com.key
    SSLCertificateChainFile /etc/ssl/certs/ca-bundle.crt

    # Mozilla Intermediate 相容配置
    SSLProtocol all -SSLv3 -TLSv1 -TLSv1.1
    SSLCipherSuite ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384
    SSLHonorCipherOrder off
    SSLSessionTickets off
    SSLUseStapling on
    SSLStaplingCache "shmcb:logs/ssl_stapling(32768)"

    # HSTS
    Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"

安全強化清單

  • 隱藏版本:ServerTokens Prod、ServerSignature Off
  • 禁用目錄列表:Options -Indexes
  • 限制請求大小:LimitRequestBody 10485760 (10MB)
  • 防止點擊劫持、MIME 嗅探、XSS 過濾器 (上方 Headers)
  • ModSecurity WAF 整合:SecRuleEngine On
  • 限制 HTTP 方法: Require all denied

監控與日誌


LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %D" combinedio
CustomLog logs/access_log combinedio
ErrorLog logs/error_log
LogLevel warn

# 慢查詢日誌 (需 mod_log_config)
LogFormat "%h %l %u %t \"%r\" %>s %b %D" slow
SetEnvIf Request_URI "^/api/slow" slowlog
CustomLog logs/slow_log slow env=slowlog

掌握 httpd.conf 結構、MPM 參數計算、壓縮快取策略、TLS 現代配置與安全標頭,即可構建高效能、高安全性的 Apache 生產環境。

copyright © 萬盛學電腦網 all rights reserved