limit_rate based on proxy upstream response header

Robert Mueller robm at fastmail.fm
Tue Jan 5 05:54:19 MSK 2010


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 Mueller
robm at fastmail.fm




More information about the nginx mailing list