Limit_rate for different resolutions!

Hello,

  Is there a way that i could use different limit_rate in nginx for

different files ? I.e

  1. limit_rate 500k for 720p video files.
  2. limit_rate 180k for 320p video files.

Best Regards.
Shahzaib

On Sat, Jul 20, 2013 at 02:29:49PM +0500, shahzaib shahzaib wrote:

Hi there,

  Is there a way that i could use different limit_rate in nginx for

different files ? I.e

  1. limit_rate 500k for 720p video files.
  2. limit_rate 180k for 320p video files.

Yes.

http://nginx.org/r/limit_rate

One possibility: have your 720p video files and your 320p video files
handled in different location{} blocks.

f

Francis D. [email protected]

Hello Francis,

what if both(720p,360p) are in same directory i.e /var/www/html/videos ?

On Sat, Jul 20, 2013 at 02:47:14PM +0500, shahzaib shahzaib wrote:

Hi there,

what if both(720p,360p) are in same directory i.e /var/www/html/videos ?

Where’s the problem?

Module ngx_http_core_module for limit_rate.

http://nginx.org/r/location, if you choose to handle the different types
of files in different location{} blocks.

f

Francis D. [email protected]

On Sun, Jul 21, 2013 at 01:47:57PM +0500, shahzaib shahzaib wrote:

Hi there,

 Following two are the file types whom i want to assign different

rate_limits :

http://domain.com/files/videos/2013/07/20/137430161313bb6-360.mp4 —>
limit_rate 180k;
http://domain.com/files/videos/2013/07/20/137430161313bb6-720.mp4
limit_rate 500;

What rate limit do you want to apply to http://domain.com/file.flv?

What rate limit actually applies to it? And did you measure it? (“curl”
is usually a good tool to see what is really happening.)

    location / {

location ~ .(flv|jpg|jpeg)$ {
location ~ .(mp4)$ {

http://nginx.org/r/location

What do your “location” lines up there mean?

For each request you make, what one location is used?

What happens if you add a

location ~ -720.mp4$ {}

block at the end? And what happens if you instead add it at the start?

f

Francis D. [email protected]

Hello,

  I added the 720p in the location{} and checked it by downloading 

the
single file using wget and got the 500K speed :).

location ~ -720.(mp4)$ {
mp4;
expires 7d;
limit_rate 500k;
root /var/www/html/tunefiles;
}
That worked :). Thanks a lot @francis.

Hello,

 Following two are the file types whom i want to assign different

rate_limits :

http://domain.com/files/videos/2013/07/20/137430161313bb6-360.mp4 —>
limit_rate 180k;
http://domain.com/files/videos/2013/07/20/137430161313bb6-720.mp4
limit_rate 500;

Following is the virtualhost config :

server {
listen 80;
server_name domain.com;

    client_max_body_size 800m;
    limit_rate 180k;
#    access_log  /websites/theos.in/logs/access.log  main;

    location / {
        root   /var/www/html/videos;
        index index.html index.htm index.php;

}

location ~ .(flv|jpg|jpeg)$ {
flv;
root /var/www/html/videos;
expires 7d;
valid_referers none blocked domain.com;
if ($invalid_referer) {
return 403;
}
}
location ~ .(mp4)$ {
mp4;
root /var/www/html/videos;
expires 7d;
}

Can you please guide me a bit that how to configure the specific
limit_rate
in different location{} blocks? I am a bit confused on it :frowning:

Thanks

What rate limit actually applies to it? And did you measure it? (“curl”
is usually a good tool to see what is really happening.)

    location / {

location ~ .(flv|jpg|jpeg)$ {
location ~ .(mp4)$ {

Well the rate_limit was 180K before for all the files because i added it
into the server{} block and these location blocks were actually means
the
rate_limit 180k will apply to any flv,mp4,jpeg file and after adding
-720
before the location ~.(mp4)$, the only 720p files will be served on
limit_rate 500k and the rest would remain the same which is 180k in my
case.

I didn’t used curl instead wget.

hello,

Changing the “mp4” location to be “~ -720.mp4$” means that those ones
will have the higher rate; but if you don’t have a second location{}
for the other mp4s, they won’t see the “mp4” directive, and so may not
be served correctly.

Yes, i’ve created different block locations for mp4 files and every mp4
file is working fine, you can check the config below and let me know if
there’s anything wrong you see ?

location ~ -720.(mp4)$ {
mp4;
expires 7d;
limit_rate 500k;
root /var/www/html/videos;
valid_referers none blocked domain.com;
if ($invalid_referer) {
return 403;
}
}
location ~ -480.(mp4)$ {
mp4;
expires 7d;
limit_rate 250k;
root /var/www/html/videos;
valid_referers none blocked domain.com;
if ($invalid_referer) {
return 403;
}
}
location ~ .(mp4)$ {
mp4;
expires 7d;
root /var/www/html/videos;
valid_referers none blocked domain.com;
if ($invalid_referer) {
return 403;
}
}

On Sun, Jul 21, 2013 at 02:29:26PM +0500, shahzaib shahzaib wrote:

Hi there,

Well the rate_limit was 180K before for all the files because i added it
into the server{} block and these location blocks were actually means the
rate_limit 180k will apply to any flv,mp4,jpeg file and after adding -720
before the location ~.(mp4)$, the only 720p files will be served on
limit_rate 500k and the rest would remain the same which is 180k in my case.

Good stuff – each directive has a meaning, and if you wanted most
requests to have the 180 rate limit, then you put it in the right place.

Changing the “mp4” location to be “~ -720.mp4$” means that those ones
will have the higher rate; but if you don’t have a second location{}
for the other mp4s, they won’t see the “mp4” directive, and so may not
be served correctly.

I didn’t used curl instead wget.

That’s fine – wget does a similar task to curl; and it’s always good
to be able to demonstrate that the change you made fixed what was broken
(and didn’t break what was already right).

Cheers,

f

Francis D. [email protected]

On Mon, Jul 22, 2013 at 09:27:59AM +0500, shahzaib shahzaib wrote:

Hi there,

location ~ -480.(mp4)$ {
mp4;
expires 7d;
root /var/www/html/videos;
valid_referers none blocked domain.com;
if ($invalid_referer) {
return 403;
}
}

It looks reasonable from here.

The various repeated directives may fit better one level higher, but
that depends on the rest of the configuration.

The parentheses (“()”) around mp4 in the locations look unnecessary.

But if it does what you want, it’s good.

f

Francis D. [email protected]

Going with the same configuration as far as they are working for me.

Thanks again Francis :slight_smile:

Best Regards.
Shahzaib