Welcome! Log In Create A New Profile

Advanced

proxy_cache seems not working with X-Accel-Redirect

Posted by gray 
My config

location ~ /cached/ {
proxy_pass http://apache;
proxy_cache cache;
proxy_cache_valid 2h;
proxy_cache_key "$host|$request_uri";

}

location /htdocs_internal/ {
internal;

alias $htdocs_path;
}

Requests with header in reply X-Accel-Redirect not cached, every time
request is sent to apache. When i add these directives
proxy_pass_header X-Accel-Redirect;
proxy_ignore_headers X-Accel-Redirect;
cache works fine (but is useless :) ), so it isn't problem with "no cache"
headers from apache.

Posted at Nginx Forum: http://forum.nginx.org/read.php?2,241734,241734#msg-241734

_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
Maxim Dounin
Re: proxy_cache seems not working with X-Accel-Redirect
August 17, 2013 04:40AM
Hello!

On Fri, Aug 09, 2013 at 06:20:21AM -0400, gray wrote:

> My config
>
> location ~ /cached/ {
> proxy_pass http://apache;
> proxy_cache cache;
> proxy_cache_valid 2h;
> proxy_cache_key "$host|$request_uri";
>
> }
>
> location /htdocs_internal/ {
> internal;
>
> alias $htdocs_path;
> }
>
> Requests with header in reply X-Accel-Redirect not cached, every time
> request is sent to apache. When i add these directives
> proxy_pass_header X-Accel-Redirect;
> proxy_ignore_headers X-Accel-Redirect;
> cache works fine (but is useless :) ), so it isn't problem with "no cache"
> headers from apache.

Yes, proxy_cache can't cache responses with X-Accel-Redirect.

As a workaround, you may use an additional proxy layer with
proxy_cache and proxy_ignore_headers X-Accel-Redirect +
proxy_pass_headers X-Accel-Redirect.

--
Maxim Dounin
http://nginx.org/en/donation.html

_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
Hi Maxim,

Can you post an example please? I don't understand how to do that.

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,241734,271925#msg-271925

_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
Maxim Dounin
Re: proxy_cache seems not working with X-Accel-Redirect
January 11, 2017 03:40PM
Hello!

On Wed, Jan 11, 2017 at 07:39:32AM -0500, bdesemb wrote:

> Hi Maxim,
>
> Can you post an example please? I don't understand how to do that.

Try something like this:

server {
server 8080;

location / {
# no caching configured, only proxying to an intermediate
# caching layer; X-Accel-Redirect is processed here

proxy_pass http://127.0.0.1:8081;
}
}

server {
server 8081;

location / {
# here caching happens; to cache X-Accel-Redirect
# responses, the header is ignored

proxy_pass http://real-upstream-server;
proxy_cache foo;
proxy_ignore_headers X-Accel-Redirect;
}
}

--
Maxim Dounin
http://nginx.org/
_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
Thanks for your help but there is still something I don't understand. I have
dynamic access for resources. For me, I have 3 different locations in 2
servers and your conf only contains 2. I have my location of the client
request, the location of the X-Accel-Redirect and the location where caching
happens.

I tried this but it's not working. I got a 200 with an empty body.

server {
listen 80
location /data {
proxy_pass http://127.0.0.1:8081;
}

location /protected_data {
internal;
alias /var/local/data/;
}
}
listen 8081 {
location / {
proxy_pass http://127.0.0.1:8000; #upstream server
proxy_cache my_cache;
proxy_ignore_headers X-Accel-Redirect;
}
}

What am I doing wrong?

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,241734,271935#msg-271935

_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
I want to add some clarification. My client calls is like "/app" and the
server respond with a name. My file is stored at /var/local/data/<name>. I
want to cache the response. So if I have another request on /app, Nginx
should respond with the cached version of the file.

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,241734,271938#msg-271938

_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
Maxim Dounin
Re: proxy_cache seems not working with X-Accel-Redirect
January 11, 2017 06:50PM
Hello!

On Wed, Jan 11, 2017 at 12:23:08PM -0500, bdesemb wrote:

> I want to add some clarification. My client calls is like "/app" and the
> server respond with a name. My file is stored at /var/local/data/<name>. I
> want to cache the response. So if I have another request on /app, Nginx
> should respond with the cached version of the file.

It is not clear what do you mean by "cached version of the file",
and what exactly do you want to cache.

Original question you are referring to was about caching responses
with the X-Accel-Redirect header. Files as referenced in these
X-Accel-Redirect responses are expected to exist permanently. The
question was how to cache responses with X-Accel-Redirect header
to avoid asking backend application each time, and return files
directly instead (using cached X-Accel-Redirect responses). I've
provided the configuration which does this.

If in your case files are generated by your app and then removed,
then this configuration will not work for you. Instead, you need
to cache full responses at the frontend level, and use an
additional backend nginx to resolve X-Accel-Redirect redirections.
That is, something like this:

server {
server 8080;

location / {
# here caching happens
proxy_pass http://127.0.0.1:8081;
proxy_cache foo;
}
}

server {
server 8081;

location / {
# here X-Accel-Redirect is processed
proxy_pass http://real-upstream-server;
}

location /path/to/files {
# a location to access files after X-Accel-Redirect
...
}
}

--
Maxim Dounin
http://nginx.org/
_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
I made a diagram to be more specific. It's not a problem if the client ask
for /data and get an older version of the file. The invalidate option in
proxy_cache is enough for me.

Here is my diagram without cache: http://imgur.com/a/soq69

Obviously, I don't want to reach the upstream server every time but only if
the file hasn't been reached after x minutes.

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,241734,271962#msg-271962

_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
Sorry, only registered users may post in this forum.

Click here to login