Limit_rate based on proxy upstream response header

We’re using nginx as a proxy, and I want to limit the download rate of
certain proxied pages/files depending on a header potentially set by the
backend server. I’m trying something like:

location ^~ / {
  set $slowrate $upstream_http_x_rate_limit;
  if ($slowrate) {
    set $limit_rate $slowrate;
  }

  rewrite (.*) /http/$host$1 break;

  proxy_pass http://backend/;
  proxy_hide_header X-Rate-Limit;


}

And then setting the X-Rate-Limit header on the backend response, but
that doesn’t seem to work.

So I tried even simpler, allowing just a simple “make this slow” header
setting.

location ^~ / {
  set $slowrate $upstream_http_x_rate_limit;
  if ($slowrate) {
    limit_rate 100;
  }

  rewrite (.*) /http/$host$1 break;

  proxy_pass http://backend/;
  proxy_hide_header X-Rate-Limit;


}

Yes, I really wanted to try just 100 bytes/second. But that didn’t work
either.

Is there a way of doing this?

Rob
Rob M.
[email protected]

Hello!

On Tue, Jan 05, 2010 at 01:54:19PM +1100, Robert Mueller wrote:

  rewrite (.*) /http/$host$1 break;

setting.
proxy_hide_header X-Rate-Limit;

}

Yes, I really wanted to try just 100 bytes/second. But that didn’t work
either.

It’s expected, as rewrite directives (including “set” and “if”)
are exectured during rewrite phase, i.e. before request to backend
happens.

Is there a way of doing this?

Try returning X-Accel-Limit-Rate header instead. Not sure it’s
documented somewhere except in source code, but it’s at least
mentioned here:

http://wiki.nginx.org/NginxXSendfile

Maxim D.

It’s expected, as rewrite directives (including “set” and “if”)
are exectured during rewrite phase, i.e. before request to backend
happens.

I thought it might be something like that.

Try returning X-Accel-Limit-Rate header instead. Not sure it’s
documented somewhere except in source code, but it’s at least
mentioned here:

http://wiki.nginx.org/NginxXSendfile

Excellent, that worked thanks.

I’ve updated the NginxHttpCoreModule in the limit_rate section to make a
note of that.

http://wiki.nginx.org/NginxHttpCoreModule

Rob