1

I'm trying to setup a virtual location in nginx in order to serve large static files from a directory. The main app is a symfony2 program.

What I would like to do is having an url where you could specify a filepath within a GET parameter, that way nginx could serve the file directly to the client.

In my example the location would be /getfile

Here's my config so far

server
{
    listen                                              80;
    server_name                                         website_url;
    set                                                 $path_web /myapp/webdir;

    client_max_body_size                                200m;   

location /getfile {             
    root /path_to_dir;
        if ($args ~ ^oid=(.*+)) {
          set $key1 $1;
          rewrite ^.*$  $key1;
        }   
    }

location ~ \.php($|/)
{
      set  $script     $uri;
      set  $path_info  "";

      if ($uri ~ "^(.+\.php)(.*)") {
          set  $script     $1;
          set  $path_info  $2;
      }


      fastcgi_pass   unix:/tmp/php-fpm.sock;
      fastcgi_index  index.php;
      fastcgi_param  SCRIPT_FILENAME  $path_web$script;
      fastcgi_param  SCRIPT_NAME      $script;
      fastcgi_param  PATH_INFO        $path_info;

      include   /etc/nginx/conf.d/fastcgi.conf;
}


    root                                                    $path_web;
    index                                                   app.php app_dev.php;

    location / 
    {   
    set $idx_file app.php;

    if ($args ~ debug=true) {
        set $idx_file app_dev.php;
        }

        if ( !-f $request_filename) {
    rewrite ^(.*)$ /$idx_file last; 
        }
    }

    location ~ /\.ht
    {
    deny                 all;
    }



    # logs path
    access_log                                          /path_to_logdir/access.log main;
    error_log                                           /path_to_logdir/error.log;
}
1
  • Are these files not under your web root? If so why not put them there or symlink the directory? Commented Jun 3, 2013 at 9:55

2 Answers 2

1

I'm adding my answer as there is no definite one. I would rather use try_files, so it would look something like that.

location /my/path {
  # try to serve file directly, fallback to rewrite
  try_files $uri $uri @rewrite;

}
location @rewrite {
  rewrite ^/(.*)$ /app.php/$1 last;
}

The server will then first look for the image at the given path and if not found fallback to app.php. It is much better than ifs and elses as it should never fallback to app.php.

Sign up to request clarification or add additional context in comments.

Comments

0

Though I'm not sure if this will work or not, but you've used an if and a set and a rewrite, too many un-needed things, I would try this

location /getfile?oid=(.*) {             
      rewrite ^ /path/$1;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.