大网数据广告活动
服务器运维问答

HSTS in Nginx: should the Strict-Transport-Security header be added in subdomain server blocks too?

· 52CE 我爱测

Let's take the following nginx.conf configuration file with server blocks for example.com and subdomain.example.com : http { ... server { listen [::]:80 ipv6only=off default_server; server_name example.com; return 301 https://exa…

问题原文

原帖链接 · Will · CC BY-SA 4.0

Let's take the following nginx.conf configuration file with server blocks for example.com and subdomain.example.com:

http {
    ...

    server {
        listen [::]:80 ipv6only=off default_server;
        server_name example.com;
        return 301 https://example.com$request_uri;
    }
    server {
        listen [::]:443 ipv6only=off ssl default_server;
        server_name example.com;
        add_header Strict-Transport-Security
            "max-age=63072000; includeSubDomains; preload" always;
        ...
    }

    server {
        listen [::]:80 ipv6only=off;
        server_name subdomain.example.com;
        return 301 https://subdomain.example.com$request_uri;
    }
    server {
        listen [::]:443 ipv6only=off ssl;
        server_name subdomain.example.com;
        add_header Strict-Transport-Security
            "max-age=63072000; includeSubDomains; preload" always; # <-- again ???
        ...
    }
}

The includeSubDomains part of the header apparently tells the browser that the header applies to all subdomains as well.

However, if that browser were to visit subdomain.example.com before ever seeing example.com, that wouldn't be of any help, would it? So to cover for this scenario, I need to add the same add_header in all subdomain server blocks too ...right?

已采纳解答

原帖链接 · Esa Jokinen · CC BY-SA 4.0

You are correct that it's better to have the HSTS Strict-Transport-Security header everywhere you need it to make sure a client gets it even if sub.example.com is accessed before example.com or if the cached HSTS information has expired.

The includeSubDomains flag affects all subdomains of where it's present. This means that includeSubDomains on sub.example.com takes effect on *.sub.example.com instead of *.example.com. (This is only natural as it would be bad if e.g. example.co.uk could affect *.co.uk.)

  • If you don't use any sub.sub.example.com you could leave the Strict-Transport-Security header of your subdomains without this flag.

  • subA.example.com can't protect subB.example.com.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-05-27)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。