fail2ban 補遺設定跟允許google爬蟲

Brave Ye
系統管理員
Posts: 6294
Joined: 2025-11-08, 05:32
Location: Taiwan

fail2ban 補遺設定跟允許google爬蟲

Post by Brave Ye »

nano /etc/fail2ban/filter.d/nginx-phpbb-auth.conf
[Definition]
# 捕捉 phpBB3 登入與註冊頁面的 POST 嘗試行為
failregex = ^<HOST> - .*? \[.*?\] "POST /phpBB3/ucp\.php\?mode=login[^"]* HTTP/[0-9.]+" 302
^<HOST> - .*? \[.*?\] "POST /phpBB3/ucp\.php\?mode=register[^"]* HTTP/[0-9.]+" 302

ignoreregex =
nano /etc/fail2ban/filter.d/nginx-webapp-bf.conf
[Definition]
# 監控目的:攔截針對 WordPress 登入頁面的惡意 POST 爆破,以及對 XML-RPC 介面的試探
# 適用於標準 Nginx 記錄格式 (或帶有反向代理真實 IP 的格式)
failregex = ^<HOST> - .* "POST /wordpress/wp-login.php HTTP/.*" 200
^<HOST> - .* "POST /wordpress/xmlrpc.php HTTP/.*" 200

# 如果您的 WordPress 安裝在網站根目錄(例如:https://zine-tw.com),請改用下方這組規則:
# failregex = ^<HOST> - .* "POST /wp-login.php HTTP/.*" 200
# ^<HOST> - .* "POST /xmlrpc.php HTTP/.*" 200

ignoreregex =
nano /etc/fail2ban/filter.d/nginx-bot-crawler.conf
[Definition]
# 辨識 Nginx access.log 中被阻擋並回傳 403 Forbidden 的惡意爬蟲
failregex = ^<HOST> - .*? \[.*?\] ".*?" 403 [0-9]+

ignoreregex =
nano /etc/fail2ban/jail.local
[DEFAULT]
# 設定白名單,讓您自己的 IP 或家裡/公司的 IP 絕對不會被 Fail2ban 誤鎖
ignoreip = 127.0.0.1/8 ::1 123.241.40.195

# 基礎封鎖時間設定 (預設錯 5 次鎖 10 分鐘)
bantime = 10m
findtime = 5m
maxretry = 5

# 順應 Debian 13 環境:將原先的 iptables 改為現代化的 nftables 複合端口封鎖模式
banaction = nftables[type=multiport]

# ----------------------------------------
# Nginx 防護區塊 (與您的 Debian 13 / PHP 8.5 環境對接)
# ----------------------------------------

[nginx-http-auth]
enabled = true
port = http,https
logpath = /var/log/nginx/error.log

[nginx-botsearch]
enabled = true
port = http,https
logpath = /var/log/nginx/error.log
maxretry = 3

[nginx-limit-req]
enabled = true
port = http,https
logpath = /var/log/nginx/error.log

# 針對 WordPress 與 phpBB3 後台登入的暴力破解防護
[nginx-webapp-bf]
enabled = true
port = http,https
filter = nginx-webapp-bf
logpath = /var/log/nginx/access.log
maxretry = 5
findtime = 3m
bantime = 1h

# 惡意爬蟲/機器人防護
[nginx-bot-crawler]
enabled = true
port = http,https
filter = nginx-bot-crawler
logpath = /var/log/nginx/access.log
maxretry = 10
findtime = 1h
bantime = 1h

# 專屬 phpBB3 認證爆破防護(嚴格:錯 3 次直接鎖 7 天)
[nginx-phpbb-auth]
enabled = true
port = http,https
filter = nginx-phpbb-auth
logpath = /var/log/nginx/access.log
maxretry = 3
findtime = 6h
bantime = 7d

設定google 為nginx白名單
nano /etc/nginx/google-whitelist.conf
geo $is_google {
default no;
64.233.160.0/19 yes;
66.102.0.0/20 yes;
66.249.64.0/19 yes;
72.14.192.0/18 yes;
74.125.0.0/16 yes;
209.85.128.0/17 yes;
216.58.192.0/19 yes;
216.239.32.0/19 yes;
2001:4860:4000::/36 yes;
2607:f8b0:4000::/36 yes;
2a00:1450:4000::/36 yes;
2c0f:fb50:4000::/36 yes;
2001:19f0:7001::/48 yes;
}
nano /etc/nginx/nginx.conf
# =========================================================================
# 核心效能區段 (全域層級)
# =========================================================================
user www-data;

