Nginx 常用安全頭的使用小結(jié)
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)。使用
nonce或hash機(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;
#}
}
到此這篇關(guān)于Nginx 常用安全頭的使用小結(jié)的文章就介紹到這了,更多相關(guān)Nginx 常用安全頭內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Nginx如何獲取自定義請(qǐng)求header頭和URL參數(shù)詳解
- 為nginx設(shè)置默認(rèn)虛擬主機(jī)(空主機(jī)頭,默認(rèn)主機(jī)頭)
- Nginx代理時(shí)header頭中帶"_"信息丟失問(wèn)題的解決
- Nginx 操作響應(yīng)頭信息的實(shí)現(xiàn)
- 關(guān)閉nginx空主機(jī)頭 防止nginx空主機(jī)頭及惡意域名指向
- 詳解nginx請(qǐng)求頭數(shù)據(jù)讀取流程
- 使用Nginx代理解決跨域問(wèn)題并傳遞請(qǐng)求頭的完整指南
- Nginx隱藏server頭信息的實(shí)現(xiàn)
- Nginx請(qǐng)求頭丟失的問(wèn)題解決
相關(guān)文章
Nginx Rewrite使用場(chǎng)景及代碼案例詳解
這篇文章主要介紹了Nginx Rewrite使用場(chǎng)景及代碼案例詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
如何解決Nginx請(qǐng)求轉(zhuǎn)發(fā)將POST變?yōu)镚ET問(wèn)題
這篇文章主要介紹了如何解決Nginx請(qǐng)求轉(zhuǎn)發(fā)將POST變?yōu)镚ET問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
Nginx進(jìn)階配置實(shí)現(xiàn)SSL證書(shū)部署與資源防盜鏈實(shí)操
本文主要介紹了在Web服務(wù)運(yùn)維中使用Nginx進(jìn)行SSL證書(shū)部署,實(shí)現(xiàn)HTTPS加密訪問(wèn),強(qiáng)制HTTP轉(zhuǎn)HTTPS以及配置Nginx防盜鏈保護(hù)靜態(tài)資源安全的具體方法,感興趣的可以了解一下2026-04-04
Nginx圖片服務(wù)器配置之后圖片訪問(wèn)404的問(wèn)題解決
本文主要介紹了Nginx圖片服務(wù)器配置之后圖片訪問(wèn)404的問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
Nginx 訪問(wèn) /root/下 403 Forbidden問(wèn)題解決
在使用Nginx作為Web服務(wù)器時(shí),可能會(huì)遇到403 Forbidden錯(cuò)誤,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-05-05
nginx 配置虛擬主機(jī),實(shí)現(xiàn)在一個(gè)服務(wù)器可以訪問(wèn)多個(gè)網(wǎng)站的方法
下面小編就為大家分享一篇nginx 配置虛擬主機(jī),實(shí)現(xiàn)在一個(gè)服務(wù)器可以訪問(wèn)多個(gè)網(wǎng)站的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
LNMPA遇到504 Gateway time-out錯(cuò)誤的解決方法
這篇文章主要介紹了LNMPA遇到504 Gateway time-out錯(cuò)誤的解決方法,需要的朋友可以參考下2017-07-07
使用nginx如何解決Access-Control-Allow-Origin問(wèn)題
這篇文章主要介紹了使用nginx如何解決Access-Control-Allow-Origin問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01

