ubuntu - How can I force caching a video file on nginx

07
2014-07
  • look

    I have two servers configured pretty much exactly similarly. One is my production server, and the other is my staging server. The nginx config on both are copies of each other except for the name of the server (and whereever else I use the server name)

    Here is the config from my staging server.

    server {
      listen 80;
      server_name *.johnvarghese.info johnvarghese.info;
      return 301 https://$host$request_uri;
    }
    
    server {
      listen 443;
      ssl on;
      ssl_certificate /etc/johnvarghese.info.ssl/star.johnvarghese.info.bundle.crt;
      ssl_certificate_key /etc/johnvarghese.info.ssl/star.johnvarghese.info.key;
    
      server_name support.johnvarghese.info;
      return 301 "$scheme://johnvarghese.info/support${request_uri}";
    }
    
    server {
      listen 443;
      ssl on;
      ssl_certificate /etc/johnvarghese.info.ssl/star.johnvarghese.info.bundle.crt;
      ssl_certificate_key /etc/johnvarghese.info.ssl/star.johnvarghese.info.key;
    
      server_name *.johnvarghese.info johnvarghese.info;
    
      passenger_ruby /usr/local/rvm/gems/ruby-2.1.0/wrappers/ruby;
      passenger_enabled on;
      root /var/www/johnvarghese.info/current/public;
    
      location ~* \.(mp4|ogg|webm)$ {
        expires 10d;
        add_header Cache-Control "max-age=864000";
      }
    
    }
    

    The staging server is called johnvarghese.info. On the production server, I replaced all instances of johnvarghese.info with the servername.com.

    The lines I am interested is the last three lines in the config - I just want to cache the webm video file (which I repeat playing in a loop). It works perfectly on the staging server. Once the video is downloaded, I can turn off wireless networking on my laptop and the video keeps looping forever with nor further download required. The response code in the console is 206 every time starting from the second time. The server is not hit anymore.

    On the production server the server is hit everytime the video is looped. The response code is 304 (not modified). If I turn of wireless networking on my laptop, when the video completes it current play, it stops because it cannot download it again for the repetition.

    Why does the same configuration

      location ~* \.(mp4|ogg|webm)$ {
        expires 10d;
        add_header Cache-Control "max-age=864000";
      }
    

    work differently on these similarly configured servers? I want to make the production server work just like the staging server. What should I do?

    Thanks

  • Answers
  • harrymc

    You should rather look at the headers received by the client browser, as the caching is done at the client browser rather than at the server. Nginx can add its own headers which may conflict with yours, depending on your server setup and added Nginx modules.

    For further analysis, add a dump of the headers received by the client browser in both the cases, where it worked and where it didn't. This comparison might give us the answer to the mystery.

    See this post for more ideas as to the cause : Nginx add_header and cache control.
    The modules more_set_headers and more_clear_headers are suggested here in order to replace or clear the headers.


  • Related Question

    cache - Where can Firefox store cached flash (.flv) video files?
  • Ivan

    I am willing to save an online flash video (.flv) file for further offline watching. I couldn't download this particular video file through a direct link (grabbed with Video Download Helper extension) because of a very slow and unresponsive server, but I could watch it to the end with a webpage embedded player, and now it is for sure saved to cache (as I can rewind it and watch from any place without no more waiting).

    I went to

    /home/user/.mozilla/firefox/a0zzqdg4.default/Cache

    But the file I want is not there (it is to be about 200 MiBs, but the biggest file there is of 22 MiBs and is called "_CACHE_003_").

    Where might it be if not in the standard cache directory?


  • Related Answers
  • kagali-san

    It is there or in /tmp;

    Nowadays, Flash Player (starting from some 10.xx version) for linux does unlink on flv files;

    ls -la /proc/{PID OF PLAYER PLUGIN}/fd to get an idea;

    More info (check the script): http://habrahabr.ru/blogs/linux/104420/

  • Paul

    C:\Users\username\AppData\Local\Temp

    Also reachable through WIN+R, then typing %appdata% and then following Local -> Temp.

  • matt wilkie

    The OP was looking for a linux solution, but the question is valid for Windows too, so here's an answer for that. Look for fla*.tmp in

    C:\Users\username\AppData\Local\Temp
    C:\Users\username\AppData\Local\Temp\acro_rd_dir
    

    Also reachable through Win+R, then typing %temp%, or following Paul's example:%appdata% and then following to Local -> Temp.

    Chances are you won't be able to do anything with the files though because they'll be marked as locked or in use. There are a few programs that can work around this for you, just search for "windows copy locked or in-use file" and variations thereof. The ones worth their salt will probably use Volume Shadow Service (VSS). I like hobocopy and here's an example use:

    hobocopy.exe /verbosity=2 %temp%\acro_rd_dir D:\Playback\Incoming flaFF7E.tmp
    pushd D:\Playback\Incoming
    move /y flaFF7E.tmp SomeUsefulName.flv
    explorer /select,%cd%\SomeUsefulName.flv 
    

    The relevant part is hobocopy source_dir destination_dir file-to-copy, and the rest sugar. Replace explorer... with start %cd%\SomeUsefulName.flv to start playing the video right away instead of just selecting it.

    Recipe last tested with Firefox 18 on Window 7.

  • Ratanachai

    Run locate *.flv to see all your flv files on your system.

    However you might need to run sudo updatedb first (to update locate command index database)

  • Peter Mortensen

    I think it is better to use WinRAR and go to C:\Users\youruser\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.IE5 OR \Low\Content.IE5. Then look in each of the folders and look for the biggest file, probably ending in .flv or .avi.