Forward single request to upstream server via proxy_store !!

shahzaib shahzaib shahzaib.cb at gmail.com
Thu Sep 25 14:13:44 UTC 2014


@RR, i've prepared the local environment with the following structure :-

client --> nginx (edge) --> varnish --> backend (Origin)

When i tested this method i.e :-

3 clients requested for test.mp4 (file size is 4mb) --> nginx --> file not
existed (proxy_store) --> varnish --> backend (fetch the file from origin).

When nginx proxied these three requests subsequently towards the varnish,,
despite of filling 4mb of tmp dir it was filled with 12MB which means nginx
is proxying all three requests towards the varnish server and creating tmp
files as long as the file is not downloaded. (The method was failed)

Although On putting varnish in front of nginx solved this issue.

3 clients requested for test.mp4(file size is 4mb) --> varnish(proxying all
requests for mp4,jpg) --> nginx.(fetch the file from origin).

This time tmp dir was filled with the size of 4Mb which means varnish
combined those 3 subsequent requests into 1.

--------------------------------------------------------------------------------------------------------------------------

Now varnish also has a flaw to send subsequent requests for same file
towards the nginx i.e

1st user requested for file http://edge.files.com/videos/test.mp4. During
the downloading of first requested file, the second user also requested the
same file but with random seeking
http://edge.files.com/videos/test.mp4?start=33 . Now as the request uri is
changed, there are two different requests for the same file in varnish and
again nginx tmp directory was filled with 8MB instead of 4 which means
nginx downloaded the full file twice. So Random seeking will only work once
the file is cached locally, otherwise nginx will keep on creating tmp files
against random seekings.

I have two questions now :-

1. If there's way to prevent duplicate downloads for random seekings while
the file not downloaded yet ? Note :- We cannot disable mp4 module.
2. Should nginx in front of varnish never work as expected or i am doing
something wrong ?

Following are existing varnish in front of nginx configs. Please let me
know if something need to be fixed :-

varnish config :-

backend origin002 {
    .host = "127.0.0.1";
    .port = "8080";
}

backend origin003 {
    .host = "127.0.0.1";
    .port = "8080";
}


sub vcl_recv {

        if   ( req.http.host == "origin002.files.com" ){
                set req.backend_hint = origin002;
        } elsif ( req.http.host == "origin003.files.com" ){
                set req.backend_hint = origin003;
        } elsif ( req.http.host == "origin004.files.com" ){
                set req.backend_hint = origin004;
        }

}

sub vcl_backend_response {


if (bereq.url ~ "^[^?]*\.(mp4|jpeg|jpg)(\?.*)?$"){
         set beresp.do_stream = true;
        return (deliver);
}
set beresp.grace = 1m;

    return (deliver);


}

sub vcl_deliver {

}

-----------------------------------------------------------------------------------------
Nginx config :-


server {

        listen       127.0.0.1:8080;
        server_name  origin002.files.com;
        root /var/www/html/tunefiles;
        location ~ \.(mp4|jpeg|jpg)$ {
               root   /var/www/html/tunefiles;
                mp4;
        error_page 404 = @fetch;

            }


        location ~ \.(php)$ {
                proxy_pass http://origin002.files.com:80;
        }



        location @fetch {
        internal;
        proxy_max_temp_file_size 0;
        proxy_pass http://content.files.com:80$uri;
        proxy_store        on;
            proxy_store_access user:rw group:rw all:r;
        root /var/www/html/tunefiles;
}

}


 I can also send the configs which were configured for nginx in front of
varnish (which didn't resolved my issue).

BTW, i am using malloc storage instead of file in varnish.

Thanks !!

On Wed, Sep 24, 2014 at 6:55 PM, shahzaib shahzaib <shahzaib.cb at gmail.com>
wrote:

> @RR, That's great. Sure it will help me. I am starting to work with it on
> local environment and will get back to you once the progress started :)
>
> Thanks a lot for writing sample config for me !!
>
> On Wed, Sep 24, 2014 at 6:32 PM, Reinis Rozitis <r at roze.lv> wrote:
>
>> @RR. could you guide me a bit on it or point me to some guide to start
>>> with. I have worked with varnish regarding php caching so i have the basic
>>> knowledge of varnish but i am just not getting on how to make it work with
>>> proxy_store. :(
>>>
>>
>> Depending on your needs (for example SSL) you can put varnish in
>> different places in the setup:
>>
>>
>> If you use SSL (which varnish itself doesn't support) you can use your
>> proxy_store server as an SSL offloader:
>>
>> 1. [client] <- -> [nginx proxy_store server] <- -> [varnish] <- ->
>> [content_server]
>>
>> .. in this case when multiple requests land onto nginx proxy_store in
>> case the file locally doesnt exist those are forwarded to varnish and
>> combined into a single request to the content server.
>>
>> A simplistic/generic nginx config:
>>
>> location / {
>>    error_page      404 = @store;
>> }
>>
>> location @store {
>>        internal;
>>        proxy_pass           http://imgstore;;
>>        proxy_store          on;
>> }
>>
>>
>> varnish config:
>>
>> backend default {
>>    .host = "content_server.ip";
>> }
>> sub vcl_recv {
>>    set req.backend = default;
>> }
>>
>>
>> Obviously add whatever else you need (like forwarded-for headers to pass
>> the real client ip, cache expire times etc).
>>
>>
>>
>> 2. In case you don't use SSL:
>>
>> [client]  <- -> [varnish] <- ->  [content_server]
>> (optionally you put nginx or some other software like stud or pound on
>> top of varnish as SSL offloader (personally I use Shrpx from Spdylay (
>> https://github.com/tatsuhiro-t/spdylay ))
>>
>> Then generic varnish config would look bassically the same:
>>
>> backend default {
>>    .host = "content_server.ip";
>> }
>> sub vcl_recv {
>>    set req.backend = default;
>> }
>>
>> sub vcl_backend_response {
>>    set beresp.do_stream = true;
>> }
>>
>>
>>
>> Hope that helps.
>>
>>
>> rr
>> _______________________________________________
>> nginx mailing list
>> nginx at nginx.org
>> http://mailman.nginx.org/mailman/listinfo/nginx
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.nginx.org/pipermail/nginx/attachments/20140925/a1c66d8e/attachment.html>


More information about the nginx mailing list