It is necessary to implement a content access network (cdn). There is a primary server (250.250.250.250) where the front-end + backend is implemented, which is linked to the mydomain.com domain. There are 3 proxy servers (111.111.111.111.222.222.222.222 and 333.333.333.333) that cache the entire front-end, and dynamic requests go to main sevrer. That is, for example, a call to the server 111.111.111.111 static context is loaded from it, and dynamic requests go to 250.250.250.250. It is necessary to make so that when accessing the mydomain.com domain to which the server 250.250.250.250 is attached there is a request for 11.111.111.111 or 222.222.222.222 or 333.333.333.333 depending on the geographical proximity. What utilities can this be implemented with and how? All servers on nginx Config primary server 250.250.250.250

server { listen 80 default_server; listen [::]:80 default_server; root /var/www/html/main/prod/public; index index.php index.html index.htm index.nginx-debian.html; server_name _; location / { try_files $uri $uri/ /index.php$is_args$args; } location ~ \.php$ { try_files $uri =404; fastcgi_pass unix:/var/run/php/php7.0-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location ~ /\.ht { deny all; } 

The proxy code of the cached server 111.111.111.111

 proxy_cache_path /var/www/cache levels= keys_zone=mycache:150m; server { #устанавливаем дефолтовый флаг, не кэшировать set $cached 0; listen 111.111.111.111:80; #нужно чтобы отдавать красивую 500Ашипку с себя error_page 502 503 504 509 /500.html; #если где-то что-то забыли, то будет работать схема прозрачного проксирования error_page 404 = @nocached; expires epoch; root /var/www/html; location = /500.html { } #динамику будем брать с frontend и если отсутствует, то скачивать location ~* ^.+\.(jpg|jpeg|gif|gz|zip|flv|rar|wmv|avi|css|swf|png|htc|ico|mpeg|mpg|txt|mp3|mov|js)$ { expires 1d; error_page 404 = @fetch; } #кэшируем статику на себя location @fetch { proxy_pass http://250.250.250.250:80; proxy_store on; proxy_temp_path /var/www/_fetch; proxy_set_header Host mydomain; proxy_set_header If-Modified-Since ""; } #для зарегистрированных проксируем прозрачно location @nocached { proxy_pass http://250.250.250.250:80; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } #гостям проксируем и кэшируем location @cached { proxy_pass http://250.250.250.250; 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_cache mycache; proxy_cache_valid 200 301 302 304 5m; proxy_cache_key "$request_method|$http_if_modified_since|$http_if_none_match|$host|$request_uri"; proxy_hide_header "Set-Cookie"; proxy_ignore_headers "Cache-Control" "Expires"; } #иначе морда домена не будет работать location = / { return 404; } location / { #если нет нашей куки if ($http_cookie !~ "userid" ) { set $cached 1; } if ($request_method = POST) { set $cached 0; } if ($request_method != GET) { set $cached 0; } if ($cached = 1) { error_page 404 405 502 504 = @cached; break; } if ($cached = 0) { error_page 404 405 502 504 = @nocached; break; } } } 
  • Simply go to the CDN provider. Ngenix, Cloud Flare, etc. - Alexey Ten
  • I know, but you need to build your cdn. In general, as I understand it, you need to raise the dns-server on the main server, which will receive requests and give the necessary caching server. But the question is how and with the help of what to make the balancer - Dmitry Dmitry

0