{"id":12391,"date":"2022-09-02T10:15:34","date_gmt":"2022-09-02T02:15:34","guid":{"rendered":"http:\/\/lrxjmw.cn\/?p=12391"},"modified":"2022-09-02T21:34:37","modified_gmt":"2022-09-02T13:34:37","slug":"http-hsts-nginx","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/http-hsts-nginx.html","title":{"rendered":"HTTP HSTS\u534f\u8bae\u548c nginx"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nNetcraft \u516c\u53f8\u6700\u8fd1\u516c\u5e03\u4e86\u4ed6\u4eec\u68c0\u6d4bSSL\/TLS\u7f51\u7ad9\u7684\u7814\u7a76\uff0c\u5e76\u6307\u51fa\u53ea\u6709\u4ec5\u4ec55%\u7684\u7528\u6237\u6b63\u786e\u6267\u884c\u4e86HTTP\u4e25\u683c\u4f20\u8f93\u5b89\u5168HSTS\u3002\u672c\u6587\u4ecb\u7ecdnginx<\/a>\u5982\u4f55\u914d\u7f6eHSTS\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

 <\/p>\n

\u4ec0\u4e48\u662fHSTS<\/strong><\/div>\n

HTTPS(SSL\u548cTLS)\u786e\u4fdd\u7528\u6237\u548c\u7f51\u7ad9\u901a\u8baf\u8fc7\u7a0b\u4e2d\u5b89\u5168\uff0c\u4f7f\u653b\u51fb\u8005\u96be\u4e8e\u62e6\u622a\u3001\u4fee\u6539\u548c\u5047\u5192\u3002\u5f53\u7528\u6237\u624b\u52a8\u8f93\u5165\u57df\u540d\u6216http:\/\/\u94fe\u63a5\uff0c\u8be5\u7f51\u7ad9\u7684\u7b2c\u4e00\u4e2a\u8bf7\u6c42\u662f\u672a\u52a0\u5bc6\u7684\uff0c\u4f7f\u7528\u666e\u901a\u7684http\u3002\u6700\u5b89\u5168\u7684\u7f51\u7ad9\u7acb\u5373\u53d1\u9001\u56de\u4e00\u4e2a\u91cd\u5b9a\u5411\u4f7f\u7528\u6237\u5f15\u5411\u5230https\u8fde\u63a5\uff0c\u7136\u800c\uff0c\u4e2d\u95f4\u4eba\u653b\u51fb\u8005\u53ef\u80fd\u4f1a\u653b\u51fb\u62e6\u622a\u521d\u59cb\u7684http\u8bf7\u6c42\uff0c\u4ece\u800c\u63a7\u5236\u7528\u6237\u540e\u7eed\u7684\u56de\u8bdd\u3002<\/p>\n

\u81ea\u7136\u800c\u7136HSTS\u5e94\u8fd0\u800c\u751f\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e00\u6f5c\u5728\u7684\u5b89\u5168\u95ee\u9898\u3002\u5373\u65f6\u7528\u6237\u8f93\u5165\u57df\u540d\u6216http\u8fde\u63a5\uff0c\u6d4f\u89c8\u5668\u5c06\u4e25\u683c\u7684\u5347\u7ea7\u5230https\u8fde\u63a5\u3002<\/p>\n

\"hsts\"<\/p>\n

HSTS\u5982\u4f55\u5de5\u4f5c\u7684<\/strong><\/div>\n

HSTS\u7b56\u7565\u662f\u4ece\u5b89\u5168\u7684HTTPS\u7ad9\u70b9\u53d1\u9001\u7684HTTP\u54cd\u5e94\u5934\u90e8\u53d1\u5e03\u7684\u3002<\/p>\n

Strict-Transport-Security: max-age=31536000\r\n<\/pre>\n

\u5f53\u6d4f\u89c8\u5668\u4eceHTTPS\u7ad9\u70b9\u770b\u5230\u8fd9\u4e2a\u5934\u90e8\uff0c\u5c31\u77e5\u9053\u8be5\u57df\u540d\u53ea\u80fd\u901a\u8fc7HTTPS\uff08SSL \u6216\u8005 TLS\uff09\u8bbf\u95ee\u4e86\u3002\u5e76\u5c06\u6b64\u4fe1\u606f\u7f13\u5b58\u523031536000\uff0c\u4e5f\u5c31\u662f1\u5e74\u3002<\/p>\n

\u53ef\u9009\u7684\u53c2\u6570includeSubDomains\u544a\u8bc9\u6d4f\u89c8\u5668\u8be5\u7b56\u7565\u9002\u7528\u4e8e\u5f53\u524d\u57df\u4e0b\u7684\u6240\u6709\u5b50\u57df\u3002<\/p>\n

Strict-Transport-Security: max-age=31536000; includeSubDomains\r\n<\/pre>\n
nginx\u914d\u7f6eHSTS<\/strong><\/div>\n

\u5728nginx\u914d\u7f6e\u6587\u4ef6\u4e0a\u8bbe\u7f6eHSTS\u54cd\u5e94\u5934\u90e8\u3002<\/p>\n

add_header Strict-Transport-Security \"max-age=31536000; includeSubDomains\" always;\r\n<\/pre>\n

always \u53c2\u6570\u786e\u4fdd\u6240\u6709\u7684\u54cd\u5e94\u8bbe\u7f6e\u8be5\u5934\u90e8\uff0c\u5305\u62ec\u5185\u90e8\u4ea7\u751f\u7684\u9519\u8bef\u54cd\u5e94\u3002nginx\u7248\u672c\u65e9\u4e8e1.7.5\u4e0d\u652f\u6301\u8be5always\u53c2\u6570\u548c\u5185\u90e8\u4ea7\u751f\u7684\u9519\u8bef\u54cd\u5e94\u4e0d\u8bbe\u7f6e\u8be5\u5934\u90e8\u4fe1\u606f\u3002<\/p>\n

add_header\u6307\u4ee4\u7ee7\u627f\u89c4\u5219\uff1a<\/p>\n

nginx\u914d\u7f6e\u5757\u7ee7\u627fadd_header\u6307\u4ee4\u6240\u5728\u7684\u5c01\u88c5\u5757\uff0c\u56e0\u6b64\u53ea\u9700\u5c06add_header\u6307\u4ee4\u653e\u5728\u9876\u7ea7\u7684server\u5757\u3002\u6b64\u5916\u8fd8\u6709\u4e2a\u91cd\u8981\u7684\u4f8b\u5916\uff0c\u5982\u679c\u4e00\u4e2a\u5757\u5305\u542b\u4e86add_header\u6307\u4ee4\u672c\u8eab\uff0c\u5b83\u4e0d\u4f1a\u4ece\u5c01\u88c5\u5757\u7ee7\u627f\u8be5\u5934\u90e8\uff0c\u4f60\u9700\u8981\u91cd\u65b0\u5b9a\u4e49\u6240\u6709\u7684add_header\u6307\u4ee4\u3002<\/p>\n

server {\r\n    listen 443 ssl;\r\n \r\n    add_header Strict-Transport-Security \"max-age=31536000; includeSubDomains\" always;\r\n \r\n    # This 'location' block inherits the STS header\r\n    location \/ {\r\n        root \/usr\/share\/nginx\/html;\r\n    }\r\n \r\n    # Because this 'location' block contains another 'add_header' directive,\r\n    # we must redeclare the STS header\r\n    location \/servlet {\r\n        add_header X-Served-By \"My Servlet Handler\";\r\n        add_header Strict-Transport-Security \"max-age=31536000; includeSubDomains\" always;\r\n        proxy_pass http:\/\/localhost:8080;\r\n    }\r\n}\r\n<\/pre>\n

\u6d4b\u8bd5HTTP\u4e25\u683c\u4f20\u8f93\u5b89\u5168\uff1a<\/p>\n

\u4e00\u65e6\u7528\u6237\u63d0\u51faHSTS\u7b56\u7565\uff0c\u5b83\u7684\u7f13\u5b58\u4fe1\u606f\u671f\u7531max-age\u6307\u5b9a\u3002\u5728\u6b64\u671f\u95f4\uff0c\u6d4f\u89c8\u5668\u5c06\u4f1a\u62d2\u7edd\u901a\u8fc7\u672a\u52a0\u5bc6\u7684HTTP\u8bbf\u95eeweb\u670d\u52a1\uff0c\u5e76\u62d2\u7edd\u7ed9\u4e88\u4f8b\u5916\u8bc1\u4e66\u9519\u8bef\uff08\u5982\u679c\u8be5\u7f51\u7ad9\u4ee5\u524d\u63d0\u4ea4\u4e86\u4e00\u4e2a\u6709\u6548\u53ef\u4fe1\u7684\u8bc1\u4e66\uff09\u3002\u5982\u679c\u6307\u5b9a\u4e86\u4e00\u4e2aincludeSubDomanis\u53c2\u6570\uff0c\u8fd9\u4e9b\u9650\u5236\u4e5f\u540c\u6837\u9002\u7528\u4e8e\u5f53\u524d\u57df\u4e0b\u7684\u6240\u6709\u5b50\u57df\u3002<\/p>\n

\u5f53\u4f60\u6d4b\u8bd5HSTS\u65f6\uff0cmax-age\u65f6\u95f4\u8bbe\u7f6e\u77ed\u70b9\u3002<\/p>\n

\u662f\u5426\u6bcf\u4e2aHTTPS\u54cd\u5e94\u9700\u8981\u6709\u4e00\u4e2aSTS\u5934\u90e8\uff1a<\/p>\n

\u6211\u4eec\u7684\u76ee\u6807\u662f\u5f53\u7528\u6237\u5f00\u59cbHTTPS\u56de\u8bdd\u65f6\uff0c\u5c3d\u53ef\u80fd\u5feb\u7684\u5448\u73b0HSTS\u7b56\u7565\u3002\u5982\u679c\u4ed6\u4eec\u5728\u56de\u8bdd\u671f\u95f4\u63a5\u6536\u5230HSTS\u7b56\u7565\uff0c\u4ed6\u4eec\u4ecd\u7136\u5bb9\u6613\u53d7\u5230HTTP\u52ab\u6301\u653b\u51fb\u7684\u3002\u6d4f\u89c8\u5668\u53ea\u9700\u67e5\u770b\u4e00\u6b21STS\u5934\u90e8\uff0c\u56e0\u6b64\u5b83\u4e0d\u662f\u4e25\u683c\u5fc5\u8981\u5c06\u5b83\u6dfb\u52a0\u5230\u6bcf\u4e2a\u4f4d\u7f6e\u5757\u548c\u6bcf\u4e2a\u54cd\u5e94\u3002\u7136\u800c\uff0c\u53ea\u5728\u4e3b\u9875\u6216\u8005\u767b\u9646\u9875\u9762\u6dfb\u52a0\u5b83\u53ef\u80fd\u662f\u4e0d\u591f\u7684\uff0c\u5982\u679c\u4f60\u53ea\u6dfb\u52a0\u5230\u7f13\u5b58\u7684\u54cd\u5e94\uff0c\u5ba2\u6237\u7aef\u53ef\u80fd\u65e0\u6cd5\u770b\u5230\u5b83\u3002\u786e\u4fdd\u5c3d\u53ef\u80fd\u591a\u7684\u5408\u7406\u7684\u8986\u76d6\u5230\u4f60\u7684URL\uff0c\u7279\u522b\u6ce8\u610f\u52a8\u6001\u7684\u5185\u5bb9\u3002<\/p>\n

HTTP\u548cHTTPS\u5e76\u884c<\/strong><\/div>\n

\u6709\u65f6\u7f51\u7ad9\u9700\u8981\u540c\u65f6\u8fd0\u884c\u5728HTTP\u548cHTTPS\u4e0b<\/p>\n

server {\r\n    listen  80;\r\n    listen  443 ssl;\r\n    ...\r\n}\r\n<\/pre>\n

\u6709\u65f6\uff0c\u9700\u8981\u5c06http\u8bf7\u6c42\u91cd\u5b9a\u5411\u5230https<\/p>\n

server {\r\n    listen 80 default_server;\r\n    listen [::]:80 default_server;\r\n    server_name _;\r\n \r\n    # Discourage deep links by using a permanent redirect to home page of HTTPS site\r\n    return 301 https:\/\/$host;\r\n \r\n    # Alternatively, redirect all HTTP links to the matching HTTPS page\r\n    # return 301 https:\/\/$host$request_uri;\r\n}\r\n \r\nserver {\r\n    listen 443 ssl;\r\n    server_name www.ttlsa.com;\r\n \r\n    add_header Strict-Transport-Security \"max-age=31536000; includeSubDomains\" always;\r\n}\r\n<\/pre>\n
\u52a0\u5f3aHSTS<\/strong><\/div>\n

\u4fdd\u62a4\u5ba2\u6237\u7aef\u4eceHTTP\u62e6\u622a\uff0c\u4ece\u5b83\u770b\u5230STS\u5934\u90e8\u5230\u58f0\u660e\u7684max-age\u7684\u671f\u95f4\u5185\u3002\u7136\u800c\uff0cHSTS\u5e76\u4e0d\u662fHTTP\u56de\u8bdd\u52ab\u6301\u7684\u5b8c\u7f8e\u89e3\u51b3\u65b9\u6848\u3002\u7528\u6237\u4ecd\u7136\u5bb9\u6613\u53d7\u5230\u653b\u51fb\uff0c\u5982\u679c\u4ed6\u4eec\u901a\u8fc7HTTP\u8bbf\u95eeHSTS\u4fdd\u62a4\u7684\u7f51\u7ad9\u65f6\uff1a<\/p>\n

    \n
  1. \u4ee5\u524d\u4ece\u672a\u8bbf\u95ee\u8fc7\u8be5\u7f51\u7ad9<\/li>\n
  2. \u6700\u8fd1\u91cd\u65b0\u5b89\u88c5\u4e86\u5176\u64cd\u4f5c\u7cfb\u7edf<\/li>\n
  3. \u6700\u8fd1\u91cd\u65b0\u5b89\u88c5\u4e86\u5176\u6d4f\u89c8\u5668<\/li>\n
  4. \u5207\u6362\u5230\u65b0\u7684\u6d4f\u89c8\u5668<\/li>\n
  5. \u5207\u6362\u5230\u4e00\u4e2a\u65b0\u7684\u8bbe\u5907\u5982\u79fb\u52a8\u7535\u8bdd<\/li>\n
  6. \u5220\u9664\u6d4f\u89c8\u5668\u7684\u7f13\u5b58<\/li>\n
  7. \u6700\u8fd1\u6ca1\u8bbf\u95ee\u8fc7\u8be5\u7ad9\u5e76\u4e14max-age\u8fc7\u671f\u4e86<\/li>\n<\/ol>\n

    \u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0cgoogle\u575a\u6301\u7ef4\u62a4\u4e86\u4e00\u4e2a\u201cHSTS preload list\u201d\u7684\u7ad9\u70b9\u57df\u540d\u548c\u5b50\u57df\u540d\uff0c\u5e76\u901a\u8fc7https:\/\/hstspreload.appspot.com\/\u63d0\u4ea4\u5176\u57df\u540d\u3002\u8be5\u57df\u540d\u5217\u8868\u88ab\u5206\u53d1\u548c\u786c\u7f16\u7801\u5230\u4e3b\u6d41\u7684web\u6d4f\u89c8\u5668\u3002\u5ba2\u6237\u7aef\u8bbf\u95ee\u6b64\u5217\u8868\u4e2d\u7684\u57df\u540d\u5c06\u4e3b\u52a8\u7684\u4f7f\u7528HTTPS\uff0c\u5e76\u62d2\u7edd\u4f7f\u7528HTTP\u8bbf\u95ee\u8be5\u7ad9\u70b9\u3002<\/p>\n

    \u4e00\u65e6\u8bbe\u7f6e\u4e86STS\u5934\u90e8\u6216\u8005\u63d0\u4ea4\u4e86\u4f60\u7684\u57df\u540d\u5230HSTS\u9884\u52a0\u8f7d\u5217\u8868\uff0c\u8fd9\u662f\u4e0d\u53ef\u80fd\u5c06\u5176\u5220\u9664\u7684\u3002\u8fd9\u662f\u4e00\u4e2a\u5355\u5411\u7684\u51b3\u5b9a\u4f7f\u4f60\u7684\u57df\u540d\u901a\u8fc7HTTPS\u53ef\u7528\u7684\u3002<\/p>\n

    \n

    \u539f\u6587\u6765\u81ea\uff1ahttp:\/\/www.ttlsa.com\/nginx\/http-hsts-nginx\/<\/a><\/p>\n

    \u672c\u6587\u5730\u5740\uff1ahttp:\/\/lrxjmw.cn\/http-hsts-nginx.html<\/a>\u7f16\u8f91\uff1a\u6768\u9e4f\u98de\uff0c\u5ba1\u6838\u5458\uff1a\u9004\u589e\u5b9d<\/span><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"

    \u5bfc\u8bfb Netcraft \u516c\u53f8\u6700\u8fd1\u516c\u5e03\u4e86\u4ed6\u4eec\u68c0\u6d4bSSL\/TLS\u7f51\u7ad9\u7684\u7814\u7a76\uff0c\u5e76\u6307\u51fa\u53ea\u6709\u4ec5\u4ec55%\u7684\u7528\u6237\u6b63\u786e\u6267\u884c\u4e86HT […]<\/p>\n","protected":false},"author":63,"featured_media":7290,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-12391","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/12391","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/63"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=12391"}],"version-history":[{"count":11,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/12391\/revisions"}],"predecessor-version":[{"id":251900,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/12391\/revisions\/251900"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/7290"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=12391"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=12391"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=12391"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}