tags:

views:

54

answers:

3

Hi

I'm using X-Accel-Redirect (so implicitly nginx), Content-Type and Content-Disposition to download a file, everything works great.

What I need to accomplish is redirecting to a new location after the download starts.

I've tested with both Refresh and Location, it doesn't work. Is it possible with HTTP 1.1/nginx?

Addendum

I am looking for a HTTP-only approach. No javascript, no <meta>.

The download starts after processing a POST form.

A: 

I would change the original download link to open in a new window (target="_blank") and use javascript to redirect. There are several variations you could try based on that.

Oli
A: 
<?php

header('refresh:5;url=http://www.google.com/');
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="file.pdf"');
readfile('file.pdf');

That will redirect you to google.com in 5 seconds after the d/l starts ;)

Oblio
readfile() puts PHP to work senselessly. I want to use the right tool for the job, as I already do by using nginx' X-Accel-Redirect.
Flavius
+1  A: 

It's not possible as soon as Nginx is already sending the response. So the only way seems to return 302 Redirect from Nginx location you are referring in X-Accel-Redirect

How would you tell Nginx to do this?

The best way is to pass a custom header to Nginx (along with XAR) from the application

How to trigger processing in Nginx?

You can easily get the header's value using variable $http_VARIABLE, so typical piece of Nginx configuration looks like

if ($http_your_header) {
  rewrite ^ http://your_redirect redirect;
}

See Nginx rewrite module documentation and Nginx HTTP header variable for more information.

Alexander Azarov
How could I instruct nginx from within my application (which issues X-Accel-Redirect) to take a parameter (from my application in this case) and do a 302 after starting to serve that resource? Again, without involving PHP or any other "user code" beside nginx.Beside that, I +1 it, since it gets the closest to what I actually asked.
Flavius
Pass a custom header to Nginx (along with XAR)
Alexander Azarov
How to use the value of that header in nginx and do the 302 with it?
Flavius
`if ($http_your_header) { rewrite ^ http://your_redirect redirect; }` See http://wiki.nginx.org/NginxHttpRewriteModule and http://wiki.nginx.org/NginxHttpCoreModule#.24http_HEADER
Alexander Azarov
Thanks. You could put that in the answer as well.
Flavius