**Nginx****靜態服務
1.****靜態資源型別
Nginx 作為靜態資源 Web 伺服器部署配置, 傳輸⾮常的⾼效, 常常⽤於靜態資源處理, 請求, 動靜分離
圖⽚壓縮案例
server {
listen 80;
server_name static.zgw.top;
root /soft/code/image;
index index.html index.htm ;
sendfile on;
access_log /var/log/nginx/static_access.log main;
location ~ .*\.(jpg|gif|png)$ {
valid_referers none blocked static.zgw.top;
if ($invalid_referer) {
return 403;
}
gzip on;
gzip_http_version 1.1;
gzip_comp_level 9;
gzip_types text/plain application/json application/x-javascript
application/css application/xml application/xml+rss text/javascript application/xhttpd-php image/jpeg image/gif image/png;
root /soft/code/image;
}
}
靜態資源跨域訪問
Nginx 跨域訪問配置
Syntax: add_header name value [always];
Default: —
Context: http, server, location, if in location
Access-Control-Allow-Origin
server {
listen 80;
server_name static.zgw.top;
sendfile on;
access_log /var/log/nginx/origin.access.log main;
error_log /var/log/nginx/origin.error.log error;
location / {
proxy_pass http://backend-server:port; # 替換為後端服務的 IP 地址和埠
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
server {
listen 80;
server_name origin.zgw.top;
sendfile on;
access_log /var/log/nginx/kuayu.log main;
location ~ .*\.(html|htm)$ {
root /soft/code/origin;
}
}
靜態資源防盜鏈
盜鏈指的是在⾃⼰的界⾯展示不在⾃⼰伺服器上的內容,透過技術⼿段獲得他⼈伺服器的資源地址,繞過別⼈資源
展示⻚⾯,在⾃⼰⻚⾯向⽤戶提供此內容,從⽽減輕⾃⼰伺服器的負擔,因為真實的空間和流量來⾃別⼈伺服器
防盜鏈設定思路: 區別哪些請求是⾮正常⽤戶請求
基於 http_refer 防盜鏈配置模組
Syntax: valid_referers none | blocked | server_names | string ...;
Default: —
Context: server, location
refer.conf
server {
listen 80;
server_name refer.zgw.top ;
root /soft/code/refer/;
index index.html index.htm;
}