Caching in NGINX

By April 24, 2013 HowTo 6 Comments
Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInEmail this to someone

Most times, your site needs some kind of caching system in place, even with a moderate increase in traffic levels.

Real Caching Needs to Be:

  • High performance
  • Light-weight
  • Flexible enough to work for any type or amount of traffic
  • A completely separate layer that lives above your web apps
  • Structured to avoid invoking the heavy application frameworks it sits in front of

All of the above benefits are available with Jelastic using NGINX.

NGINX CachingNGINX can store data in front of your web servers. For example, the files a user automatically requests by looking at a web-page can be stored in your NGINX cache directory. When a user returns to a page they have recently looked at, the browser can get those files from the NGINX cache directory rather than the original server, saving time and traffic.

Caching improves performance while accessing resources in two ways:

  • Reduces the access time to the resource by copying it closer to the user
  • Increases the resource building speed by reducing the number of accesses. For example, instead of building the homepage of your blog at each request, you can store it in a cache.

Use the next instructions to set up a caсhe on the NGINX-balancer and Nginx PHP server in Jelastic.

Using NGINX-balancer

1. Log in to Jelastic Manager.

2. While in Jelastic dashboard, click the Create environment button at the top left.

3. Create the environment with a few application servers and NGINX as a balancer, specify the cloudlet limits, type the name of the environment and click Create button.

NGINX_create_environment

In a minute your environment will be created and appears in the environment list.

4. For setting up, the cache click Config button for NGINX node in your environment and navigate to conf.d > cache.conf.

NGINX_balancer_config

5. For enabling cache uncomment lines as it is shown in the picture below.

NGINX_cach.conf

  • proxy_cache_path /var/lib/nginx/cache [levels=levels] keys_zone=name:size

This directive sets path and other parameters of a cache. The levels parameter defines hierarchy levels of a cache. The keys_zone parameter configure name and RAM size of the zone (for examplejelastic:32m).

  • proxy_cache zone | off, where zone

    – identifier of the cache.

This directive defines a shared memory zone used for caching. The same zone can be used in several places. The off parameter disables caching inherited from the previous configuration level.

  • proxy_temp_path /var/lib/nginx/tmp [level1 [level2 [level3]]]

This defines a directory for storing temporary files with data received from proxied servers. Up to three-level subdirectory hierarchy can be used underneath the specified directory.

  • proxy_cache_valid {number}m

It specifies the time during which the NGINX should keep cash. For example 720m.

Learn more about other available directives.

Note: If it is necessary to clear the cache you can just use proxy_cache_valid directory with the 0m parameter value.

6. Save the changes and restart the node.

NGINX_balancer_restart

Using Nginx PHP server

With Nginx PHP server you can use the caching for improving performance by getting the cache already compiled html replies for repeated php queries.

Note: We limit the size of the disk cache to 5 GB.

To perform caching follow the instruction:

1. Log in to Jelastic Manager.

2. While in Jelastic dashboard, click the Create environment button at the top left.

3. Create the environment with the NGINX as an application server, specify the cloudlet limits, type the name of the environment and click Create button.

Nginx_server_create_environment

In a minute your environment will be created and will appear in the environment list.

4. For setting up the cache click Config button for NGINX node in your environment and navigate to etc > php.ini.

Nginx_server_config

5. For enabling cache on your NginxPHP server you can use APC or eAccelerator.

  • eAccelerator

For using eAccelerator you should uncomment it in the Accelerators section.

Nginx_eAccelerator

Now you need to specify the directory which will be used for the disk cache. For that add the following line as it is shown in the picture above:

eaccelerator.cache_dir = "/var/lib/nginx/cache"

Nginx_eaccelerator_cache

Learn more about eAccelerator additional settings.

  • APC

To use APC accelerator you should just uncomment it in the Accelerators section.

Nginx_APC

Learn more about additional APC functions.

That’s all! Now you can use NGINX caching for your applications. On NGINX it’s so efficient! Forget about the load problems, Nginx in front is a great tool and you can use its abilities to add caching easy.

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInEmail this to someone

6 Comments

Leave a Reply

Contact Jelastic for More Information CONTACT
%d bloggers like this: