Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> 電腦軟體教學 >> 伺服器技術 >> 關於伺服器

Linux Apache 進階配置實戰:環境變數判斷、防盜鏈、日誌切割與條件記錄

編輯:關於伺服器

核心模組:mod_setenvif、mod_log_config、cronolog

Apache 透過 SetEnvIf 系列指令以正則比對請求特徵設定環境變數,再搭配 CustomLog、Allow/Deny 實現條件存取控制、日誌記錄、盜鏈防護。

一、環境變數設定範例

1. 標記特定路徑請求

# 不分大小寫比對 Request_URI 開頭為 /aslibra
SetEnvIfNoCase Request_URI "^/aslibra" IS_START=1

2. 排除靜態資源日誌 (減少 I/O、噪音)

# 針對常見靜態副檔名設定 NOLOG=1
SetEnvIfNoCase Request_URI "\.(gif|jpg|jpeg|png|css|js|ico|woff|woff2)$" NOLOG=1

# 日誌指令排除 NOLOG=1 的請求
CustomLog logs/access_log combined env=!NOLOG

3. 條件記錄特定路徑至專屬日誌

SetEnvIfNoCase Request_URI "^/aslibra" IS_START=1
CustomLog "|/usr/local/sbin/cronolog /var/log/httpd/%Y/%m/%d/aslibra.%Y%m%d%H.log" combined env=IS_START

二、防盜鏈實作 (基於 Referer)

白名單模式:僅允許指定域名引用


# 允許自有域名與 localhost
SetEnvIfNoCase Referer "^https?://(www\.)?mydomain\.com/" LOCAL_REF=1
SetEnvIfNoCase Referer "^https?://127\.0\.0\.1/" LOCAL_REF=1


    Order Allow,Deny
    Allow from env=LOCAL_REF
    # 可選:拒絕時回傳預設圖片
    # ErrorDocument 403 /images/hotlink-denied.png

黑名單模式:封鎖惡意域名


SetEnvIfNoCase Referer "^https?://bad\.domain\.com/" BAD_REF=1


    Order Allow,Deny
    Allow from all
    Deny from env=BAD_REF

進階:空 Referer 策略 (直接輸入 URL/書籤/某些代理)


# 允許空 Referer (直接訪問)
SetEnvIf Referer "^$" ALLOW_EMPTY_REF=1


    Order Allow,Deny
    Allow from env=LOCAL_REF
    Allow from env=ALLOW_EMPTY_REF

三、日誌切割與歸檔:cronolog 最佳實務

安裝


# CentOS/RHEL
yum install cronolog
# Debian/Ubuntu
apt-get install cronolog
# 原始碼編譯
./configure --prefix=/usr/local && make && make install

日誌格式定義 (httpd.conf)


LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent

# 自訂格式:含 Cookie、精確時間
LogFormat "%h %{YYYY-MM-dd'T'HH:mm:ss}t \"%r\" %{SESSIONID}C" detailed

管道式日誌寫入 (自動按年/月/日/時分割)


# 主站綜合日誌
CustomLog "|/usr/local/sbin/cronolog /var/log/httpd/main/%Y/%m/%d/access.%Y%m%d%H.log" combined

# 特定應用條件日誌
SetEnvIfNoCase Request_URI "^/api/" IS_API=1
CustomLog "|/usr/local/sbin/cronolog /var/log/httpd/api/%Y/%m/%d/api.%Y%m%d%H.log" combined env=IS_API

# 錯誤日誌同樣可切割
ErrorLog "|/usr/local/sbin/cronolog /var/log/httpd/error/%Y/%m/%d/error.%Y%m%d%H.log"

logrotate 配合壓縮保留 (替代方案)


/var/log/httpd/*/*.log {
    daily
    missingok
    rotate 90
    compress
    delaycompress
    notifempty
    create 640 root adm
    sharedscripts
    postrotate
        /bin/systemctl reload httpd > /dev/null 2>&1 || true
    endscript
}

四、效能與安全加固建議

  • 關閉不必要模組:a2dismod autoindex status cgi userdir 等
  • 限制請求大小:LimitRequestBody 10485760 (10MB)
  • 隱藏版本資訊:ServerTokens Prod、ServerSignature Off
  • 啟用 HTTP/2:Protocols h2 http/1.1 (需 SSL)
  • MPM 事件模式:mpm_event_module + ThreadLimit/ThreadsPerChild 調優
  • 快取策略:mod_expires、mod_headers 設定靜態資源長期快取、ETag 調整
Copyright © Windows教程網 All Rights Reserved