1

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.

2 Answers2

3

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.

drookie
  • 8,625
  • 1
  • 19
  • 29
0

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

}
ursa
  • 101
  • 2