One of the main methods of optimization is web caching, which is simply a method to store a copy of the web content to accelerate the serving it to the client.

The most famous caching method is setting up a reverse proxy in-front of" /> One of the main methods of optimization is web caching, which is simply a method to store a copy of the web content to accelerate the serving it to the client.

The most famous caching method is setting up a reverse proxy in-front of" /> \n One of the main methods of optimization is web caching, which is simply a method to store a copy of the web content to accelerate the serving it to the client.

The most famous caching method is setting up a reverse proxy in-front of" />

Loader

Nginx FastCGI Caching

Nginx FastCGI Caching

One of the main methods of optimization is web caching, which is simply a method to store a copy of the web content to accelerate the serving it to the client.

The most famous caching method is setting up a reverse proxy in-front of the web server or the application server to store their responses. Another method is enabling the client caching for the static content like images and icons.

This post introduces the setup of Nginx as a reverse proxy in-front of a php5-fpm server serving a WordPress application.

The setup of Nginx caching is a straight forward process. It consists of:

  • Adding fastcgi_cache_path directive in http{} context with specific options (path to the stored cache, cache zone name, zone’s memory size to store the meta information about the cached file ).
  • Adding the fastcgi_cache in the location{} context, to enable the the cache in this location.

[code]fastcgi_cache_path /run/nginxcache/ levels= 1:2 keys_zone=nginx_cache:10m max_size=50m inactive=15m;
fastcgi_cache_key $scheme$proxy_host$uri$is_args$args;[/code]

  • fastcgi_cache_key: adding the key for hashing.

[code]
fastcgi_cache_valid  200 302 304 10m;
fastcgi_cache_valid  301 1h;
fastcgi_cache_valid  any 1m;
fastcgi_cache_methods GET HEAD;[/code]

  • proxy_cache_vaild: specifies the validity time of certain responses.
  • proxy_cache_method: specifies the HTTP request methods to be cached.

 

http {} configuration

[code]
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
fastcgi_cache_path /run/nginxcache/ levels= 1:2 keys_zone=cache:10m max_size=50m inactive=15m;
fastcgi_cache_key   $scheme$proxy_host$uri$is_args$args;
add_header Test-Cache $upstream_cache_status;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
include /etc/nginx/mime.types;
default_type application/octet-stream;
error_log /var/log/nginx/error.log;
gzip on;
gzip_disable "msie6";
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
[/code]

Note that $upstream_cache_status variable is used to validate the caching process.

Nginx will not cache pages that set cookies by default, so it is necessary to ignore the “Set-Cookie” header. Cache-control and Expires request headers may prevent Nginx from caching the content, fastcgi_ignore_headers Disables processing of certain response header fields from the proxied server.

The virtual host configuration

[code]
server {
listen 80 default_server;

root /var/www/spirula;
index index.php index.html index.htm;
server_name example.com;

location / {
try_files $uri $uri/ /index.php?q=$request_uri;
}
location ~ \.php$ {
fastcgi_pass 104.131.208.226:9000;
fastcgi_index index.php;
include fastcgi_params;
>fastcgi_cache cache;
fastcgi_cache_valid 200 302 304 10m;
fastcgi_cache_valid 301 1h;
fastcgi_cache_valid any 1m;
fastcgi_cache_methods GET HEAD;
}
[/code]

Browser Caching

Applying the browser cache to a certain type of content allows the client’s browser to store the content for a certain amount of time. In this case, the content is static files (css, jpeg, xml, etc.).

These configurations will enable the browser caching for static files:

[code]
location ~* ^.+\.(?:css|cur|js|jpe?g|gif|htc|bmp|ico|rtf|png|xml|otf|ttf|eot|woff|mid |midi|woff2|svg|png|ico|zip|tgz|gz|rar|bz2)$ {
expires max;
log_not_found off;
}
[/code]

Speed test

 

Siege is a http benchmarking and stress testing utility.

Using siege to test a 500 MB Ram VPS with one core CPU:

[code]
$siege -v -c70 -t5M example.com</pre>

Transactions:                1615 hits
Availability:               100.00 %
Elapsed time:               299.60 secs
Data transferred:           14.83 MB
Response time:               12.21 secs
Transaction rate:            5.39 trans/sec
Throughput:                 0.05 MB/sec
Concurrency:               65.83
Successful transactions:     1615
Failed transactions:         0
Longest transaction:         13.84
Shortest transaction:       0.80
[/code]

Testing after enabling the caching:

[code]
Transactions:               76472 hits
Availability:               100.00 %
Elapsed time:              299.09 secs
Data transferred:           703.48 MB
Response time:              0.28 secs
Transaction rate:           255.68 trans/sec
Throughput:                 2.35 MB/sec
Concurrency:               71.23
Successful transactions:    76472
Failed transactions:        0
Longest transaction:        3.27
Shortest transaction:       0.26
[/code]

It is clear that the cache improved the response time; decreasing it from 12.21 seconds to 0.28 seconds, which is an average  of 97.7% improvement.

Tags: