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
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
actionto 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_optionsdirectory 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