-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathnginx-ssl.conf
More file actions
95 lines (82 loc) · 3.29 KB
/
Copy pathnginx-ssl.conf
File metadata and controls
95 lines (82 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# HTTP - 重定向到 HTTPS(保留 Let's Encrypt 验证路径)
server {
listen 80;
server_name xkcoding.com www.xkcoding.com;
# Let's Encrypt 验证路径
location /.well-known/acme-challenge/ {
root /var/www/html;
}
# 其他请求重定向到 HTTPS
location / {
return 301 https://$server_name$request_uri;
}
}
# HTTPS
server {
listen 443 ssl;
server_name xkcoding.com www.xkcoding.com;
# SSL 证书路径
ssl_certificate /etc/letsencrypt/live/xkcoding.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/xkcoding.com/privkey.pem;
# SSL 配置优化
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers off;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384;
ssl_session_timeout 1d;
ssl_session_cache shared:SSL:10m;
ssl_session_tickets off;
root /usr/share/nginx/html;
index index.html;
# Gzip 压缩
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_min_length 1000;
gzip_types text/plain text/css text/xml text/javascript application/javascript application/json application/xml application/rss+xml image/svg+xml;
# ==================== 安全头(全局) ====================
# HSTS - 强制 HTTPS,包含子域名
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always;
# 强制升级不安全请求(解决混合内容和证书错误内容问题)
add_header Content-Security-Policy "upgrade-insecure-requests" always;
# 防止点击劫持
add_header X-Frame-Options "SAMEORIGIN" always;
# 防止 MIME 类型嗅探
add_header X-Content-Type-Options "nosniff" always;
# XSS 防护
add_header X-XSS-Protection "1; mode=block" always;
# Referrer 策略
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
# ==================== 旧链接重定向 ====================
# Hexo 旧链接格式: /YYYY/MM/DD/slug.html → /YYYY-MM-DD-slug.html
# 必须放在 .html$ 规则之前,否则会被先匹配
location ~ "^/([0-9]{4})/([0-9]{2})/([0-9]{2})/(.+)\.html$" {
return 301 /$1-$2-$3-$4.html;
}
# 静态资源缓存
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
expires 1y;
add_header Cache-Control "public, immutable" always;
# 继承安全头
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always;
add_header Content-Security-Policy "upgrade-insecure-requests" always;
add_header X-Content-Type-Options "nosniff" always;
access_log off;
}
# HTML 不缓存
location ~* \.html$ {
expires -1;
add_header Cache-Control "no-store, no-cache, must-revalidate" always;
# 继承安全头
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always;
add_header Content-Security-Policy "upgrade-insecure-requests" always;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
}
# 主路由
location / {
try_files $uri $uri/ $uri.html =404;
}
# 404 错误页
error_page 404 /404.html;
}