nginx 安全配置

Last updated on December 17, 2025 am

🧙 Questions

☄️ Ideas

server {

    # 隐藏nginx版本号
    server_tokens off;

    # 基础安全头
    add_header X-XSS-Protection          "1; mode=block" always;
    add_header X-Frame-Options           "DENY" always;
    add_header X-Content-Type-Options    "nosniff" always;
    add_header Referrer-Policy           "strict-origin-when-cross-origin" always;
    
    # Content Security Policy (根据业务调整)
    add_header Content-Security-Policy "default-src 'self'; 
    script-src 'self' 'unsafe-inline' 'unsafe-eval' https://trusted-cdn.com; 
    style-src 'self' 'unsafe-inline'; 
    img-src 'self' data: https:; 
    font-src 'self' data:; 
    connect-src 'self'; 
    frame-src 'self'; 
    object-src 'none'; 
    frame-ancestors 'self';" always;
    
    # HSTS - 仅HTTPS站点启用
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
    
    # 防止点击劫持
    add_header X-Frame-Options "SAMEORIGIN" always;
    
    # 其他推荐安全头
    add_header X-Permitted-Cross-Domain-Policies "none" always;
    add_header Permissions-Policy "geolocation=(), microphone=(), camera=()" always;
    add_header Clear-Site-Data "\"cache\", \"cookies\", \"storage\"" always;
    more_clear_headers "X-Powered-By";
}

nginx 安全配置
https://ispong.isxcode.com/vue/nginx/nginx 安全配置/
Author
ispong
Posted on
December 10, 2025
Licensed under