最新消息:

Nginx中add_header遇上4XX、5xx状态码不生效

Linux ipcpu 127浏览 0评论

遇到一个问题,在nginx中使用add_header时,只有当返回码是2xx,3xx时才生效,网上一搜果然,找到了一个很详细的解释,如下:

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:    —
context:    http, server, location, if in location

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.
In another way, you can try the HttpHeadersMoreModule, which is more powerful.

来源: https://stackoverflow.com/questions/20414669/nginx-add-headers-when-returning-400-codes

转载请注明:IPCPU-网络之路 » Nginx中add_header遇上4XX、5xx状态码不生效

发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址