#!/bin/bash
###########################################################
# 宝塔 Nginx 安全头一键加固脚本
# 适用:swhswl.com
# 作用:自动添加全套安全头,实现安全检测 A+
###########################################################
DOMAIN="swhswl.com"
NGINX_CONF="/www/server/panel/vhost/nginx/${DOMAIN}.conf"
# 备份原有配置
cp -f ${NGINX_CONF} ${NGINX_CONF}.bak.$(date +%Y%m%d%H%M%S)
# 插入安全头(自动写入)
sed -i '/listen 80;/d' ${NGINX_CONF}
sed -i '/listen 443;/d' ${NGINX_CONF}
sed -i '/server_name/a
# 安全头加固 - 创世神一键脚本
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
add_header X-Frame-Options SAMEORIGIN always;
add_header X-Content-Type-Options nosniff always;
add_header X-XSS-Protection "1; mode=block" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Permissions-Policy "geolocation=(),camera=(),microphone=()" always;
add_header Expect-CT "max-age=86400" always;
' ${NGINX_CONF}
# 重启 nginx
systemctl restart nginx
echo "========================================"
echo "✅ 安全头已一键配置完成!"
echo "✅ 网站:${DOMAIN}"
echo "✅ 配置文件:${NGINX_CONF}"
echo "✅ 已自动备份原配置"
echo "========================================"
上面的脚本, 或者直接用下面这个 一键脚本,复制到宝塔 → 计划任务 → Shell 脚本 → 执行一次即可。

ssl_session_timeout 10m;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
add_header X-Frame-Options "SAMEORIGIN";
add_header Referrer-Policy "strict-origin-when-cross-origin";

🧱 安全头缺失(高危)这个应该很多人都没有搞过.但是这的确是的
© 版权声明
THE END
暂无评论内容