# 自動對接您的 12 核心 CPU
worker_processes auto;
worker_cpu_affinity auto;

pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

# 提高每個進程能打開的檔案描述符上限(Workerman WebSocket 長連線非常需要)
worker_rlimit_nofile 65535;

events {
# 12 核環境下,總共可支援 12 * 4096 = 49,152 個同時連線
worker_connections 4096;

# 讓核心盡可能同時接收多個新連線
multi_accept on;

# Linux 環境下最推薦的高效並發模型
use epoll;
}

# =========================================================================
# HTTP 核心優化區段 (應用層級)
# =========================================================================
http {
##
# 基礎網路傳輸優化
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
types_hash_max_size 2048;
server_tokens off; # 隱藏 Nginx 版本號,提升安全性

include /etc/nginx/mime.types;
default_type application/octet-stream;

##
# 超時時間配置 (兼顧網頁與 Workerman 長連線)
##
# 網頁瀏覽器的長連線維持時間,設 30 秒能快速釋放閒置連線,保護 15G 記憶體
keepalive_timeout 30;

# 允許單一長連線傳輸最多 500 個請求
keepalive_requests 500;

# 遮蔽沒反應的客戶端,釋放快取
reset_timedout_connection on;
client_body_timeout 15;
send_timeout 15;

##
# 快取與緩衝區優化 (關鍵:防止大型 Wiki 頁面與 WordPress 產生臨時硬碟讀寫)
##
# 配合您 PHP 允許 800M 上傳,將 Nginx 的限制也放大
client_max_body_size 855M;
client_body_buffer_size 128k;

# 放大標頭緩衝區,防止複雜網頁或多 Cookie 導致 400 Bad Request
client_header_buffer_size 4k;
large_client_header_buffers 4 16k;

##
# Gzip 壓縮優化 (大幅降低網頁傳輸頻寬,減輕 CPU 負擔)
##
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
# 設為 5 即可,太高(如 9)會瘋狂榨乾您的 CPU
gzip_comp_level 5;
gzip_min_length 256;
# 針對 CMS 論壇、Wiki 的各種靜態、動態資源進行壓縮
gzip_types
text/plain
text/css
text/xml
text/javascript
application/javascript
application/x-javascript
application/xml
application/xml+rss
application/json
image/svg+xml;

##
# 載入您的虛擬主機設定檔 (保持原樣)
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
include /etc/nginx/google-whitelist.conf;
}
修改nginx 網站設定檔
nano /etc/nginx/sites-available/zine-tw
# =========================================================================
# 區塊 1A: Port 80 網域名稱 (強制導向 HTTPS)
# =========================================================================
server {
listen 80;
server_name zine-tw.com www.zine-tw.com;

# ① 先擋惡意 IP
if ($bad_ip) { return 444; }

# ② 再擋 bot UA
set $should_block "";
if ($block_bot) {
set $should_block "Y";
}
if ($is_google = "yes") {
set $should_block "N"; # 如果是真·Google IP,強制洗成不阻擋
}
if ($should_block = "Y") {
return 444;
}

# ③ 速率限制
limit_req zone=botzone burst=20 nodelay;

# ④ 掃描器路徑靜默處理
location ~* /\.(git|svn|hg|bzr|env|aws|DS_Store) {
access_log off;
return 404;
}
location ~ /\.ht {
access_log off;
return 404;
}
location = /dns-query {
access_log off;
return 404;
}

location /.well-known/acme-challenge/ {
root /var/www/html;
}

location / {
return 301 https://$host$request_uri;
}

location = /404_deadlink {
internal;
return 404;
}
}


