Score:0

Nginx - proxy_pass to another internal location block

mx flag

I am using Nginx to cache some responses. The backend that generates these responses, sets a common Cache-control header for all the responses. However, I need to cache some of the responses for a longer duration than the others. That is I need to modify the cache-control header before it is taken into consideration by the proxy_pass directive.

I am using the ngx_lua_module and want to modify the Cache-Control header in the internal location block using header_filter_by_lua_block directive. The intended config looks like the following:

location / {
    proxy_pass /actual;
    proxy_cache something;
}

location = /actual {
    internal;
    proxy_pass https://backend;
    proxy_cache off;
    header_filter_by_lua_block {
        -- modify cache-control header based on request/response parameters
    }
}

However I couldn't figure out a way to achieve this internal redirection via proxy_pass. I would appreciate any insight you have to make this work.

Score:1
za flag

You cannot proxy_pass to a location, you can only proxy_pass to an upstream or an URL (which basically is non-declared upstream). So, answering to your question formally, you should proxy_pass to localhost with Host header set to the current server_name; but this probably will overcomplicate things.

Instead - looks like all you need to do is to get rid of the location / {} that you don't need and then rename location = /actual to location / {}.

I'd also say that you don't need lua at all - just remove the header you're getting from proxied web with proxy_hide_header and add your own with add_header.

Score:0
mx flag

Generally speaking to pass control to another location block you should use internal redirects (rewrite), not proxy_pass:

location / {
    rewrite ^.*$ /actual;
}

To modify upstream headers you can use proxy_set_header:

location /actual {
    proxy_set_header Cache-Control '<your value>';
}

To modify downstream headers you can use more_set_headers. It requires custom Nginx build with additional module, but it's really powerful in your case:

location /actual {
    more_set_headers 'Cache-Control: <your value>';
}

Taking into account the title of the question you can also make hardcore things like switching servers to handle clients traffic. I would NOT recommend it for such trivial tasks, but in rare cases it could help:

http {

    upstream internal_http_routing {
        server         unix:var/internal.sock;
    }

    server {
        # Internal interface
        listen         unix:var/internal.sock;

        location / {
            return     200;
        }
    }

    server {
        # Client-facing interface
        listen         443 ssl;

        location / {
            proxy_pass http://internal_http_routing;
        }
    }

}

tcp {

    upstream internal_tcp_routing {
        server         unix:var/internal.sock;
    }

    server {
        # Client-facing interface
        listen         8443 ssl;
        proxy_pass     internal_tcp_routing;
    }

}
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.