nginx:带有 www-authentication 的页面上的 HSTS

nginx:带有 www-authentication 的页面上的 HSTS

在 nginx 上,即使在需要 WWW 身份验证的页面上,是否可以发送 Strict-Transport-Security 标头?

当我同时拥有auth_basic和时add_header Strict-Transport-Security "max-age=2592000";,不会发送 HSTS 标头:

$ curl -Ik https://****************
HTTP/1.1 401 Unauthorized
Server: nginx/1.4.6 (Ubuntu)
Date: Sun, 14 Sep 2014 17:56:08 GMT
Content-Type: text/html
Content-Length: 203
Connection: keep-alive
WWW-Authenticate: Basic realm="*********"

发送到不同的页面,不需要身份验证,因此该add_header指令有效 - 只是在需要身份验证时无效。

答案1

从 2014 年 9 月 16 日发布的 Nginx 1.7.5 开始,只需always在指令中添加“ ”标志即可轻松实现这一点add_header。不再需要额外的模块。:-)

add_header Strict-Transport-Security "max-age=2592000" always;

作为add_header文档现在解释:

如果always指定了该参数(1.7.5),则无论响应代码如何,都会添加标头字段。

来自我自己的一个Web应用程序(已编辑):

$ curl -I https://example.com/
HTTP/1.1 401 UNAUTHORIZED
Server: nginx/1.11.3
Date: Wed, 31 Aug 2016 15:37:59 GMT
Content-Type: text/plain; charset=utf-8
Content-Length: 17
Connection: keep-alive
WWW-Authenticate: Basic realm="example"
Strict-Transport-Security: max-age=31536000

答案2

这是可能的,但不是通过add_header指令来实现的,因为在 401 未授权响应的情况下它不会执行任何操作。

add_header 指令的说明ngx_http_headers_module 文档说:

当响应代码等于 200、201、204、206、301、302、303、304 或 307 时,将指定字段添加到响应头。值可以包含变量。

要在每个页面上发送 HSTS 标头,你必须使用ngx_headers_more模块(或者nginx-extras如果您使用的是 Debian,只需安装包),然后将以下行添加到您的 nginx 配置文件中:

more_set_headers "Strict-Transport-Security: max-age=31536000; includeSubDomains";

相关内容