📜 ⬆️ ⬇️

nginx, once again about caching

Sometimes the growth rate of a project is slightly higher than the speed of optimization of a web application or the acquisition of more powerful equipment under the backend.

The simplest scheme of "parallelization" of the load is the removal of the main load on several frontend. Previously, you had to suffer (or enjoy someone as you like) with webdavs, clustered filesystems and other tricks to provide up-to-date information, this was until nginx appeared, or rather, proxy_store and proxy_cache in it.


')
In general, the scheme of work is as follows:
Several frontend (cheap servers without raid and other adult intricacies) through round robin are given to clients.
frontend gives off statics, proxies and gives off cached dynamics for guests and performs transparent proxying for registered users.
TTX backend in this case does not matter much, the question of the correct configuration should be discussed individually. Additional caching of all that is possible on the backend is welcome.

The advantages of this approach are in the cost of this “clustering” due to the budget characteristics of the frontend. The refusal of any frontend is not critical, it is enough to raise its ip (if available) on another frontend or change the DNS configuration (which may result in unavailability for a number of users). In practice, the city portal where the video / photo storage on 4TB (occupied on the backend) on the frontend does not contain more than 300GB of what users actually request (you can clean it by atime, but considering that the frontend FS is mounted with noaime then by date of creation file). Static content on the backend is generated with a new name, modifying the file also creates a new file name so that the frontend works correctly.

http {
proxy_cache_path /var/www/cache levels= keys_zone=mycache:150m;
...
}

server {
# ,
set $cached 0;
listen server;
server_name server *.server;
# 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 1y;
error_page 404 = @fetch;
}

#
location @fetch {
proxy_pass htt://backend;
proxy_store on;
proxy_temp_path /var/www/_fetch;
proxy_set_header Host mydomain;
proxy_set_header If-Modified-Since "";
}

#
location @nocached {
proxy_pass htt://backend;
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 htt://backend;
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;
}

}

}


The whole thing is for the benefit of my Novosibirsk site

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


All Articles