NGINX Architecture – An Insight (Part 2)
Sandeep Khuperkar I CTO and Director, Ashnik
Overview on NGINX Caching
NGINX as a web server handles static content very efficiently , in addition Nginx can act as a very capable cache server. Nginx can cache content received from other servers.
Nginx can be used as both as cache server and load balancer by acting as a gateway for other web or application servers.
Nginx as a cache sever receives the initial HTTP requests it then handles the request if it has a cached copy of requested resource orelse pass on the request to origin server. Response from the origin server are read by the cache server to decide if the response needs to be cached or pass through.
In Nginx cache keys and cache metadata are stored in shared memory segments, these memory segments can be accessed by cache loader, cache manager and workers. Each cached response is placed in different file in the file system.
When Nginx reads the response from an upstream server the content initially is written to a temporary file which is outside the cache directory structure and as the request is processed it renames the temporary file and moves in cache directory.
NGINX Caching Processes
Cache loader and Cache manager are two Nginx processes involved in caching. Cache manager checks the state of cache file storage periodically. It removes the least recently used data when the size of file storage exceeds the max_size parameter.
When Nginx starts cache loader is activated. It loads the meta information about the previously cached data into the shared memory zone. Cache loader works in iterations with parameters as configured for proxy_cache_path .
Nginx has a scalable configuration system which is essential for web server. Normally the challenge of scaling is faced when maintaining lots of virtual servers , directories, locations and datasets. Keeping this in mind Nginx configuration is designed to simplify day-to-day operations and to provide ease of expansion of web server configuration.
Nginx configuration resides in /usr/local/etc/nginx or /etc/nginx. The main configuration file is usually called nginx.conf. In general, nginx settings also provide support for several original mechanisms that can be very useful as part of a lean web server configuration. It makes sense to briefly mention variables and the try_files directive, which are somewhat unique to nginx.
Variables in nginx were developed to provide an additional even-more-powerful mechanism to control run-time configuration of a web server. Nginx configuration was designed to simplify day-to-day operations and to provide an easy means for further expansion of web server configuration.