# 源码吧 (www.mycode8.com) - 生产环境 Nginx + PHP/ASP.NET 安全基线配置参考 # 使用前请将 example.com 替换为您的真实域名,并在 nginx -t 校验通过后重载 # 1. 裸域 301 跳转 www 并统一 HTTPS if ($host = "example.com") { return 301 https://www.example.com$request_uri; } # 2. 拦截隐藏目录、版本库与备份文件 location ~ ^/(\.user\.ini|\.htaccess|\.git|\.env|\.svn|composer\.json|composer\.lock|README\.md) { return 404; } location ~* \.(sql|bak|swp|log|old|zip|tar|gz)$ { deny all; } # 3. 禁止上传目录执行 PHP 脚本 location ~* ^/(uploads|storage|runtime)/.*\.php$ { return 403; } # 4. 安全响应头 add_header X-Content-Type-Options "nosniff" always; add_header X-Frame-Options "SAMEORIGIN" always; add_header Referrer-Policy "strict-origin-when-cross-origin" always; # 5. PHP 前端控制器伪静态 location / { try_files $uri $uri/ /index.php?$query_string; } # 6. 静态资源本地长缓存 location ~* \.(css|js|png|jpg|jpeg|gif|ico|svg|webp|woff2)$ { expires 30d; access_log off; }