问题
nginx使用中,如果请求返回的状态code类似404或者50x这种,仍然返回自定义的header。
分析和解决
nginx文档中关于 add_header的部分 有这么一句
adds the specified field to a response header provided that the response code equals 200, 201, 204, 206, 301, 302, 303, 304, or 307. a value can contain variables.
也就是说 add_header 一般是不会作用在 4xx,5xx的响应上的。
但是从 1.7.5版本之后可以使用 always关键字来解决,下面来测试下。
配置片段
location /hello { add_header access-control-allow-origin* always; return404; }
测试结果
curl -i http://127.0.0.1:9999/hellohttp/1.1404not foundserver: openresty/1.9.3.1date: mon, 01 feb 201611:17:59 gmtcontent-type: text/htmlcontent-length: 174connection: keep-aliveaccess-control-allow-origin: *
可以看到测试成功。
拓展模块另外还可以使用 春哥写的 headers-more-nginx-module 来做个事情,具体请参考文档。
refhttp://serverfault.com/questions/418709/nginx-add-header-for-a-50-page').addclass('pre-numbering').hide(); $(this).addclass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadein(1700); }); }); 以上就介绍了4xx,5xx 保持自定义header,包括了方面的内容,希望对php教程有兴趣的朋友有所帮助。