先确保服务器满足两个核心条件,后续操作才能生效:
这是第一道防线,政务外网场景下必须修改默认设置:
bt restart生效政务外网场景下,要同时配置服务器系统防火墙(iptables/ufw)和云服务商安全组/政务物理防火墙,只开放必须端口:

查看当前iptables规则
iptables -L -n
清空默认规则(操作前确认白名单SSH已生效!)
iptables -F
iptables -X
设置默认策略为DROP(只允许已配置的入站/出站)
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT DROP
允许本地回环
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
允许已建立的连接(比如访问网站后的响应)
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
允许白名单IP访问宝塔面板端口(假设白名单IP是192.168.1.10,面板端口52147)
iptables -A INPUT -s 192.168.1.10 -p tcp --dport 52147 -j ACCEPT
允许白名单IP访问SSH端口(22或2222)
iptables -A INPUT -s 192.168.1.10 -p tcp --dport 22 -j ACCEPT
允许白名单IP访问HTTPS端口443
iptables -A INPUT -s 0.0.0.0/0 -p tcp --dport 443 -j ACCEPT
保存规则(CentOS 7)
service iptables save
保存规则(CentOS 8)
firewall-cmd --runtime-to-permanent
重启iptables(CentOS7)
systemctl restart iptables
重启firewalld(CentOS8)
systemctl restart firewalld
政务外网要求所有Web访问必须使用HTTPS,禁止HTTP明文:
HTTP强制跳转HTTPS
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
return 301 https://$server_name$request_uri;
禁用所有不需要的HTTP方法
if ($request_method !~ ^(GET|POST|HEAD)$ ) {
return 405;
}
}
HTTPS配置
server {
listen 443 ssl http2;
server_name yourdomain.com www.yourdomain.com;
证书路径替换为实际路径
ssl_certificate /www/server/panel/vhost/cert/yourdomain.com/fullchain.pem;
ssl_certificate_key /www/server/panel/vhost/cert/yourdomain.com/privkey.pem;
SSL安全协议配置(禁用TLS1.0/1.1,政务可选禁用TLS1.2仅留TLS1.3)
ssl_protocols TLSv1.2 TLSv1.3;
SSL加密套件配置(仅用强加密)
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1d;
ssl_session_tickets off;
HSTS(需替换max-age等参数)
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
禁用XSS、点击劫持、嗅探
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "no-referrer-when-downgrade" always;
add_header Content-Security-Policy "default-src 'self' 'unsafe-inline' 'unsafe-eval' data:; img-src 'self' data:; font-src 'self' data:;" always;
禁止访问隐藏文件
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
禁止访问敏感文件(根据业务调整)
location ~ \.(sql|bak|log|conf|ini|sh|py)$ {
deny all;
access_log off;
log_not_found off;
}
}
避免SSH与SFTP共用端口,降低被暴力破解的风险:
Port 22,在下方新增一行Port 2222Subsystem sftp /usr/libexec/openssh/sftp-server,替换为:
Subsystem sftp internal-sftp
Match User zwbtftp
ChrootDirectory /www/wwwroot
ForceCommand internal-sftp
AllowTcpForwarding no
PermitTunnel no
X11Forwarding no
chown root:root /www/wwwroot
chmod 755 /www/wwwroot
业务目录权限需调整为www:www(假设用Nginx+PHP)
chown -R www:www /www/wwwroot/yourdomain.com
chmod -R 755 /www/wwwroot/yourdomain.com
上传目录权限需调整为775
chmod -R 775 /www/wwwroot/yourdomain.com/uploads
政务场景下数据库是核心,必须严格配置:
mysql -u root -p,输入密码后执行:
DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');
FLUSH PRIVILEGES;
port=3306,修改为20000-65535之间的随机高位端口(比如53248),保存后重启数据库政务场景下要求日志留存≥6个月,备份留存≥3个月:
易频IT社区是综合性互联网IT技术门户网站,专注分享网络技术、服务器运维、网络安全、编程开发、系统架构、云计算、大数据等行业干货,实时更新IT行业资讯、零基础教程、实战案例,为IT从业者、技术爱好者提供专业的学习交流平台。
Copyright © 2021-2026 易频IT社区. All Rights Reserved. 备案号:闽ICP备2023013482号 网站地图