On the page of the cost of EC2 components on Amazon there is one line that at first you can not pay much attention, but which can lead to serious financial costs - Data Transfer, traffic. If the lease of instances and EBS volumes can be planned and monitored, then traffic is quite difficult to predict, and ignoring it will not be given a monthly bill)
For example: the average news site, 30 thousand visits per day, it will pull a small small or even micro instance. Take the full page size of 2 megabytes, then the monthly traffic will be (without taking into account the cached content) - 30000 * 0.002 * 30 = 1800
GB or
$ 216 . It turns out the cost of Data Transfer is even more than the lease of the instance itself! At S3, the situation with the price of traffic is exactly the same.
Most of this traffic is static content, which is not necessarily distributed directly from Amazon. We need a cheap and fast channel - for these purposes, the simplest dedicated servers on Hetzner are excellent.
')
Although static is static, it is constantly changing - files are uploaded, updated, deleted, so you have to set up automatic synchronization between the Amazon instance and the static server.
Take lsyncd for this purpose - it monitors files in specified directories using inotify and executes a piece of Lua script if something changes (a more complete description of lsyncd in a good post:
habrahabr.ru/post/132098 )
On Amazon server (examples for CentOS):
Install lsyncd, rsync:
yum install lsyncd mkdir -p /var/log/lsyncd
We create a config in /etc/lsyncd.conf with the meaning “Synchronize all changes that occur to files, except php files, no more than once every 3 seconds, use rsync via ssh.”
settings = { logfile = "/var/log/lsyncd/lsyncd.log", statusFile = "/var/log/lsyncd/lsyncd.status", } sync { default.rsyncssh, source = "/home/user/example.com", host = "static.example.com", targetdir = "/home/user/static.example.com", rsyncOps = {"-av", "--temp-dir=/tmp", "--delete", "--exclude=*php"}, exclude = {"somestaticfile.json"}, delay = 3, }
We generate (if they are not already) the keys via ssh-keygen, write the generated id_rsa.pub to the statics server in authorized_keys.
Run lsyncd:
lsync /etc/lsyncd.conf
Synchronization messages should immediately go in the logs, and files that you can already distribute will appear on the statics server, of course, using nginx. The only time is that the client does not receive an error message when the file has not yet been synchronized, and the client is already requesting it, then you need to make proxying of such requests back to the Amazon instance. Such a situation may be when, for example, when downloading an image, you need to immediately show it, or if for some reason synchronization has fallen off. The nginx config is obtained like this:
server { listen 80; server_name static.example.com location / { root /home/user/static.example.com; add_header Access-Control-Allow-Origin *;
We end up with the combined reliability and flexibility of AWS and the cheapness of the Hetzner.
PSFor sites with a large number of files, it may be necessary to increase the limits on inotify in sysctl:
fs.inotify.max_user_instances = 1024 fs.inotify.max_user_watches = 65000 fs.inotify.max_queued_events = 16384