Skip to content

[Bug]: add_headers from multiple layers not getting merged #7830

Open
@dhatri308

Description

@dhatri308

Version

3.7.1

What Kubernetes platforms are you running on?

Openshift

Steps to reproduce

We have a main nginx and then bunch of Ingresses.
I tried adding custom if blocks in main nginx configmap location-snippets to add some headers conditionally.
While some of the Ingress resources did get this config but some did not.
Then I noticed that the Ingresses that did not get this change from main nginx configmap had their own location-snippets and that had a higher precedence than the main nginx's configmap's location-snippets.

I was hoping that the location-snippets from both layers would get merged but looks like that is not the case. Is this behavior expected ?

If I add the custom if blocks in Ingress location-snippets, the add_headers in http_context from main nginx gets masked. I was hoping that the headers from both would get merged. Is this an issue in nginx or works as expected ?

location / {
    # This block adds the necessary CORS headers if the Origin header is present
    if ($http_origin ~* "^https?://([a-z0-9-]+\.)*foobar\.com$") {
        add_header 'Access-Control-Allow-Origin' "$http_origin" always;
        add_header 'Access-Control-Allow-Credentials' 'true' always;
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE' always;
        add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range' always;
        add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always;
    }
 
    # Handle preflight OPTIONS requests
    if ($request_method = OPTIONS ) {
        add_header 'Access-Control-Allow-Origin' "$http_origin" always;
        add_header 'Access-Control-Allow-Credentials' 'true' always;
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE' always;
        add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range' always;
        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Content-Type' 'text/plain charset=UTF-8';
        add_header 'Content-Length' 0;
        return 204;
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugAn issue reporting a potential bugwaiting for responseWaiting for author's response

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions