Add Nginx deny config and server hardening scripts

Add new nginx/deny.conf to block unmatched hosts and explicitly close
fukun.net. Remove Photoview icon hijacking, add client_max_body_size.

Add scripts for deep inspection, disk monitoring, firewall setup,
Gitea port confinement, and security updates.
This commit is contained in:
2026-05-28 10:51:36 +08:00
parent cb3bfaf206
commit 30918f4642
7 changed files with 645 additions and 11 deletions
+74
View File
@@ -0,0 +1,74 @@
#!/usr/bin/env bash
# ============================================================
# secure-gitea@tx-ubuntu-ubuntu.sh
# 将 Gitea 端口收敛到 127.0.0.1(Nginx 反代已就绪)
# ============================================================
set -e
C_YELLOW='\033[1;33m'
C_GREEN='\033[1;32m'
C_CYAN='\033[1;36m'
C_RESET='\033[0m'
section() { echo -e "\n${C_CYAN}━━━ $1 ━━━${C_RESET}"; }
ok() { echo -e " ${C_GREEN}✓${C_RESET} $1"; }
warn() { echo -e " ${C_YELLOW}⚠${C_RESET} $1"; }
APP_INI=/data/gitea/app.ini
section "1. 当前状态"
echo " 绑定: $(sudo ss -tlnp | grep 3000 | awk '{print $4}')"
if grep -q 'HTTP_ADDR' "$APP_INI"; then
echo " HTTP_ADDR: $(grep HTTP_ADDR "$APP_INI")"
else
echo " HTTP_ADDR: (未设置,默认 0.0.0.0)"
fi
section "2. 修改配置"
# 在 [server] 段中 HTTP_PORT 之后插入 HTTP_ADDR
if grep -q '^HTTP_ADDR' "$APP_INI"; then
# 已存在则修改
sudo sed -i 's/^HTTP_ADDR.*/HTTP_ADDR = 127.0.0.1/' "$APP_INI"
ok "HTTP_ADDR 已修改为 127.0.0.1"
else
# 不存在则在 HTTP_PORT 行后插入
sudo sed -i '/^HTTP_PORT/a HTTP_ADDR = 127.0.0.1' "$APP_INI"
ok "HTTP_ADDR = 127.0.0.1 已添加"
fi
# 验证配置
echo ""
echo " 新配置 [server] 段:"
grep -E '(HTTP_PORT|HTTP_ADDR|DOMAIN)' "$APP_INI"
section "3. 重启 Gitea"
sudo systemctl restart gitea
sleep 2
if systemctl is-active --quiet gitea; then
ok "Gitea 已重启,服务 active"
else
echo " ✗ Gitea 启动失败,回滚!"
sudo sed -i '/^HTTP_ADDR.*127.0.0.1/d' "$APP_INI"
sudo systemctl restart gitea
warn "已回滚配置,需排查"
exit 1
fi
section "4. 验证"
echo " 端口绑定:"
sudo ss -tlnp | grep 3000
echo ""
echo " Nginx 反代探活:"
CODE=$(curl -s -o /dev/null -w "%{http_code}" --connect-timeout 3 https://git.fukun.net 2>/dev/null || echo "000")
if [ "$CODE" = "200" ] || [ "$CODE" = "302" ] || [ "$CODE" = "301" ]; then
ok "git.fukun.net → HTTP $CODE (正常)"
else
warn "git.fukun.net → HTTP $CODE (异常,检查 Nginx)"
fi
echo ""
echo -e "${C_GREEN}===== Gitea 端口收敛完成 =====${C_RESET}"