📜 ⬆️ ⬇️

Web server abroad + statics in Russia = speeding up page loading speed

The most common question when choosing a foreign host (for example, Hetzner): “Will the site be slow to stop”? Despite the fact that ping to Hetzner from Russia is quite small, static (especially heavy or if there are a lot of it) from servers hosted in Russia is given faster.

There are several options for increasing the speed remaining in Hetzner:

1. Use of Russian CDN-services.
2. Proxy / caching statics via server / vps, located in Russia.
')
I will tell you how to technically implement static proxying / caching using nginx via a server hosted in Russia.


To solve the problem, it will be necessary to bring all the static to a separate subdomain, and if there are many statics - to several domains. For example, assets01.example.com, assets02.example.com, ..., assets04.example.com (using more than 4 domains can lead to a decrease in performance, because DNS-resolve also takes some time ). Thus, bypassing the limitations of simultaneous connections, the browser will load the page faster. It is important that the links to the statics on the page always remain the same, since in case of continuous mixing of assets01-asstets04 domains, the browser will reload it.

In the following example, the static will be transparently proxied from the server in Hetzner and cached on the Russian server, so the reload will occur only when changes are made on the server in Hetzner. I note that in case you need to change, for example, css, you will need to either change the file name or the number in the request. For example, you can use the addresses assetsXX.example.com/css/styles.css?2342324 , and when changing the styles.css file, change the serial to any other. This is also useful when using the nginx “expires max” parameter — the browser will not even check for updates, which also increases the speed of the page opening.

The nginx config on the Russian server will look like this (on the server in Hetzner you also need to register aliases):

#      30 ,    100  proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=static:32m inactive=30d max_size=100g; server { listen 80; server_name assets01.example.com assets02.example.com assets03.example.com assets04.example.com; # access_log     , ..             access_log off; error_log /var/log/nginx/example.com-err.log; #     ,         http://assetsXX.example.com/ -      location / { rewrite ^ http://example.com$uri permanent; } #  ,    location ~* \.(jpg|jpeg|gif|png|ico|css|midi|wav|bmp|js|swf|flv|avi|djvu|mp3)$ { #  IP   Hetzner proxy_pass http://111.111.111.111:80; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_buffer_size 16k; proxy_buffers 32 16k; #   expires  ,    serial ( , http://assetsXX.example.com/css/styles.css?2342324) if ($request_uri ~* "\?[0-9]+$") { expires max; break; } proxy_cache static; #      30  proxy_cache_valid 30d; proxy_ignore_headers "Cache-Control" "Expires"; #        ,    ,    assetsXX     (  /css/styles.css?123  /css/styles.css?321   ) proxy_cache_key "$uri$is_args$args"; proxy_cache_lock on; } } 

Source: https://habr.com/ru/post/160127/


All Articles