{"id":50663,"date":"2024-10-08T12:51:14","date_gmt":"2024-10-08T04:51:14","guid":{"rendered":"https:\/\/server.hk\/cnblog\/50663\/"},"modified":"2024-10-08T12:51:14","modified_gmt":"2024-10-08T04:51:14","slug":"nginx-%e5%ae%89%e5%85%a8%e7%ad%96%e7%95%a5%ef%bc%9a%e8%a8%ad%e7%bd%ae-x-xss-protection-%e9%a0%ad","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/50663\/","title":{"rendered":"Nginx \u5b89\u5168\u7b56\u7565\uff1a\u8a2d\u7f6e X-XSS-Protection \u982d"},"content":{"rendered":"<h1 id=\"nginx-%e5%ae%89%e5%85%a8%e7%ad%96%e7%95%a5%ef%bc%9a%e8%a8%ad%e7%bd%ae-x-xss-protection-%e9%a0%ad-iWBnkACrvc\">Nginx \u5b89\u5168\u7b56\u7565\uff1a\u8a2d\u7f6e X-XSS-Protection \u982d<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u74b0\u5883\u4e2d\uff0c\u7db2\u7ad9\u5b89\u5168\u6027\u8b8a\u5f97\u8d8a\u4f86\u8d8a\u91cd\u8981\u3002\u96a8\u8457\u7db2\u7d61\u653b\u64ca\u624b\u6bb5\u7684\u65e5\u76ca\u589e\u591a\uff0c\u7db2\u7ad9\u7ba1\u7406\u8005\u5fc5\u9808\u63a1\u53d6\u6709\u6548\u7684\u5b89\u5168\u63aa\u65bd\u4f86\u4fdd\u8b77\u7528\u6236\u6578\u64da\u548c\u7db2\u7ad9\u5b8c\u6574\u6027\u3002Nginx \u4f5c\u70ba\u4e00\u500b\u9ad8\u6548\u7684\u7db2\u9801\u4f3a\u670d\u5668\uff0c\u63d0\u4f9b\u4e86\u591a\u7a2e\u5b89\u5168\u914d\u7f6e\u9078\u9805\uff0c\u5176\u4e2d\u4e4b\u4e00\u5c31\u662f\u8a2d\u7f6e X-XSS-Protection \u982d\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af-x-xss-protection-%e9%a0%ad%ef%bc%9f-iWBnkACrvc\">\u4ec0\u9ebc\u662f X-XSS-Protection \u982d\uff1f<\/h2>\n<p>X-XSS-Protection \u662f\u4e00\u500b HTTP \u97ff\u61c9\u982d\uff0c\u7528\u65bc\u555f\u7528\u6216\u7981\u7528\u700f\u89bd\u5668\u7684\u8de8\u7ad9\u8173\u672c\uff08XSS\uff09\u904e\u6ffe\u5668\u3002\u9019\u500b\u982d\u90e8\u4e3b\u8981\u7528\u65bc\u9632\u6b62 XSS \u653b\u64ca\uff0c\u9019\u662f\u4e00\u7a2e\u5e38\u898b\u7684\u7db2\u7d61\u653b\u64ca\u65b9\u5f0f\uff0c\u653b\u64ca\u8005\u901a\u904e\u5728\u7db2\u9801\u4e2d\u6ce8\u5165\u60e1\u610f\u8173\u672c\u4f86\u7aca\u53d6\u7528\u6236\u4fe1\u606f\u6216\u57f7\u884c\u5176\u4ed6\u4e0d\u7576\u64cd\u4f5c\u3002<\/p>\n<h2 id=\"x-xss-protection-%e7%9a%84%e5%b7%a5%e4%bd%9c%e5%8e%9f%e7%90%86-iWBnkACrvc\">X-XSS-Protection \u7684\u5de5\u4f5c\u539f\u7406<\/h2>\n<p>\u7576\u700f\u89bd\u5668\u63a5\u6536\u5230\u5305\u542b X-XSS-Protection \u982d\u7684\u97ff\u61c9\u6642\uff0c\u5b83\u6703\u6839\u64da\u8a72\u982d\u7684\u503c\u4f86\u6c7a\u5b9a\u662f\u5426\u555f\u7528 XSS \u904e\u6ffe\u5668\u3002\u9019\u500b\u982d\u7684\u5e38\u898b\u503c\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><code>0<\/code>\uff1a\u7981\u7528 XSS \u904e\u6ffe\u5668\u3002<\/li>\n<li><code>1<\/code>\uff1a\u555f\u7528 XSS \u904e\u6ffe\u5668\uff0c\u4e26\u5728\u6aa2\u6e2c\u5230 XSS \u653b\u64ca\u6642\u963b\u6b62\u9801\u9762\u52a0\u8f09\u3002<\/li>\n<li><code>1; mode=block<\/code>\uff1a\u555f\u7528 XSS \u904e\u6ffe\u5668\uff0c\u4e26\u5728\u6aa2\u6e2c\u5230\u653b\u64ca\u6642\u963b\u6b62\u9801\u9762\u52a0\u8f09\uff0c\u800c\u4e0d\u662f\u986f\u793a\u932f\u8aa4\u9801\u9762\u3002<\/li>\n<\/ul>\n<p>\u4f8b\u5982\uff0c\u7576\u8a2d\u7f6e\u70ba <code>1; mode=block<\/code> \u6642\uff0c\u5982\u679c\u700f\u89bd\u5668\u6aa2\u6e2c\u5230\u6f5b\u5728\u7684 XSS \u653b\u64ca\uff0c\u5b83\u5c07\u4e0d\u6703\u986f\u793a\u9801\u9762\uff0c\u800c\u662f\u986f\u793a\u4e00\u500b\u7a7a\u767d\u9801\u9762\uff0c\u9019\u6a23\u53ef\u4ee5\u6709\u6548\u9632\u6b62\u653b\u64ca\u8005\u7684\u60e1\u610f\u8173\u672c\u57f7\u884c\u3002<\/p>\n<h2 id=\"%e5%a6%82%e4%bd%95%e5%9c%a8-nginx-%e4%b8%ad%e8%a8%ad%e7%bd%ae-x-xss-protection-%e9%a0%ad-iWBnkACrvc\">\u5982\u4f55\u5728 Nginx \u4e2d\u8a2d\u7f6e X-XSS-Protection \u982d<\/h2>\n<p>\u5728 Nginx \u4e2d\u8a2d\u7f6e X-XSS-Protection \u982d\u975e\u5e38\u7c21\u55ae\u3002\u60a8\u53ea\u9700\u5728 Nginx \u914d\u7f6e\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u78bc\uff1a<\/p>\n<pre><code>add_header X-XSS-Protection \"1; mode=block\";<\/code><\/pre>\n<p>\u9019\u884c\u4ee3\u78bc\u53ef\u4ee5\u653e\u5728 server \u5340\u584a\u6216 location \u5340\u584a\u4e2d\uff0c\u5177\u9ad4\u53d6\u6c7a\u65bc\u60a8\u5e0c\u671b\u61c9\u7528\u6b64\u8a2d\u7f6e\u7684\u7bc4\u570d\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u793a\u4f8b\uff1a<\/p>\n<pre><code>server {\n    listen 80;\n    server_name example.com;\n\n    add_header X-XSS-Protection \"1; mode=block\";\n\n    location \/ {\n        root \/var\/www\/html;\n        index index.html index.htm;\n    }\n}<\/code><\/pre>\n<p>\u5b8c\u6210\u914d\u7f6e\u5f8c\uff0c\u8acb\u8a18\u5f97\u91cd\u65b0\u52a0\u8f09 Nginx \u4ee5\u4f7f\u66f4\u6539\u751f\u6548\uff1a<\/p>\n<pre><code>sudo systemctl reload nginx<\/code><\/pre>\n<h2 id=\"%e5%85%b6%e4%bb%96%e5%ae%89%e5%85%a8%e5%bb%ba%e8%ad%b0-iWBnkACrvc\">\u5176\u4ed6\u5b89\u5168\u5efa\u8b70<\/h2>\n<p>\u9664\u4e86\u8a2d\u7f6e X-XSS-Protection \u982d\uff0c\u9084\u6709\u5176\u4ed6\u4e00\u4e9b\u5b89\u5168\u63aa\u65bd\u53ef\u4ee5\u9032\u4e00\u6b65\u589e\u5f37\u7db2\u7ad9\u7684\u5b89\u5168\u6027\uff1a<\/p>\n<ul>\n<li>\u8a2d\u7f6e Content Security Policy (CSP)\uff1a\u9019\u662f\u4e00\u7a2e\u5f37\u5927\u7684\u9632\u8b77\u63aa\u65bd\uff0c\u53ef\u4ee5\u9650\u5236\u7db2\u7ad9\u4e0a\u53ef\u57f7\u884c\u7684\u5167\u5bb9\u4f86\u6e90\u3002<\/li>\n<li>\u4f7f\u7528 HTTPS\uff1a\u52a0\u5bc6\u7db2\u7ad9\u6d41\u91cf\uff0c\u4fdd\u8b77\u7528\u6236\u6578\u64da\u4e0d\u88ab\u7aca\u53d6\u3002<\/li>\n<li>\u5b9a\u671f\u66f4\u65b0 Nginx \u548c\u5176\u4ed6\u76f8\u95dc\u8edf\u4ef6\uff1a\u4fdd\u6301\u7cfb\u7d71\u66f4\u65b0\uff0c\u4ee5\u9632\u6b62\u5df2\u77e5\u6f0f\u6d1e\u88ab\u5229\u7528\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-iWBnkACrvc\">\u7e3d\u7d50<\/h2>\n<p>\u8a2d\u7f6e X-XSS-Protection \u982d\u662f\u589e\u5f37\u7db2\u7ad9\u5b89\u5168\u6027\u7684\u4e00\u500b\u91cd\u8981\u6b65\u9a5f\u3002\u901a\u904e\u6b63\u78ba\u914d\u7f6e Nginx\uff0c\u60a8\u53ef\u4ee5\u6709\u6548\u5730\u9632\u6b62 XSS \u653b\u64ca\uff0c\u4fdd\u8b77\u7528\u6236\u6578\u64da\u548c\u7db2\u7ad9\u5b8c\u6574\u6027\u3002\u9664\u4e86 X-XSS-Protection\uff0c\u9084\u61c9\u8003\u616e\u5176\u4ed6\u5b89\u5168\u63aa\u65bd\uff0c\u5982\u8a2d\u7f6e Content Security Policy \u548c\u4f7f\u7528 HTTPS\uff0c\u4ee5\u9032\u4e00\u6b65\u63d0\u5347\u7db2\u7ad9\u7684\u5b89\u5168\u6027\u3002\u5982\u679c\u60a8\u9700\u8981\u66f4\u591a\u6709\u95dc <a href=\"https:\/\/server.hk\">\u9999\u6e2f VPS<\/a> \u548c\u5176\u4ed6\u4f3a\u670d\u5668\u89e3\u6c7a\u65b9\u6848\u7684\u4fe1\u606f\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3\u5982\u4f55\u5728 Nginx \u4e2d\u8a2d\u7f6e X-XSS-Protection \u982d\uff0c\u4ee5\u589e\u5f37\u7db2\u7ad9\u5b89\u5168\u6027\uff0c\u9632\u6b62\u8de8\u7ad9\u8173\u672c\u653b\u64ca\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4924],"tags":[],"class_list":["post-50663","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/50663","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=50663"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/50663\/revisions"}],"predecessor-version":[{"id":50664,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/50663\/revisions\/50664"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=50663"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=50663"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=50663"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}