2

on a website I host I have a nginx set up as http server mainly for static content. Some content that gets serverd actually are software downloads (setup.exe stuff). What i want to do is somehow get nginx to serve the newest download package under a static URL. So that http://www.example.com/download/myapp leads to a download of myapp-setup-1.0.msi as long as myapp-setup-1.0.msi is the newest file in the respective content directory.

I tried to implemnt this by use of symlinks but then the problem arises that one has to set an appropriate header (Content-Disposition) containing the filename the browser displays and I do not know how to get the target filename of a symlink from inside the nginx configuration. I think this may not be possible.

Here is the config so far:

server {

    listen   80;

    server_name www.example.com example.com;

    access_log  /var/log/nginx/download.access.log;

    location /download/ {
      alias /var/www/download/;
      default_type application/octet-stream;
    }

    location /download/myapp/ {
      alias /var/www/download/myapp/;
      index latest.msi;
      default_type application/octet-stream;
      if ($request_filename ~* ^.*/([^/]*?)$) {
        set $exits true;
        set $msi $1;
      }
      if ($msi = latest.msi) {
        set $msi "myapp-setup-1.0.msi";
      }
      if ($exits) {
        add_header Content-Disposition: "attachment; filename=$msi";
      }
    }

}

Note that latest.msi is a symlink that points to myapp-setup-1.0.msi.

So any ideas on how to implement a download of the latest file by a static url with nginx? Or does it make more sense to use some kind of cgi script for this situation?

2 Answers 2

1

You could test a mix between the statement try_files :

http://wiki.nginx.org/HttpCoreModule#try_files

and if_modified_since statement :

http://wiki.nginx.org/HttpCoreModule#if_modified_since

Finally the Embedded Perl module could return the name of the latest file in a directory with more options.

http://wiki.nginx.org/EmbeddedPerlModule

1
  • If I understand right the if_modified_since directive only changes how nginx handles certain request header parameters. I have not found a sufficient solution either with rewrites or the try_files statement. So i actually did go with the embedd perl module. Aug 29, 2011 at 9:48
0

The following was an edit to the question by the original poster:

I went with the embedd perl module like DrGkill suggested and the following short perl snippet:

perl_set $link_target '
  sub {
        my $r = shift;
        my $filename = $r->filename;
        if (-l $filename) {
          return readlink $filename;
        } else {
          return "";
        }
   }
  ';

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .