Categories
caching configuration microcaching nginx SSI module web-server

SSI module : Date wise caching in Nginx

Hi everyone, today we are going to see how to implement date wise caching in Nginx using SSI module. There happen to be time when we want to refresh the cache as soon as new date comes. That is, we need such caching when we have our content as static but only for a specific date and it changes with new date.

For the date-wise caching, it doesn’t need actually a lot of work. You just need to change the fastcgi_cache_key and append date as well. For the date, we can use $date_local variable in nginx. But the problem comes as $date_local contains time as well, it is in non readable format, just numbers, (we can’t rewrite it).

SSI module comes for the rescue here. SSI module is a great implementation in nginx which processes SSI(Server Side Include) commands in the response to be sent. We can format the $date_local variable using this, to a readble form and even modify it for just the date.

Then we can use $date_local variable and append it with cache_key. Please follow below to see some codes of how to achieve it.

server {

ssi on;

location ~ \.php$ {

fastcgi_cache_key $your_key|$date_local;

}

}

Now, in somewhere in your view(response to be sent) html tag include below ssi command,

less than html greater than

less than!–# config timefmt=”%A, %d-%b-%Y” –greater than

less than/html greater than

Now, above command will be executed by Nginx before it is sent to client. It changes the $date_local default format to the desired format. And so your cache_key will be generated date-wise. As soon as, new date comes a new cache will be generated, and for the same date, the response from the cache will be served.

So now with the help of SSI module of Nginx, date wise caching can be easily done.
Read my article here for how to install nginx from source, which is needed to install third party module(SSI module) for Nginx.

Categories
configuration install modules nginx source ubuntu

Install Nginx from Source in Ubuntu

Hello Everyone, the very handy command to ubuntu users installing nginx, sudo apt-get install nginx, that’s it. It does install nginx on your machine, with almost all the modules usually needed by the users, super awesome. The shortcoming of this method of installing nginx is that if you want to add more modules to the nginx, you just can’t do it. Currently, nginx installed from apt-get do not support adding custom modules to it. Now when you look over internet about how to add module in nginx, it suggests you to install nginx from source. Some times it gets so much confusing, there is something, which always holds developer back to install it this way(As they already had an easy way).

So here I will show you that installing nginx from source is as easy as the apt-get(almost) and interesting as well. Lets do it,

