Nginx如何应用proxy_ignore_headers只有匹配的位置不包含查询字符串时才设置cookie?

问题描述 投票:1回答:1

我需要请求相同位置的URI(root,homepage - /)不包含任何(或特定)查询字符串,然后才开始缓存并阻止在用户浏览器中设置cookie,这与代理服务器发送标题“set”相反-cookies“,请参阅我为此目的使用的代码:

nginx.conf(其中nGinx是前端缓存代理,Apache是​​后端Web服务器):

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;
                }

}

但不幸的是,使用proxy_ignore_headers和proxy_hide_header directivites内部条件如果{}(内部位置{}内部)是不可能/允许的,我有错误消息:

/etc/nginx/nginx.conf:145中不允许使用“proxy_ignore_headers”指令

所以这不行......

我花了很多时间在互联网上找到解决方案,但我没有找到类似用途的类似线程。

请帮助,如果知道任何解决方案如何解决我的例子,非常感谢提前!问候

nginx nginx-location nginx-config
1个回答
0
投票

您可以使用if语句跳转到另一个location块。选择一个不存在的URI(例如:/bogus)并将其用作internal位置块来处理不带参数的请求。

例如:

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;
}

有关详细信息,请参阅this document

© www.soinside.com 2019 - 2024. All rights reserved.