multiple hsts headers问题

hsts

https://hstspreload.org/


现在ont.io.conf:

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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238

upstream newservice {
server 127.0.0.1:8988 weight=1;
}


upstream ontioglobal {
server 127.0.0.1:3001 weight=1;
}

upstream ontiochinese {
server 127.0.0.1:3003 weight=1;
}

server {
listen 443 ssl;
server_name ont.io;
client_max_body_size 80M;
access_log /data/install/nginx/logs/ont.io/access.log main;

location /wp {
root /data/install/wp;
index index.html index.htm;
}
location /googlea11162a0e175a9a9.html {
alias /data/install/googleprove/googlea11162a0e175a9a9.html;
}

location /api/v1/newsservice {
proxy_pass http://newservice/api/v1/newsservice;
}

location /api/v1/candidate {
proxy_pass http://newservice/api/v1/candidate;
}

location /api/v1/asset {
proxy_pass http://newservice/api/v1/asset;
}

location /sharding {
return https://github.com/ontio/documentation/blob/master/sharding/ontology-sharding.pdf;
}
location /certification/ {
proxy_pass http://127.0.0.1:9001/;
}

location /pdf{
root /data/install/ontology-website-pdf;
}
location /Cyberspace4Philosophers {
#减少点击劫持
add_header X-Frame-Options DENY;
#add_header 'Access-Control-Allow-Origin' 'XSSS';
add_header 'Access-Control-Allow-Origin' *;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,web-token,app-token,Authorization,Accept,Origin,Keep-Alive,User-Agent,X-Mx-ReqToken,X-Data-Type,X-Auth-Token,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
#add_header Strict-Transport-Security "max-age=63072000; includeSubDomains" always;
try_files $uri $uri/ /Cyberspace4Philosophers/index.html;
alias /data/install/ontology-4th-anniversary;
index index.html index.htm;
if ($geoip2_data_country_code = CN) {
return 403;
}
}



ssl_certificate /data/lego/_.ont.io.pem;
ssl_certificate_key /data/lego/_.ont.io.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssi on;
ssi_silent_errors on;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers EECDH+AESGCM:EDH+AESGCM;
#add_header Strict-Transport-Security "max-age=63072000; includeSubDomains" always;

ssl_prefer_server_ciphers on;


location / {
add_header X-Frame-Options DENY;
add_header 'Access-Control-Allow-Origin' *;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,web-token,app-token,Authorization,Accept,Origin,Keep-Alive,User-Agent,X-Mx-ReqToken,X-Data-Type,X-Auth-Token,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
#add_header Strict-Transport-Security "max-age=63072000; includeSubDomains" always;
proxy_pass http://ontioglobal;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload";
add_header X-Frame-Options "SAMEORIGIN";
add_header Referrer-Policy "no-referrer";
add_header X-Content-Type-Options "nosniff";
add_header Permissions-Policy "geolocation=(), microphone=(), camera=()";

index index.php index.htm index.html;
if ($geoip2_data_country_code = CN) {
return 403;
#proxy_pass http://ontiochinese;
}
# raindy 202300307 request: treat hk as global
#if ($geoip2_data_country_code = HK) {
#proxy_pass http://ontiochinese;
#return 403;
#}
#error_page 403 /403.html;
# location /403.html {
# allow all;
# root /data/install/ontio/public/403/;
# }

}
error_page 403 /403.html;
location = /403.html {
root /data/install/ontology-new/dist/server;
allow all;
}

}



server {
listen 443 ssl;
server_name www.ont.io;
client_max_body_size 80M;
access_log /data/install/nginx/logs/ont.io/access.log main;

location /wp {
root /data/install/wp;
index index.html index.htm;
}

location /api/v1/newsservice {
proxy_pass http://newservice/api/v1/newsservice;
}

location /api/v1/candidate {
proxy_pass http://newservice/api/v1/candidate;
}

location /api/v1/asset {
proxy_pass http://newservice/api/v1/asset;
}

location /sharding {
return https://github.com/ontio/documentation/blob/master/sharding/ontology-sharding.pdf;
}
location /certification/ {
proxy_pass http://127.0.0.1:9001/;
}

location /pdf{
root /data/install/ontology-website-pdf;
}
location /Cyberspace4Philosophers {
#减少点击劫持
add_header X-Frame-Options DENY;
#add_header 'Access-Control-Allow-Origin' 'XSSS';
add_header 'Access-Control-Allow-Origin' *;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,web-token,app-token,Authorization,Accept,Origin,Keep-Alive,User-Agent,X-Mx-ReqToken,X-Data-Type,X-Auth-Token,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';

#add_header Strict-Transport-Security "max-age=63072000; includeSubDomains" always;
add_header Referrer-Policy "no-referrer";
add_header X-Content-Type-Options "nosniff";
add_header Permissions-Policy "geolocation=(), microphone=(), camera=()";

try_files $uri $uri/ /Cyberspace4Philosophers/index.html;
alias /data/install/ontology-4th-anniversary;
index index.html index.htm;
if ($geoip2_data_country_code = CN) {
return 403;
}
}



ssl_certificate /data/lego/_.ont.io.pem;
ssl_certificate_key /data/lego/_.ont.io.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssi on;
ssi_silent_errors on;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers EECDH+AESGCM:EDH+AESGCM;

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


location / {
add_header X-Frame-Options DENY;
add_header 'Access-Control-Allow-Origin' *;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,web-token,app-token,Authorization,Accept,Origin,Keep-Alive,User-Agent,X-Mx-ReqToken,X-Data-Type,X-Auth-Token,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
#add_header Strict-Transport-Security "max-age=63072000; includeSubDomains" always;
proxy_pass http://ontioglobal;
index index.php index.htm index.html;
if ($geoip2_data_country_code = CN) {
#proxy_pass http://ontiochinese;
# rongyi, 20230807
return 403;
}
# raindy 202300307 request: treat hk as global
#if ($geoip2_data_country_code = HK) {
#proxy_pass http://ontiochinese;
#return 403;
#}
#error_page 403 /403.html;
# location /403.html {
# allow all;
# root /data/install/ontio/public/403/;
# }

}

error_page 403 /403.html;
location = /403.html {
root /data/install/ontology-new/dist/server;
allow all;
}

}





