[Nginx] 4xx and 5xx retain custom Headers

Source: Internet
Author: User

[Nginx] 4xx and 5xx retain custom Headers
Problem

If the status code returned by the request is 404 or 50x, the custom header is still returned.

Analysis and Solution

The add_header section in the nginx document contains the following sentence:

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.

That is to say, add_header generally does not apply to 4xx and 5xx responses.

However, you can use the always keyword after version 1.7.5. Let's test it.

Configuration snippets

    location /hello {        add_header Access-Control-Allow-Origin * always;        return 404;    }

Test Results

curl -i http://127.0.0.1:9999/helloHTTP/1.1 404 Not FoundServer: openresty/1.9.3.1Date: Mon, 01 Feb 2016 11:17:59 GMTContent-Type: text/htmlContent-Length: 174Connection: keep-aliveAccess-Control-Allow-Origin: *

The test is successful.

Expansion module

You can also use headers-more-nginx-module written by chunge to do things. For more information, see the documentation.

REF http://serverfault.com/questions/418709/nginx-add-header-for-a-50-page

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.