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.