server {
listen 80;
server_name ont.io www.ont.io;
client_max_body_size 50m;
return 301 https://ont.io;
}


上面确实有2处没有被注释的 Strict-Transport-Security

去掉一个


按claudeAI给的建议,是这样的:

当然,我很乐意详细解释具体做了哪些改动。以下是主要的修改:

  1. 添加了全局HSTS设置:
    在配置文件的顶部添加了一个map指令:

    1
    2
    3
    map $scheme $hsts_header {
    https "max-age=31536000; includeSubDomains; preload";
    }

    这个设置允许我们只在HTTPS连接中发送HSTS头。

  2. 统一了安全头部设置:
    在每个server块的开始处,添加了一组统一的安全头部设置:

    1
    2
    3
    4
    5
    add_header Strict-Transport-Security $hsts_header always;
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header Referrer-Policy "no-referrer" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header Permissions-Policy "geolocation=(), microphone=(), camera=()" always;

    这些头部现在使用always参数,确保它们在所有响应中都被添加。

  3. 移除了重复的头部设置:
    原配置中,在多个location块中重复设置了一些头部。这些重复的设置已被移除,包括:

    • /Cyberspace4Philosophers位置块中的重复头部设置
    • 在根位置块(location /)中的重复头部设置
  4. 调整了HSTS头部:
    原配置中有多处不一致的HSTS设置,如:

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

    这些已被统一替换为使用$hsts_header变量的设置。

  5. 保持SSL配置不变:
    SSL相关的配置(证书、密码等)保持不变,但被移到了每个server块的顶部,以提高可读性。

  6. www.ont.io的server块应用相同的更改:
    为了保持一致性,对www.ont.io的server块应用了相同的头部设置和结构调整。

  7. 移除了注释掉的代码:
    一些被注释掉的配置(如关于香港的特殊处理)已被移除,以提高配置文件的清晰度。

  8. 保持其他功能不变:
    所有其他的location块、upstream定义、错误页面设置等都保持不变,确保现有功能不受影响。

这些修改的主要目的是:

  • 解决多个HSTS头部的问题
  • 提供一致的安全头部设置
  • 简化配置,提高可维护性
  • 确保HSTS只在HTTPS连接中发送

通过这些更改,您的Nginx配置现在应该能够一致地应用HSTS和其他安全头部,同时保持原有的功能完整。



Warning: Unnecessary HSTS header over HTTP

The HTTP page at http://ont.io sends an HSTS header. This has no effect over HTTP, and should be removed.


HSTS 配置:有 preload 和没有 preload 的对比

没有 preload 的 HSTS 配置

1
Strict-Transport-Security: max-age=31536000; includeSubDomains

特点:

  1. 浏览器第一次访问网站时通过 HTTPS 连接接收到此头部。
  2. 之后在 max-age 指定的时间内(此例中为一年),浏览器会自动将 HTTP 请求转换为 HTTPS。
  3. includeSubDomains 参数使此策略也适用于所有子域名。

有 preload 的 HSTS 配置

1
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload

特点:

  1. 包含所有没有 preload 的 HSTS 特点。
  2. 允许将网站提交到浏览器的 HSTS 预加载列表中。
  3. 一旦网站被添加到预加载列表,即使用户从未访问过该网站,浏览器也会强制使用 HTTPS。

主要区别

  1. 首次访问保护:

    • 无 preload:首次 HTTP 访问可能存在风险。
    • 有 preload:甚至首次访问也被保护,因为浏览器预先知道必须使用 HTTPS。
  2. 覆盖范围:

    • 无 preload:仅影响已访问过网站的用户。
    • 有 preload:影响所有使用支持 HSTS 预加载的浏览器的用户。
  3. 实施时间:

    • 无 preload:在用户首次通过 HTTPS 访问网站后立即生效。
    • 有 preload:需要网站所有者手动提交到浏览器的预加载列表,并等待下一次浏览器更新。
  4. 撤销难度:

    • 无 preload:相对容易撤销,等待 max-age 过期即可。
    • 有 preload:非常难以撤销,需要联系每个浏览器厂商并等待多个浏览器版本更新。
  5. 副作用:

    • 无 preload:影响相对较小,主要影响回头客。
    • 有 preload:影响巨大,可能影响所有潜在访问者,包括从未访问过网站的用户。

遇到的坑是,要看看外层的nginx.conf里面是不是有这个配置…要注释掉,不然一直有那个warning