参考资料

  1. Nginx配置文件中通过location块匹配静态资源类型,并添加缓存响应头
  2. 如何设置nginx高级安全配置
  3. nginx 配置ssl证书
  4. Nginxsendfile 零复制指令详细说明以及案例
  5. Nginxexpires:页面缓存时间配置详细说明以及案例
  6. nginx 配置跨域
  7. 忽略非错误信息 nginx -q详细说明以及案例
  8. Nginx + PHP-FPM 配置指南

Nginx如何设置防篡改?

  1. 安装Nginx
    确保已安装Nginx,可通过命令检查:

nginx -v
  1. 配置防篡改模块
    在nginx.conf的http模块添加:

http {
    ...
    server_tokens off;
    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";
    ...
}
  1. 设置文件完整性校验
    使用sha256sum生成校验值:

sha256sum /path/to/your/file > checksum.txt
  1. 配置访问控制
    在server块中添加:

location /protected/ {
    internal;
    alias /path/to/protected/files/;
}
  1. 启用HTTPS
    配置SSL证书:

server {
    listen 443 ssl;
    ssl_certificate /path/to/cert.pem;
    ssl_certificate_key /path/to/key.pem;
    ...
}
  1. 设置文件权限

chown -R nginx:nginx /var/www/html
chmod -R 750 /var/www/html
  1. 配置日志监控
    在nginx.conf中添加:

log_format security '$remote_addr - $http_user_agent';
access_log /var/log/nginx/security.log security;
  1. 重启Nginx

systemctl restart nginx
  1. 验证配置
    检查语法:

nginx -t
  1. 定期检查
    设置cron任务定期校验文件:

0 3 * * * sha256sum -c /path/to/checksum.txt