以下依照操作紀錄整理成結構化的設定文章,可直接作為技術分享或備忘錄。
- 環境說明
- 作業系統:Debian 13 (Trixie)
- Web Server:Nginx
- 防護工具:Fail2ban + CrowdSec
- Bouncer 方案:iptables(不使用 nftables bouncer)
- 管理 IP 白名單:123.241.40.195(請自行替換)
# ── 二、安裝 Fail2ban ──
sudo apt update && sudo apt install fail2ban -y
# ── 三、安裝 CrowdSec ──
curl -s https://install.crowdsec.net | sudo sh
sudo apt update && sudo apt install crowdsec -y
sudo cscli collections install crowdsecurity/nginx crowdsecurity/wordpress crowdsecurity/http-cve
sudo systemctl restart crowdsec
#
# 這個不要裝!
# sudo apt install crowdsec-firewall-bouncer-nftables -y
sudo apt remove crowdsec-firewall-bouncer-nftables -y
sudo apt install crowdsec-firewall-bouncer -y
sudo apt install crowdsec-nginx-bouncer -y
sudo systemctl enable crowdsec-firewall-bouncer
sudo systemctl restart crowdsec-firewall-bouncer
sudo systemctl status crowdsec-firewall-bouncer
# ── 四、設定 CrowdSec 日誌採集 ──
# 編輯 /etc/crowdsec/acquis.yaml:
Code: Select all | 複製程式碼
filenames:
- /var/log/auth.log
labels:
type: syslog
---
filenames:
- /var/log/nginx/*.log
labels:
type: nginx
- /var/log/auth.log
labels:
type: syslog
---
filenames:
- /var/log/nginx/*.log
labels:
type: nginx
# ── 五、設定 Fail2ban ──
nano /etc/fail2ban/fail2ban.local
Code: Select all | 複製程式碼
[Definition]
dbpurgeage = 8d
dbpurgeage = 8d
Code: Select all | 複製程式碼
[DEFAULT]
# 關鍵:設定白名單,讓您自己的 IP 或家裡/公司的 IP 絕對不會被 Fail2ban 誤鎖
ignoreip = 127.0.0.1/8 ::1 123.241.40.195
[nginx-bot-crawler]
enabled = true
port = http,https
filter = nginx-bot-crawler
logpath = /var/log/nginx/access.log
backend = polling
maxretry = 10
findtime = 1h
bantime = 1h
[nginx-phpbb-auth]
enabled = true
port = http,https
filter = nginx-phpbb-auth
logpath = /var/log/nginx/access.log
backend = polling
maxretry = 3
findtime = 6h
bantime = 7d
# 關鍵:設定白名單,讓您自己的 IP 或家裡/公司的 IP 絕對不會被 Fail2ban 誤鎖
ignoreip = 127.0.0.1/8 ::1 123.241.40.195
[nginx-bot-crawler]
enabled = true
port = http,https
filter = nginx-bot-crawler
logpath = /var/log/nginx/access.log
backend = polling
maxretry = 10
findtime = 1h
bantime = 1h
[nginx-phpbb-auth]
enabled = true
port = http,https
filter = nginx-phpbb-auth
logpath = /var/log/nginx/access.log
backend = polling
maxretry = 3
findtime = 6h
bantime = 7d
# 自訂 Filter:phpBB 登入爆破
nano /etc/fail2ban/filter.d/nginx-phpbb-auth.conf
Code: Select all | 複製程式碼
[Definition]
failregex = ^<HOST> - - \[.*?\] "POST /phpBB3/ucp\.php\?mode=login[^"]*HTTP/[0-9.]+" 302 [0-9]+\s
^<HOST> - - \[.*?\] "POST /phpBB3/ucp\.php\?mode=register[^"]*HTTP/[0-9.]+" 302 [0-9]+\s
ignoreregex =
failregex = ^<HOST> - - \[.*?\] "POST /phpBB3/ucp\.php\?mode=login[^"]*HTTP/[0-9.]+" 302 [0-9]+\s
^<HOST> - - \[.*?\] "POST /phpBB3/ucp\.php\?mode=register[^"]*HTTP/[0-9.]+" 302 [0-9]+\s
ignoreregex =
Code: Select all | 複製程式碼
[Definition]
# 辨識 Nginx access.log 中被您的 block-bots.conf 阻擋並回傳 403 Forbidden 的惡意爬蟲
failregex = ^<HOST> - - \[.*?\] ".*?" 403 [0-9]+\s
ignoreregex =
# 辨識 Nginx access.log 中被您的 block-bots.conf 阻擋並回傳 403 Forbidden 的惡意爬蟲
failregex = ^<HOST> - - \[.*?\] ".*?" 403 [0-9]+\s
ignoreregex =
Code: Select all | 複製程式碼
map $http_user_agent $block_bot {
default 0;
"~*(SemrushBot|Reflectionbot|AhrefsBot|DotBot|MJ12bot)" 1;
}
default 0;
"~*(SemrushBot|Reflectionbot|AhrefsBot|DotBot|MJ12bot)" 1;
}
Code: Select all | 複製程式碼
# =========================================================================
# 區塊 1A: Port 80 網域名稱 (強制導向 HTTPS)
# =========================================================================
server {
listen 80;
server_name zine-tw.com www.zine-tw.com;
# ① 先擋惡意 IP
if ($bad_ip) { return 444; }
# ② 再擋 bot UA
if ($block_bot) { 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
if ($block_bot) { 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
if ($block_bot) { 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;
}
}
# 區塊 1A: Port 80 網域名稱 (強制導向 HTTPS)
# =========================================================================
server {
listen 80;
server_name zine-tw.com www.zine-tw.com;
# ① 先擋惡意 IP
if ($bad_ip) { return 444; }
# ② 再擋 bot UA
if ($block_bot) { 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
if ($block_bot) { 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
if ($block_bot) { 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;
}
}
sudo pkill -9 fail2ban
sudo rm -rf /var/run/fail2ban/*
sudo systemctl daemon-reload
sudo systemctl restart fail2ban
sudo fail2ban-client status
sudo fail2ban-client status nginx-http-auth
sudo fail2ban-client status nginx-botsearch
# ── 六、CrowdSec 追加 scenarios 與白名單 ──
sudo cscli collections install crowdsecurity/nginx
sudo cscli collections install crowdsecurity/base-http-scenarios
sudo systemctl reload crowdsec
# 編輯 /etc/crowdsec/parsers/s02-enrich/whitelists.yaml:
Code: Select all | 複製程式碼
name: crowdsecurity/whitelists
description: "Whitelist events from private ipv4 addresses"
filter: "1==1"
whitelist:
reason: "private ipv4/ipv6 ip/ranges and my own ip"
ip:
- "::1"
- "123.241.40.195"
cidr:
- "127.0.0.0/8"
- "192.168.0.0/16"
- "10.0.0.0/8"
- "172.16.0.0/12"
- "127.0.0.1/8"
- "::1/128"
description: "Whitelist events from private ipv4 addresses"
filter: "1==1"
whitelist:
reason: "private ipv4/ipv6 ip/ranges and my own ip"
ip:
- "::1"
- "123.241.40.195"
cidr:
- "127.0.0.0/8"
- "192.168.0.0/16"
- "10.0.0.0/8"
- "172.16.0.0/12"
- "127.0.0.1/8"
- "::1/128"
sudo systemctl restart crowdsec
sudo systemctl status crowdsec
sudo systemctl status crowdsec-firewall-bouncer
# ── 七、開機自動啟動 ──
sudo systemctl enable fail2ban
sudo systemctl enable crowdsec
sudo systemctl enable crowdsec-firewall-bouncer
# ── 八、狀態檢查指令速查 ──
sudo cscli metrics
sudo cscli decisions list
sudo cscli bouncers list
sudo cscli capi status
sudo nft list set ip crowdsec bouncers_sets
sudo nft list ruleset | grep crowdsec
sudo fail2ban-client status
sudo fail2ban-client status sshd
# 改用 iptables 驗證(因為已改用 iptables bouncer)
sudo iptables -L CROWDSEC -n --line-numbers
sudo iptables -L F2B-sshd -n
# ── 九、定期清空處分(自動解除封鎖排程) ──
# ── 十、常見踩坑整理 ──
測試 filter 是否正確命中:
sudo fail2ban-regex /var/log/nginx/access.log /etc/fail2ban/filter.d/nginx-phpbb-auth.conf
更新最新名單
sudo cscli hub update && sudo cscli hub upgrade && sudo systemctl restart crowdsec
• 查看爬蟲封鎖狀況:bash
sudo fail2ban-client status nginx-bot-crawler
• 查看論壇防破壞狀況:bash
sudo fail2ban-client status nginx-phpbb-auth
。
• 手動解封某個被誤傷的 IP(例如解封 1.2.3.4):bash
sudo fail2ban-client set nginx-phpbb-auth unbanip 1.2.3.4