{"id":1975,"date":"2023-01-31T14:35:28","date_gmt":"2023-01-31T14:35:28","guid":{"rendered":"https:\/\/c.ipcpu.com\/?p=1975"},"modified":"2023-07-31T14:46:02","modified_gmt":"2023-07-31T14:46:02","slug":"nginx-add_header-4xx-not-work","status":"publish","type":"post","link":"https:\/\/c.ipcpu.com\/2023\/01\/nginx-add_header-4xx-not-work\/","title":{"rendered":"Nginx\u4e2dadd_header\u9047\u4e0a4XX\u30015xx\u72b6\u6001\u7801\u4e0d\u751f\u6548"},"content":{"rendered":"

\u9047\u5230\u4e00\u4e2a\u95ee\u9898\uff0c\u5728nginx\u4e2d\u4f7f\u7528add_header\u65f6\uff0c\u53ea\u6709\u5f53\u8fd4\u56de\u7801\u662f2xx\uff0c3xx\u65f6\u624d\u751f\u6548\uff0c\u7f51\u4e0a\u4e00\u641c\u679c\u7136\uff0c\u627e\u5230\u4e86\u4e00\u4e2a\u5f88\u8be6\u7ec6\u7684\u89e3\u91ca\uff0c\u5982\u4e0b\uff1a<\/p>\n

For nginx >= 1.7.5\n\nAppend "always" to the header definition:\n\nadd_header 'Access-Control-Allow-Origin' '*' always;\nFor nginx < 1.7.5\n\nAccording to the nginx official document of ngx_header_module, the add_header can't work when response code is 400\n\nsyntax:     add_header name value;\ndefault:    \u2014\ncontext:    http, server, location, if in location\n\nAdds the specified field to a response header provided that the response code equals \n200, 201, 204, 206, 301, 302, 303, 304, or 307. A value can contain variables.\nIn another way, you can try the HttpHeadersMoreModule, which is more powerful.<\/code><\/pre>\n

\u6765\u6e90\uff1a https:\/\/stackoverflow.com\/questions\/20414669\/nginx-add-headers-when-returning-400-codes<\/a><\/p>\n

\u8f6c\u8f7d\u8bf7\u6ce8\u660e\uff1aIPCPU-\u7f51\u7edc\u4e4b\u8def<\/a> » Nginx\u4e2dadd_header\u9047\u4e0a4XX\u30015xx\u72b6\u6001\u7801\u4e0d\u751f\u6548<\/a><\/p>","protected":false},"excerpt":{"rendered":"

\u9047\u5230\u4e00\u4e2a\u95ee\u9898\uff0c\u5728nginx\u4e2d\u4f7f\u7528add_header\u65f6\uff0c\u53ea\u6709\u5f53\u8fd4\u56de\u7801\u662f2xx\uff0c3xx\u65f6\u624d\u751f\u6548\uff0c\u7f51\u4e0a\u4e00\u641c\u679c\u7136\uff0c\u627e\u5230\u4e86\u4e00\u4e2a\u5f88\u8be6\u7ec6\u7684\u89e3\u91ca\uff0c\u5982\u4e0b\uff1a For nginx >= 1.7.5 Append "always" to the header definition: add_header 'Access-Control-Allow-Origin' '*' always; For nginx < 1.7.5 According to the nginx official document of ngx_header_module, the add_header can't work when response code is 400 syntax: add_header name value; default: \u2014 context: http, server, location, if in location Adds the specified field to a […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[13],"tags":[14],"_links":{"self":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts\/1975"}],"collection":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/comments?post=1975"}],"version-history":[{"count":1,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts\/1975\/revisions"}],"predecessor-version":[{"id":1976,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts\/1975\/revisions\/1976"}],"wp:attachment":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/media?parent=1975"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/categories?post=1975"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/tags?post=1975"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}