Special headers and X-Accel-Redirect

Hi guys,

I have small problem with file download using X-Accel-Redirect. I am
using Nginx as proxy before some REST API application, some of the
requests are for big files which are located at other host - so I am
using X-Accel-Redirect to download them. But I have also some metadata
connected with file which I want to return in X-metadata header from
REST API.

So, here is my config:

server {
    listen 80;
    listen 443 ssl;

    server_name some-service.pl;

    ssl_certificate /etc/ssl/certs/some-service.pl.pem;
    ssl_certificate_key /etc/ssl/private/some-service.pl.key;

    client_max_body_size 100M;
    client_body_buffer_size 128k;

    location / {
        if ($http_x_user = "") {
          return 502;
        }

        proxy_pass http://api;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For

$proxy_add_x_forwarded_for; # client, proxy1, proxy2, …

    }

    location /files {
        internal;
        rewrite ^/files(.*)$ $1 break;

        proxy_pass http://filestore;
        proxy_hide_header Content-Type;
        proxy_hide_header Content-Disposition;

        set $x_metadata $upstream_http_x_metadata;

        add_header X-metadata $x_metadata;
    }
}

I am using this trick with set to temporary variable and add_header
with this variable (which I’ve found somewhere on this list), but this
is not working. Even simple:

set $x_std “testest”;
add_header X-metadata $x_std;

does not work for me.

Could please somebody tell me what could be wrong?

Thanks,


kgs

Oh, and version:

nginx: nginx version: nginx/1.0.4
nginx: TLS SNI support enabled
nginx: configure arguments: --with-http_ssl_module
–with-http_stub_status_module --with-http_realip_module
–with-http_gzip_static_module --add-module=…/modules/upstream_hash
–sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf
–error-log-path=/var/log/nginx/error.log
–http-log-path=/var/log/nginx/access.log --prefix=/var/lib/nginx
–http-client-body-temp-path=/var/lib/nginx/client_body_temp
–http-proxy-temp-path=/var/lib/nginx/proxy_temp
–http-fastcgi-temp-path=/var/lib/nginx/fastcgi_temp
–pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx.lock


kgs

Ok,

I’ve found solution (or workaround - if this is bug in nginx).

I have to put:

set $x_metadata $upstream_http_x_metadata;

BEFORE

rewrite ^/files(.*)$ $1 break;

Could you please explain me why?

Thanks,


kgs

On 6 Jun 2011 15h33 WEST, [email protected] wrote:

rewrite ^/files(.*)$ $1 break;

Could you please explain me why?

Because you’re breaking the rewrite phase processing by issuing a
rewrite with a break. A break or return terminates the rewrite phase
so what you’re saying to Nginx is: rewrite and do no more rewrite
phase processing on this location.

The set directive is part of the rewrite module which means that it’s
processed during the rewrite phase. It’s not a bug, it’s the way it’s
supposed to be.

    location /files {
        internal;
        rewrite ^/files(.*)$ $1 break; ## Stops rewrite phase 

processing here

        proxy_pass http://filestore;
        proxy_hide_header Content-Type;
        proxy_hide_header Content-Disposition;

        set $x_metadata $upstream_http_x_metadata;

        add_header X-metadata $x_metadata;
    }

So the solution is to place all rewrite phase directives that you
want to be processed before the rewrite with the break flag.

    location /files {
        internal;

        set $x_metadata $upstream_http_x_metadata;
        add_header X-metadata $x_metadata;

        rewrite ^/files(.*)$ $1 break;

        proxy_pass http://filestore;
        proxy_hide_header Content-Type;
        proxy_hide_header Content-Disposition;
    }

— appa

On Mon, Jun 06, 2011 at 03:15:05PM +0200, Kamil G. wrote:

Hi guys,

I have small problem with file download using X-Accel-Redirect. I am
using Nginx as proxy before some REST API application, some of the
requests are for big files which are located at other host - so I am
using X-Accel-Redirect to download them. But I have also some metadata
connected with file which I want to return in X-metadata header from
REST API.

So, here is my config:

    location /files {
        internal;
        rewrite ^/files(.*)$ $1 break;

        proxy_pass http://filestore;

location /files/ {
internal;
proxy_pass http://filestore/;


Igor S.

It was a error in the nginx config all is well now!

Posted at Nginx Forum: