22 July 2014

If you are running in AWS environment it might be useful to get Amazon instance_id in Nginx for different purposes (logging, headers etc).

To do that we need to compile Nginx with Perl support (--with-http_perl_module flag during compilation). After that is done we can put this snippet inside of our httpd Nginx config section:

Important: While the Perl module is performing a long-running operation, such as resolving a domain name, connecting to another server, or querying a database, other requests assigned to the current worker process will not be processed. It is thus recommended to perform only such operations that have predictable and short execution time, such as accessing the local file system. That’s why we added simple caching.

Reload Nginx and now you should have $instance_id variable set.

You can use it in access log:

log_format main '$msec\t$remote_addr\t$remote_user\t$time_iso8601\t$request\t$status\t$body_bytes_sent\t"$http_referer"\t"$http_user_agent"\t$geoip_country_code\t$request_time\t"$http_x_forwarded_for"\t$http_host\t$instance_id';

Or set custom header to simplify debugging:

add_header x-by "$instance_id";

Your feedback is always welcomed!

Contact me on Codementor


Some popular ones

My books recommendations

Great book for operations people. Helped me to design and build solid deployment pipelines. Awesome advices on automated testing as well. The author advocates against feature branches, every commit goes to master! Scary? I know, but it actually makes sense once you get the idea. Read the book to find out more.

One of those rare books where every word counts!

Classics from John Allspaw who is SVP of Infrastructure and Operations at Etsy (and used to work for Flickr). The book covers very important topics like metrics collection, continuous deployment, monitoring, dealing with unexpected traffic spikes, dev and ops collaboration and much more. Def recommend if you are starting out in the operations field or been doing it for a while ( in latter case you probably read this book already :).

This book is must read for every software engineer, no matter which language you use! It will change your perspective on writing code. I was amazed by the quality of material - very detailed and up to the point.

"The only way to make the deadline -- the only way to go fast -- is to keep the code as clean as possible at all times."


blog comments powered by Disqus