1. Find the stable version of Nginx (http://nginx.org/download/nginx-1.10.2.tar.gz, current stable version) compatible with your custom module you need to add(if the module doesn’t support all versions of nginx) from its download page. (tar file used here)

2. open terminal

3. sudo apt-get install build-essential libpcre3 libpcre3-dev libssl-dev (install the tools required to compile nginx from source)

4. sudo su (get the super user privilege).

5. mkdir nginx (create a directory named nginx{not necessarily}, all the work will be done into this directory)

6. cd nginx (get into nginx directory)

7. wget http://nginx.org/download/nginx-1.10.2.tar.gz (download your compatible version of nginx)

8. tar -xzvf nginx-1.10.2.tar.gz(extract the downloaded content)

9. cd nginx-1.10.2 (get into nginx directory)

10. ./configure (run the configure file)

11. make & make install (google to know about them at your will, I got to work for that or just run them)

12. sudo /usr/local/nginx/sbin/nginx -s stop (stop the nginx server if already running)

13. sudo /usr/local/nginx/sbin/nginx (start the nginx server)

14. enter the ip of your server, and you must be able to see the default nginx page.

So, that’s it. You have now installed your nginx from source.
Now, the commands for starting and stopping and the locations of the config files are different than /etc/nginx. The nginx files are kept in /usr/local/nginx.

Configuration file: /usr/local/nginx/conf/nginx.conf
Start server : /usr/local/nginx/sbin/nginx
Stop server: /usr/local/nginx/sbin/nginx -s stop
Reload server : /usr/local/nginx/sbin/nginx -s reload
Other commands you can find by tabbing after -s or reading the /usr/local/nginx/sbin/nginx file.

Now you can add any module to your nginx, by compiling nginx with the module. Go ahead and learn and test different great available modules of nginx. One with which I’m currently fascinated with is rtmp module.

Note.
1. Each time you are adding a module, make sure you add all other modules(which you earlier had compiled with and still need them) as well.

2. Each time after the module is added, restart the nginx i.e., stop and start, rather than reload.

Categories
access-log configuration custom-log error-log log-format logging monitoring nginx web-server

Monitoring : How to customize the error log in nginx?

Well, recently I came around a task in which I had to format the error log for nginx web server. I googled and I managed to use log_format directive to format access logs. Being lazy, I went for the same way to customize error log(I did it on live machine) and bang. It was giving error, and I just couldn’t believe why it won’t work as it was, a while ago, and now it it isn’t. After some time I realized, that there is difference in type of logs. It worked for access logs but not for error logs.

I then looked in detail, that, it indeed works for only for access logs and we can not customize the error logs in nginx.

Yes, I was puzzled when I saw following.

Capture

I was like what to do now.

Then I thought wait, I have a work around.

When we go through back, we note that, 1- we can format access logs, 2-we know that we have a directive called error_page, so why not create access log only when error occurs. Yes this is the work around.

This is how we can do it.

http {
...
  log_format custom_combined "...";
  server {
    ...
    error_page 50x @create_custom_error50x;
    ...
    location @create_custom_error50x {
      access_log path custom_combined;
      return 50x;
    }
  }
}

Explanation

1 – log_format defines a format for the logs in http context.

2 – error_page directive internally redirect to create_custom_error50x named location on 50x error.

3 – inside create_custom_error50x location we create formatted access log using access_log directive.

4 – it returns the respective error(otherwise it will 404 error and we won’t know the error type occured) using return directive.

So, we can similarly write different location blocks and error_page statements for different errors. If the error code doesn’t matter, then all of them combined as well.

SO YEAH, GO AHEAD WITH YOUR CUSTOMIZED ERROR LOG. 🙂

Links : The same search result in screenshot with one more answer.

Read about log_format, error_page.

Categories
bypass caching configuration fastcgi full-page-caching mastering-nginx microcaching nginx performance static upstream

Microcaching : How to do caching in nginx web server?

Caching is a technique to speed up the response of your website’s static content, content which does not change with time. Microcaching is a type of caching which has short expiry time of cache. This article is about how to set up microcaching in nginx web server.

If you have a website and it contain webpages which do not change, then you are at the right place. Yes you can improve your website’s performance by many folds.

How caching improves performance?

Whenever some request on your website is made, it first goes to your Nginx web server (reverse proxy server). Then it is forwarded to some upstream server to run php-code. But if you have some static content, calling upstream servers is just an overhead. You would always want to keep your upstream servers as free as possible because they are already slow. What can be done is unnecessary or repeated request are served by Nginx itself(this guy is damn fast), relieving upstream servers. This is where caching shows up. Whenever there is some static content request, they are cached by Nginx. On further request of same data they are served by Nginx from its cache.

Nginx does full page caching i.e., it caches the data in its html form. The cache data might be encoded depending on the response from Upstream servers(first time response).

How is Caching done?

We will see a simple configuration for caching using Nginx. It might be made more efficient on further deep study of the same.

First we need to define a caching region, in http context of your Nginx configuration,

fastcgi_cache_path /var/nginx/cache levels=1:2 keys_zone=MYCACHE:10m;

Then, we got to use this cache region, [following should be included in proper context, possibly server context]

fastcgi_cache MYCACHE;

Define the cache key,

fastcgi_cache_key $server_name|$request_uri;

You can also create conditions to bypass and to avoid the caching using following parameters,

fastcgi_cache_bypass ByPass; [Bypass is “1” to bypass]

fastcgi_cache_nocache NoCache; [NoCache is “1” to avoid cache]

This is it, this much should do it. You are set for microcaching of static content of your website on your nginx web server.

Have a rocking Deepawali with Caching !

Do contact me in case of any query.