Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I recently put nginx in front of apache to act as a reverse proxy.

Up until now Apache handled directly the requests and file uploads

Now, I need to configure nginx so that it sends file upload requests to apache.

I have several endpoints for uploading files and I am trying to see if there is a quick way to define one configuration options in nginx for all my endpoints

Right now the below refers just to one file upload entry

location /banner_upload {
    proxy_pass  http://backend:8080/banner/save;
   }




 location /banner/save {
            # Pass altered request body to this location
            upload_pass /banner_upload;

        # Store files to this directory
        # The directory is hashed, subdirectories 0 1 2 3 4 5 6 7 8 9 should exist
        upload_store /tmp 1;

        # Allow uploaded files to be read only by user
        #upload_store_access user:r;

        # Set specified fields in request body
        upload_set_form_field "${upload_field_name}_name" $upload_file_name;
        upload_set_form_field "${upload_field_name}_content_type" $upload_content_type;
        upload_set_form_field "${upload_field_name}_path" $upload_tmp_path;

        # Inform backend about hash and size of a file
        upload_aggregate_form_field "${upload_field_name}_md5" $upload_file_md5;
        upload_aggregate_form_field "${upload_field_name}_size" $upload_file_size;

        upload_pass_form_field "(.*)";
    }

But the above entry is just for 1 endpoint

I have like 7 different ones

Do I need to make a new entry foreach of my endpoints or is there a way I can group them

Thanks

share|improve this question

2 Answers 2

Haven't tested this mind, but you should be able to set a named location and rewrite requests to that. You still need one line for each request though

#Add one of these for each location
rewrite ^/upload/location1$ @upload last;
rewrite ^/upload/location2$ @upload last;
rewrite ^/upload/location3$ @upload last;
rewrite ^/upload/location4$ @upload last;

location = @upload {
        # Pass altered request body to this location
        upload_pass /banner_upload;

        # Store files to this directory
        # The directory is hashed, subdirectories 0 1 2 3 4 5 6 7 8 9 should exist
        upload_store /tmp 1;

        # Allow uploaded files to be read only by user
        #upload_store_access user:r;

        # Set specified fields in request body
        upload_set_form_field "${upload_field_name}_name" $upload_file_name;
        upload_set_form_field "${upload_field_name}_content_type" $upload_content_type;
        upload_set_form_field "${upload_field_name}_path" $upload_tmp_path;

        # Inform backend about hash and size of a file
        upload_aggregate_form_field "${upload_field_name}_md5" $upload_file_md5;
        upload_aggregate_form_field "${upload_field_name}_size" $upload_file_size;

        upload_pass_form_field "(.*)";
}

That's the rough idea, I might have got some of the syntax wrong so consult the wiki if you get issues.

Edit

Depending on the rest of your config, you might have better luck rewriting to a directory

#Add one of these for each location
rewrite ^/upload/location1$ /upload/ last;
rewrite ^/upload/location2$ /upload/ last;
rewrite ^/upload/location3$ /upload/ last;
rewrite ^/upload/location4$ /upload/ last;

location /upload/ {
        # Pass altered request body to this location
        upload_pass /banner_upload;

        # Store files to this directory
        # The directory is hashed, subdirectories 0 1 2 3 4 5 6 7 8 9 should exist
        upload_store /tmp 1;

        # Allow uploaded files to be read only by user
        #upload_store_access user:r;

        # Set specified fields in request body
        upload_set_form_field "${upload_field_name}_name" $upload_file_name;
        upload_set_form_field "${upload_field_name}_content_type" $upload_content_type;
        upload_set_form_field "${upload_field_name}_path" $upload_tmp_path;

        # Inform backend about hash and size of a file
        upload_aggregate_form_field "${upload_field_name}_md5" $upload_file_md5;
        upload_aggregate_form_field "${upload_field_name}_size" $upload_file_size;

        upload_pass_form_field "(.*)";
}
share|improve this answer

Yes, there is a way to group all your endpoints into an "alias":

  upstream backend_pool {
      server 10.0.0.1:8080;
      server 10.0.0.2:8080;
      server 10.0.0.3:8080;
  }

  location /banner_upload {
      proxy_pass  http://backend_pool/banner/save;
  }

More about that here:

http://wiki.nginx.org/LoadBalanceExample

That's it, it should work fine!

share|improve this answer
    
Hi danakim. I was not clear I think, regarding what I mean by endpoints. I have just one server. The endpoints I mention, all point to the same server. I do not have many backend servers. What I want ideally, is to group these endpoints. Hope it's more clear now –  Thomas Jun 20 '11 at 5:51
    
Hi Thomas. Sorry, I am still not sure what you mean by endpoint. So you have just one server and an endpoint is a URI on that server? Or a different port on that server? So you have backend:8080/banner/save ; backend:8080/banner/save1 and so on? Or you have backend:8080 ; backend:8081 and so on? –  danakim Jun 20 '11 at 7:46
    
Hi. I have just one server. By endpoints I mean URIs (should have used URI instead of endpoint). So I have 1 backend server (backend:8080) and many different URIs, like /banner/save, /image/upload, /video/save etc. –  Thomas Jun 20 '11 at 7:58
    
Understood now. Then I am afraid you might have to define each URI separately. I wouldn't take my word for it and I would check out the documentation a bit more, but I am 90% sure you can not do it. –  danakim Jun 20 '11 at 9:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.