web 应用中配置 http 安全响应头是提升网站安全性的重要一步。合理配置 nginx 的安全头,可以抵御常见的安全威胁(如 xss、点击劫持、mime 类型嗅探等),增强用户隐私保护和传输安全性。
常见的 http 安全头及其作用
1. content-security-policy (csp)
作用:限制资源(如脚本、样式、图片等)的加载来源,防止 xss 和数据注入攻击。
add_header content-security-policy "default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; object-src 'none'; frame-ancestors 'none';worker-src blob:;";
配置说明:
- `default-src ‘self’:限制所有资源的默认加载来源为当前域。
script-src 'self':仅允许加载来自当前域的脚本。style-src 'self:限制样式来源为当前域。img-src 'self' data::允许图片来自当前域和data:uri。object-src 'none':禁止加载<object>、<embed>,防止插件攻击。frame-ancestors 'none':禁止页面被嵌套到其他站点的iframe中。worker-src blob::允许 web worker 资源来源为blob:。
注意事项:
如果需要加载第三方资源(如 cdn),需显式添加来源。
script-src ‘self’ https://cdn.jsdelivr.net;
避免使用
'unsafe-inline'和'unsafe-eval',减少 xss 风险。使用
nonce或hash机制允许特定的内联脚本:add_header content-security-policy “script-src ‘self’ ‘nonce-random123’;”;
2. strict-transport-security (hsts)
作用:强制浏览器通过 https 访问网站,防止中间人攻击。
add_header strict-transport-security "max-age=63072000; includesubdomains; preload" always;
配置说明:
max-age=63072000:hsts 有效期(以秒为单位),这里是 2 年。includesubdomains:对子域名启用 hsts。preload:允许网站加入 hsts 预加载列表。
3. x-frame-options
作用:防止页面被嵌套到其他站点的 iframe 中,防止点击劫持攻击。
add_header x-frame-options sameorigin always;
配置说明:
sameorigin:仅允许页面被嵌套到自身域的iframe中。deny:彻底禁止页面被嵌套。allow-from uri:仅允许特定来源嵌套页面(需注意已被部分浏览器废弃)。
4. x-xss-protection
作用:启用浏览器的 xss 过滤功能,防止跨站脚本攻击。
add_header x-xss-protection "1; mode=block" always;
配置说明:
1; mode=block:启用 xss 过滤器并阻止加载恶意脚本的页面。
5. x-content-type-options
作用:防止浏览器对资源类型进行 mime 嗅探,避免脚本注入攻击。
add_header x-content-type-options "nosniff" always;
配置说明:
nosniff:强制浏览器使用content-type指定的 mime 类型。
6. referrer-policy
作用:控制 referer 头信息的发送,保护用户隐私。
add_header referrer-policy "origin" always;
配置说明:
origin:跨域请求时仅发送来源站点信息,不包括完整的 url。
7. permissions-policy (原 feature-policy)
作用:限制浏览器功能的使用,防止滥用。
add_header permissions-policy "geolocation=(), microphone=(), camera=(), fullscreen=(self);";
配置说明:
- 禁用了地理定位、麦克风、摄像头功能。
- 仅允许自身域使用全屏功能。
8. cache-control 和 pragma
作用:控制缓存行为,防止敏感数据被缓存。
add_header cache-control "no-store" always; add_header pragma "no-cache" always;
配置说明:
cache-control: no-store:禁止缓存页面内容。pragma: no-cache:兼容旧版 http 协议的缓存控制头。
9. set-cookie
作用:为 cookie 添加安全属性,防止 xss 和中间人攻击。
add_header set-cookie "path=/; httponly; secure";
配置说明:
httponly:防止客户端脚本访问 cookie,避免 xss。secure:仅通过 https 发送 cookie。
10. cross-origin-embedder-policy (coep)
作用: 限制跨域资源的加载,用于启用跨域隔离。
add_header cross-origin-embedder-policy "require-corp" always;
配置说明:
require-corp:仅允许跨域加载具有 cors 或cross-origin-resource-policy标头的资源。
11. cross-origin-opener-policy (coop)
作用: 隔离文档上下文,防止跨窗口攻击。
add_header cross-origin-opener-policy "same-origin" always;
配置说明:
same-origin:仅允许相同源的页面与当前页面共享浏览上下文。
12. cross-origin-resource-policy (corp)
作用: 限制资源的跨域加载。
add_header cross-origin-resource-policy "same-origin" always;
配置说明:
same-origin:仅允许资源从相同源加载。
为静态资源启用缓存
为静态资源(如图片、css、js 文件)启用缓存可以显著提升性能,同时不会直接引发安全问题。以下是推荐的配置:
location ~* .(css|js|png|jpg|jpeg|gif|ico|woff|woff2|ttf|svg|eot|otf)$ {
expires 1y;
add_header cache-control "public";
add_header content-security-policy "default-src 'self';";
add_header x-content-type-options nosniff;
add_header x-frame-options sameorigin;
add_header strict-transport-security "max-age=63072000; includesubdomains; preload";
access_log off;
}
配置说明:
expires 1y:允许静态资源缓存 1 年。cache-control: public:标记资源为公共可缓存。access_log off:禁用访问日志,减少服务器存储压力。
完整示例配置
安全头配置文件:
add_header content-security-policy "default-src 'self' http: https: blob: ; script-src 'self' yourjsurl; object-src 'self'; img-src 'self' data: blob: yourimgurl; style-src 'unsafe-inline' http: ; frame-ancestors 'self'; worker-src blob:;" always; 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 strict-transport-security "max-age=63072000; includesubdomains; preload" always; add_header referrer-policy origin always; add_header cache-control no-store always; add_header pragma no-cache always; add_header x-permitted-cross-domain-policies none always; add_header x-download-options noopen always; add_header set-cookie "path=/; httponly; secure" always; add_header cross-origin-embedder-policy "require-corp" always; add_header cross-origin-opener-policy "same-origin" always; add_header cross-origin-resource-policy "same-origin" always;
如果js和img需要配置允许的域名,替换路径即可。
跨域头配置文件:
add_header 'access-control-allow-origin' "$cors_origin" always; add_header 'vary' 'origin' always; add_header 'access-control-allow-methods' 'get, post, put, delete, options' always; add_header 'access-control-allow-headers' 'content-type, authorization, x-requested-with' always; add_header 'access-control-max-age' "$cors_max_age" always;
nginx 的default.conf配置示例:
# 动态设置允许的跨域来源
map $http_origin $cors_origin {
default "";
"~^https?://trusteddomain1.com$" $http_origin;
"~^https?://trusteddomain2.com$" $http_origin;
}
# 动态设置缓存时间
map $http_origin $cors_max_age {
default "0";
"~^https?://trusteddomain1.com$" "86400"; # 1 天
"~^https?://trusteddomain2.com$" "3600"; # 1 小时
}
server {
listen 8081;
listen [::]:8081;
server_name localhost;
root /usr/share/nginx/html/applet/dist/build/h5/;
server_tokens off;
include /etc/nginx/conf.d/safety_headers.conf;
location ~* .(css|js|png|jpg|jpeg|gif|ico|woff|woff2|ttf|svg|eot|otf)$ {
expires 1y;
include /etc/nginx/conf.d/safety_headers.conf;
add_header cache-control "public";
access_log off;
}
# 禁止敏感路径访问
location = /auth/ {
deny all;
return 404;
}
error_page 403 =404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~* ^/(code|authfront|adminplus|external|auth|admin|marry){
include /etc/nginx/conf.d/safety_headers.conf;
# 设置 cors 响应头
include /etc/nginx/conf.d/cors_headers.conf;
# 如果是预检请求 (options),直接返回成功
if ($request_method = 'options') {
# 设置 cors 响应头
add_header 'access-control-allow-origin' "$cors_origin" always;
add_header 'vary' 'origin' always;
add_header 'access-control-allow-methods' 'get, post, put, delete, options' always;
add_header 'access-control-allow-headers' 'content-type, authorization, x-requested-with' always;
add_header 'access-control-max-age' "$cors_max_age" always;
add_header 'content-length' 0;
add_header 'content-type' 'text/plain';
return 204;
}
proxy_pass backendurl;
proxy_set_header upgrade $http_upgrade;
proxy_set_header connection "upgrade";
proxy_set_header x-real-ip $remote_addr;
proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
client_max_body_size 1024m;
proxy_buffer_size 1024k;
proxy_buffers 16 1024k;
proxy_busy_buffers_size 2048k;
proxy_temp_file_write_size 2048k;
}
#location /manage {
# alias /usr/share/nginx/html/manage/dist/;
#}
location ~^/oss/crossdomain.xml {return 403;}
location ~^/oss/(.*host.*){return 403;}
location ~^/oss/ {
proxy_buffering off;
proxy_set_header host $http_host;
rewrite ^/oss/(.*)$ /$1 break;
proxy_pass http://172.17.0.1:9000;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
#error_page 500 502 503 504 /50x.html;
#location = /50x.html {
# root /usr/share/nginx/html;
#}
# proxy the php scripts to apache listening on 127.0.0.1:80
#
#location ~ .php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the php scripts to fastcgi server listening on 127.0.0.1:9000
#
#location ~ .php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param script_filename /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if apache's document root
# concurs with nginx's one
#
#location ~ /.ht {
# deny all;
#}
}
到此这篇关于nginx 常用安全头的使用小结的文章就介绍到这了,更多相关nginx 常用安全头内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论