# =========================================================================
# 區塊 1B: Port 80 IP 直連 (中國/牆內用戶通道)
# =========================================================================
server {
listen 80 default_server;
server_name 123.241.40.195;

# ① 先擋惡意 IP
if ($bad_ip) { return 444; }

# ② 再擋 bot UA
set $should_block "";
if ($block_bot) {
set $should_block "Y";
}
if ($is_google = "yes") {
set $should_block "N"; # 如果是真·Google IP,強制洗成不阻擋
}
if ($should_block = "Y") {
return 444;
}

# ③ 速率限制
limit_req zone=botzone burst=20 nodelay;

# ④ 掃描器路徑靜默處理
location ~* /\.(git|svn|hg|bzr|env|aws|DS_Store) {
access_log off;
return 404;
}
location ~ /\.ht {
access_log off;
return 404;
}
location = /dns-query {
access_log off;
return 404;
}

root /var/www/html;
index index.php index.html index.htm;

add_header X-Frame-Options "SAMEORIGIN" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Access-Control-Allow-Origin "*" always;
add_header Cache-Control "public, no-transform" always;

# -----------------------------------------------------------------
# phpMyAdmin(僅允許本機)
# -----------------------------------------------------------------
location ^~ /phpmyadmin {
allow 127.0.0.1;
deny all;
error_page 403 =404 /404_deadlink;
alias /var/www/html/phpmyadmin/;
index index.php index.html;

location ~ \.php$ {
allow 127.0.0.1;
deny all;
error_page 403 =404 /404_deadlink;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php8.5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}

# 載入各站路由
include snippets/site-mediawiki.conf;
include snippets/site-phpbb.conf;
include snippets/site-wordpress.conf;
include snippets/site-assets-fix.conf;

# -----------------------------------------------------------------
# 聊天室主要路由
# -----------------------------------------------------------------
location = /chat {
return 301 /chat/;
}

location ^~ /chat/ {
alias /var/www/my-chat/public/;
index index.html;
}

# 聊天室圖片上傳 PHP 路由
location = /chat/upload.php {
alias /var/www/my-chat/upload.php;
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.5-fpm.sock;
fastcgi_param SCRIPT_FILENAME /var/www/my-chat/upload.php;
# include fastcgi_params; ← 已省略,fastcgi-php.conf 已包含
}

# 聊天室圖片瀏覽路由(IP 直連專用 — 解除防盜鏈)
location ^~ /chat/uploads/ {
alias /var/www/my-chat/uploads/;
access_log off;
expires 30d;
}

# -----------------------------------------------------------------
# AWStats
# -----------------------------------------------------------------
location ^~ /awstats-icon/ {
alias /usr/share/awstats/icon/;
access_log off;
}

location ^~ /awstatscss/ {
alias /usr/share/awstats/wwwroot/css/;
access_log off;
}

location = /awstats {
return 301 http://$host/awstats/awstats.pl?config=zine-tw.com;
}

location = /awstats/ {
return 301 http://$host/awstats/awstats.pl?config=zine-tw.com;
}

location ^~ /awstats/ {
alias /usr/lib/cgi-bin/;
index awstats.pl;

location ~ \.pl$ {
include fastcgi_params;
fastcgi_pass unix:/run/fcgiwrap.socket;
fastcgi_param SCRIPT_FILENAME /usr/lib/cgi-bin/awstats.pl;
fastcgi_param SCRIPT_NAME /awstats.pl;
fastcgi_param DOCUMENT_ROOT /usr/lib/cgi-bin;
fastcgi_param QUERY_STRING $query_string;
charset utf-8;
override_charset on;
gzip off;
sub_filter_once off;
sub_filter '</style>' '</style><link rel="stylesheet" href="/awstatscss/awstats.css">';
}
}

# -----------------------------------------------------------------
# WebSocket 代理
# -----------------------------------------------------------------
location ^~ /wss {
proxy_pass http://127.0.0.1:8282;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_read_timeout 600s;
proxy_send_timeout 600s;
proxy_buffering off;
}

# 其餘 PHP 解析
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

location = /404_deadlink {
internal;
return 404;
}
} # ← 補上這個 }


# =========================================================================
# 區塊 2: HTTPS 主站 (server_name zine-tw.com)
# =========================================================================
server {
listen 443 ssl;
http2 on;
server_name zine-tw.com www.zine-tw.com;

# ① 先擋惡意 IP
if ($bad_ip) { return 444; }

# ② 再擋 bot UA
set $should_block "";
if ($block_bot) {
set $should_block "Y";
}
if ($is_google = "yes") {
set $should_block "N"; # 如果是真·Google IP,強制洗成不阻擋
}
if ($should_block = "Y") {
return 444;
}

# ③ 速率限制
limit_req zone=botzone burst=20 nodelay;

# ④ 掃描器路徑靜默處理
location ~* /\.(git|svn|hg|bzr|env|aws|DS_Store) {
access_log off;
return 404;
}
location ~ /\.ht {
access_log off;
return 404;
}
location = /dns-query {
access_log off;
return 404;
}

root /var/www/html;
index index.php index.html index.htm;

ssl_certificate /etc/letsencrypt/live/zine-tw.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/zine-tw.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Permissions-Policy "geolocation=(), microphone=(), camera=()" always;
add_header Content-Security-Policy "default-src 'self' https: data: 'unsafe-inline' 'unsafe-eval'; connect-src 'self' https://zine-tw.com ws://zine-tw.com wss://zine-tw.com http://123.241.40.195:8282 ws://123.241.40.195:8282;" always;
add_header Cross-Origin-Opener-Policy "same-origin" always;
# add_header Cross-Origin-Embedder-Policy "credentialless" always;
# add_header Cross-Origin-Resource-Policy "same-origin" always;
add_header Access-Control-Allow-Origin "*" always;
add_header Cache-Control "public, no-transform" always;

# -----------------------------------------------------------------
# AWStats
# -----------------------------------------------------------------
location ^~ /awstats-icon/ {
alias /usr/share/awstats/icon/;
access_log off;
}

location ^~ /awstatscss/ {
alias /usr/share/awstats/wwwroot/css/;
access_log off;
}

location = /awstats {
return 301 $scheme://$host/awstats/awstats.pl?config=zine-tw.com;
}

location = /awstats/ {
return 301 $scheme://$host/awstats/awstats.pl?config=zine-tw.com;
}

location ^~ /awstats/ {
alias /usr/lib/cgi-bin/;
index awstats.pl;

location ~ \.pl$ {
include fastcgi_params;
fastcgi_pass unix:/run/fcgiwrap.socket;
fastcgi_param SCRIPT_FILENAME /usr/lib/cgi-bin/awstats.pl;
fastcgi_param SCRIPT_NAME /awstats.pl;
fastcgi_param DOCUMENT_ROOT /usr/lib/cgi-bin;
fastcgi_param QUERY_STRING $query_string;
charset utf-8;
override_charset on;
gzip off;
sub_filter_once off;
sub_filter '</style>' '</style><link rel="stylesheet" href="/awstatscss/awstats.css">';
}
}

# -----------------------------------------------------------------
# phpMyAdmin(僅允許本機)
# -----------------------------------------------------------------
location ^~ /phpmyadmin {
allow 127.0.0.1;
deny all;
error_page 403 =404 /404_deadlink;
alias /var/www/html/phpmyadmin/;
index index.php index.html;

location ~ \.php$ {
allow 127.0.0.1;
deny all;
error_page 403 =404 /404_deadlink;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php8.5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}

# -----------------------------------------------------------------
# WebSocket 代理
# -----------------------------------------------------------------
location ^~ /wss {
proxy_pass http://127.0.0.1:8282;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 600s;
proxy_send_timeout 600s;
proxy_buffering off;
}

# 聊天室主要路由
location = /chat {
return 301 /chat/;
}

location ^~ /chat/ {
alias /var/www/my-chat/public/;
index index.html;
}

# 聊天室圖片上傳 PHP 路由
location = /chat/upload.php {
alias /var/www/my-chat/upload.php;
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.5-fpm.sock;
fastcgi_param SCRIPT_FILENAME /var/www/my-chat/upload.php;
# include fastcgi_params; ← 已省略
}

# 聊天室圖片瀏覽路由(HTTPS 主站,保留防盜鏈)
location ^~ /chat/uploads/ {
alias /var/www/my-chat/uploads/;
access_log off;
expires 30d;

valid_referers none blocked server_names 123.241.40.195;
if ($invalid_referer) {
return 403;
}
}

# 核心引入:各站點規則
include snippets/site-mediawiki.conf;
include snippets/site-phpbb.conf;
include snippets/site-wordpress.conf;
include snippets/site-assets-fix.conf;

# 根目錄其餘 PHP 解析
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.5-fpm.sock;
include fastcgi_params;
fastcgi_param HTTP_PROXY "";
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

location = /404_deadlink {
internal;
return 404;
}
}