nginx

关注公众号 jb51net

关闭
首页 > 网站技巧 > 服务器 > nginx > Nginx 常用安全头

Nginx 常用安全头的使用小结

作者:k***195

Web 应用中配置 HTTP 安全响应头是提升网站安全性的重要一步,本文就来详细的介绍一下Nginx 常用安全头,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

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:;";

配置说明:

注意事项:

2. Strict-Transport-Security (HSTS)

作用:强制浏览器通过 HTTPS 访问网站,防止中间人攻击。

add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always;

配置说明:

3. X-Frame-Options

作用:防止页面被嵌套到其他站点的 iframe 中,防止点击劫持攻击。

add_header X-Frame-Options SAMEORIGIN always;

配置说明:

4. X-XSS-Protection

作用:启用浏览器的 XSS 过滤功能,防止跨站脚本攻击。

add_header X-XSS-Protection "1; mode=block" always;

配置说明:

5. X-Content-Type-Options

作用:防止浏览器对资源类型进行 MIME 嗅探,避免脚本注入攻击。

add_header X-Content-Type-Options "nosniff" always;

配置说明:

6. Referrer-Policy

作用:控制 Referer 头信息的发送,保护用户隐私。

add_header Referrer-Policy "origin" always;

配置说明:

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;

配置说明:

9. Set-Cookie

作用:为 Cookie 添加安全属性,防止 XSS 和中间人攻击。

add_header Set-Cookie "Path=/; HttpOnly; Secure";

配置说明:

10. Cross-Origin-Embedder-Policy (COEP)

作用: 限制跨域资源的加载,用于启用跨域隔离。

add_header Cross-Origin-Embedder-Policy "require-corp" always;

配置说明:

11. Cross-Origin-Opener-Policy (COOP)

作用: 隔离文档上下文,防止跨窗口攻击。

add_header Cross-Origin-Opener-Policy "same-origin" always;

配置说明:

12. Cross-Origin-Resource-Policy (CORP)

作用: 限制资源的跨域加载。

add_header Cross-Origin-Resource-Policy "same-origin" always;

配置说明:

为静态资源启用缓存

为静态资源(如图片、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;
}

配置说明:

完整示例配置

安全头配置文件

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;
    #}
}

参考:http-headers-MDN

到此这篇关于Nginx 常用安全头的使用小结的文章就介绍到这了,更多相关Nginx 常用安全头内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文