2

I need when requested URI of the same location (root, homepage - /) does not contain any (or specific) query strings, only then start caching and prevent set cookie in user browser, opposite the fact that proxied server was sending header "set-cookies", pls see code what i tried for this purpose use:

nginx.conf (where nGinx is front-end cache proxy and Apache is back-end webserver):

location = / { 
        proxy_pass  https://115.xx.xx.xx:8443;   
        proxy_cache             my_cache;
        proxy_cache_valid       10m;


            # only if there is no query strings in request (URI without any variables, 
            # for example, exact is: https://mydomain.xyz/ ), only then ignore 
            # Set-Cookies header what means in result cache page and prevent to 
            # client's browser set cookie what webpage sent = apply following 
            # proxy_ignore_headers and proxy_hide_header commands:

                if ($args ~ "^$") { 
                    ## if req URI is https://mydomain.xyz/ , then:

                       proxy_ignore_headers    Set-Cookie;
                       proxy_hide_header       Set-Cookie;
                }



            # but when the same URI (location) contains query string (for example: 
            # https://mydomain.xyz/?rand=4564894653465), then let cache 
            # automatically disabled (as default, when nGinx see that the webpages 
            # sent header "Set-Cookie", then does not cache answer page) and allow 
            # set this cookie in user's browser (as webpage requested it):

                if ($args ~ "^rand=[:digit:]+") {  
                    ## if req URI is https://mydomain.xyz/?rand=45648 , then:

                       proxy_pass_header   Set-Cookie;
                }

}

But unfortunately, use of proxy_ignore_headers and proxy_hide_header directivites inside condition if { } (what is inside location { }) is not possible / allowed and i have error message:

"proxy_ignore_headers" directive is not allowed here in /etc/nginx/nginx.conf:145

so this cannot work...

I spent a lot time to find solution on internet but i not found similar thread with similar purpose ever.

Please help if know any solution how resolve my example, many thanks in advance! regards

Milos Xk
  • 21
  • 1
  • 3

1 Answers1

0

You could use your if statement to jump to another location block. Select a non-existent URI (for example: /bogus) and use that as an internal location block to process the request with no arguments.

For example:

proxy_cache             my_cache;
proxy_cache_valid       10m;

location = / { 
    proxy_pass  https://115.xx.xx.xx:8443;   
    if ($args = "") {
        rewrite ^ /bogus last;
    }

    ...
}
location = /bogus {
    internal;
    proxy_pass  https://115.xx.xx.xx:8443;   
    proxy_ignore_headers    Set-Cookie;
    proxy_hide_header       Set-Cookie;
}

See this document for details.

Richard Smith
  • 45,711
  • 6
  • 82
  • 81