最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Nginx 常用安全頭的使用小結(jié)

 更新時(shí)間:2026年01月14日 11:20:53   作者:k***195  
Web 應(yīng)用中配置 HTTP 安全響應(yīng)頭是提升網(wǎng)站安全性的重要一步,本文就來(lái)詳細(xì)的介紹一下Nginx 常用安全頭,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

Web 應(yīng)用中配置 HTTP 安全響應(yīng)頭是提升網(wǎng)站安全性的重要一步。合理配置 Nginx 的安全頭,可以抵御常見(jiàn)的安全威脅(如 XSS、點(diǎn)擊劫持、MIME 類(lèi)型嗅探等),增強(qiáng)用戶隱私保護(hù)和傳輸安全性。

常見(jiàn)的 HTTP 安全頭及其作用

1. Content-Security-Policy (CSP)

作用:限制資源(如腳本、樣式、圖片等)的加載來(lái)源,防止 XSS 和數(shù)據(jù)注入攻擊。

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

配置說(shuō)明:

  • `default-src ‘self’:限制所有資源的默認(rèn)加載來(lái)源為當(dāng)前域。
  • script-src 'self':僅允許加載來(lái)自當(dāng)前域的腳本。
  • style-src 'self:限制樣式來(lái)源為當(dāng)前域。
  • img-src 'self' data::允許圖片來(lái)自當(dāng)前域和 data: URI。
  • object-src 'none':禁止加載 <object><embed>,防止插件攻擊。
  • frame-ancestors 'none':禁止頁(yè)面被嵌套到其他站點(diǎn)的 iframe 中。
  • worker-src blob::允許 Web Worker 資源來(lái)源為 blob:。

注意事項(xiàng):

  • 如果需要加載第三方資源(如 CDN),需顯式添加來(lái)源。

    script-src ‘self’ https://cdn.jsdelivr.net;

  • 避免使用 'unsafe-inline''unsafe-eval',減少 XSS 風(fēng)險(xiǎn)。

  • 使用 noncehash 機(jī)制允許特定的內(nèi)聯(lián)腳本:

    add_header Content-Security-Policy “script-src ‘self’ ‘nonce-random123’;”;

2. Strict-Transport-Security (HSTS)

作用:強(qiáng)制瀏覽器通過(guò) HTTPS 訪問(wèn)網(wǎng)站,防止中間人攻擊。

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

配置說(shuō)明:

  • max-age=63072000:HSTS 有效期(以秒為單位),這里是 2 年。
  • includeSubDomains:對(duì)子域名啟用 HSTS。
  • preload:允許網(wǎng)站加入 HSTS 預(yù)加載列表。

3. X-Frame-Options

作用:防止頁(yè)面被嵌套到其他站點(diǎn)的 iframe 中,防止點(diǎn)擊劫持攻擊。

add_header X-Frame-Options SAMEORIGIN always;

配置說(shuō)明:

  • SAMEORIGIN:僅允許頁(yè)面被嵌套到自身域的 iframe 中。
  • DENY:徹底禁止頁(yè)面被嵌套。
  • ALLOW-FROM uri:僅允許特定來(lái)源嵌套頁(yè)面(需注意已被部分瀏覽器廢棄)。

4. X-XSS-Protection

作用:?jiǎn)⒂脼g覽器的 XSS 過(guò)濾功能,防止跨站腳本攻擊。

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

配置說(shuō)明:

  • 1; mode=block:?jiǎn)⒂?XSS 過(guò)濾器并阻止加載惡意腳本的頁(yè)面。

5. X-Content-Type-Options

作用:防止瀏覽器對(duì)資源類(lèi)型進(jìn)行 MIME 嗅探,避免腳本注入攻擊。

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

配置說(shuō)明:

  • nosniff:強(qiáng)制瀏覽器使用 Content-Type 指定的 MIME 類(lèi)型。

6. Referrer-Policy

作用:控制 Referer 頭信息的發(fā)送,保護(hù)用戶隱私。

add_header Referrer-Policy "origin" always;

配置說(shuō)明:

  • origin:跨域請(qǐng)求時(shí)僅發(fā)送來(lái)源站點(diǎn)信息,不包括完整的 URL。

7. Permissions-Policy (原 Feature-Policy)

作用:限制瀏覽器功能的使用,防止濫用。

add_header Permissions-Policy "geolocation=(), microphone=(), camera=(), fullscreen=(self);";

配置說(shuō)明:

  • 禁用了地理定位、麥克風(fēng)、攝像頭功能。
  • 僅允許自身域使用全屏功能。

8. Cache-Control 和 Pragma

作用:控制緩存行為,防止敏感數(shù)據(jù)被緩存。

add_header Cache-Control "no-store" always;
add_header Pragma "no-cache" always;

配置說(shuō)明:

  • Cache-Control: no-store:禁止緩存頁(yè)面內(nèi)容。
  • Pragma: no-cache:兼容舊版 HTTP 協(xié)議的緩存控制頭。

9. Set-Cookie

作用:為 Cookie 添加安全屬性,防止 XSS 和中間人攻擊。

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

配置說(shuō)明:

  • HttpOnly:防止客戶端腳本訪問(wèn) Cookie,避免 XSS。
  • Secure:僅通過(guò) HTTPS 發(fā)送 Cookie。

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

作用: 限制跨域資源的加載,用于啟用跨域隔離。

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

配置說(shuō)明:

  • require-corp:僅允許跨域加載具有 CORS 或 Cross-Origin-Resource-Policy 標(biāo)頭的資源。

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

作用: 隔離文檔上下文,防止跨窗口攻擊。

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

配置說(shuō)明:

  • same-origin:僅允許相同源的頁(yè)面與當(dāng)前頁(yè)面共享瀏覽上下文。

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

作用: 限制資源的跨域加載。

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

配置說(shuō)明:

  • same-origin:僅允許資源從相同源加載。

為靜態(tài)資源啟用緩存

為靜態(tài)資源(如圖片、CSS、JS 文件)啟用緩存可以顯著提升性能,同時(shí)不會(huì)直接引發(fā)安全問(wèn)題。以下是推薦的配置:

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

配置說(shuō)明:

  • expires 1y:允許靜態(tài)資源緩存 1 年。
  • Cache-Control: public:標(biāo)記資源為公共可緩存。
  • access_log off:禁用訪問(wèn)日志,減少服務(wù)器存儲(chǔ)壓力。

完整示例配置

安全頭配置文件

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配置示例:

# 動(dòng)態(tài)設(shè)置允許的跨域來(lái)源
map $http_origin $cors_origin {
    default "";
    "~^https?://trusteddomain1.com$" $http_origin;
    "~^https?://trusteddomain2.com$" $http_origin;
}

# 動(dòng)態(tài)設(shè)置緩存時(shí)間
map $http_origin $cors_max_age {
    default "0";
    "~^https?://trusteddomain1.com$" "86400"; # 1 天
    "~^https?://trusteddomain2.com$" "3600";  # 1 小時(shí)
}
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;
  	}
    # 禁止敏感路徑訪問(wèn)
  	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;
		# 設(shè)置 CORS 響應(yīng)頭
		include /etc/nginx/conf.d/cors_headers.conf;
		# 如果是預(yù)檢請(qǐng)求 (OPTIONS),直接返回成功
		if ($request_method = 'OPTIONS') {
			# 設(shè)置 CORS 響應(yīng)頭
 			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

到此這篇關(guān)于Nginx 常用安全頭的使用小結(jié)的文章就介紹到這了,更多相關(guān)Nginx 常用安全頭內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

阿尔山市| 临漳县| 黔东| 萨迦县| 西盟| 武城县| 泊头市| 布尔津县| 额济纳旗| 嵊州市| 东阿县| 平和县| 吉林省| 买车| 方山县| 福建省| 裕民县| 延寿县| 杭锦旗| 越西县| 凤山县| 肇庆市| 师宗县| 吴川市| 西林县| 孟州市| 怀远县| 枣庄市| 涞源县| 个旧市| 灵山县| 达州市| 武定县| 平乐县| 凤山县| 长海县| 安义县| 来安县| 绥芬河市| 台中县| 郸城县|