limit_rate based on proxy upstream response header

Maxim Dounin mdounin at mdounin.ru
Tue Jan 5 15:45:17 MSK 2010


Hello!

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

> 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.

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 Dounin



More information about the nginx mailing list