nginx:使用wwwauthentication的页面上的HSTS

是否有可能在nginx上发送Strict-Transport-Security头,即使在需要WWW身份validation的页面上也是如此。

当我同时拥有auth_basicadd_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="*********" 

它发送到不同的页面,不需要authentication,所以add_header指令有效果 – 只是当它需要authentication。

这是可能的,但不是与add_header指令,因为它没有做任何事情的401未经授权的回应。

ngx_http_headers_module 文档中的add_header指令说明ngx_http_headers_module

如果响应代码等于200,201,204,206,301,302,303,304或307,则将指定的字段添加到响应头。值可以包含variables。

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

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

从2014年9月16日发布的Nginx 1.7.5开始,通过在add_header指令中添加“ always ”标志很容易实现。 不再需要额外的模块。 🙂

 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