When your website is behind a load balancer how can you ensure that file uploads (e.g., custom option) goes to the correct server? I think having multiple servers is pretty common but I can't seem to find an answer to this.

Right now when users upload an image for a custom order, the file is created under pub/media/custom_options/quote/. Because I have two servers behind a load balancer, which server the file is created on is random. In my case, I have two servers, let's call them master and slave. If the file is uploaded to master, it propagates to slave using lsync. In the case of the opposite, the file is deleted by lsync after a short period of time.

My solution right now is a shell script running on a cron job on slave that looks for new files coming into the custom_options directory, checks that it does not exist on master, and if not uses rsync to copy the file. It seems sloppy to do it this way but so far this has not failed me.

Aside from how I'm already handling this, a few ideas come to mind on how I can do this more efficiently:

  • Make an apache rule that forces multipart/form-data requests only go to master. My hosting provider advised against this saying it would be extremely inefficient.
  • Alter my add to cart form's action to point to a subdomain that goes directly to master. I figured out how to change the action, but this doesn't work because Magento 2 automatically redirects the subdomain to the store's URL and breaks the add to cart button. This was the solution recommended to me.
  • Ignore the pub/media/custom_options directory in lsync and find another way to sync these directories. Perhaps this in combination with what I'm doing would be best.
  • Set up amazon s3 or equivalent to host files, then use a module such as this to store the files. I'm not possitive if this would solve my problem but I have see some hints that this is somewhat standard. I'd rather not pay for more servers and the files are only accessed once or twice after an order, I think it would be overkill.

What is the ideal way to handle this situation?

Magento Open Source 2.1.9

1 Answer 1


To anyone who is in a similar situation, I finally did get a solution.

The answer was simply to use nfs to share a drive on a third server (but this could be done with only two servers as well). The nfs share is mapped to my pub/media and pub/static folder. As a bonus I've set up a second domain and disabled cookies so that my static content loads without cookies.

I can provide more info if anyone finds this helpful (leave a comment). I found digital ocean to be a valuable resource for information to set this up: https://www.digitalocean.com/community/tutorials/how-to-set-up-an-nfs-mount-on-ubuntu-14-04

I was worried about reliability originally but I've been running this set up for a month with no problems so far.

  • Please comment if you are downvoting, I'd like to know how I can improve the answer.
    – jamil
    Commented Jan 25, 2019 at 19:22

Your Answer

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

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