Jun 10, 2013

f Comment

[SOLVED] Changing Nginx Log Time Zone in SECONDS!

Amazon When you view Nginx's server log the time stamp is based on the time zone set in the configuration file. An example log entry with time zone set to UTC+0 looks like:

[09/Jun/2013:06:37:39 +0000] "GET http://www.mensfashionforless.com/g-by-guess-white-shirt.html HTTP/1.1" 200 6856 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"
Let's say you are located in Taiwan how do you change time zone to that of Taiwan?

I am using Nginx 1.4.1. My default time zone is UTC+0 or GMT+0. My OS is Ubuntu 10.04.4 LTS.
Find the time zone text
Many know they need to indicate the desired time zone in Nginx's configuration file, but they may not know how to find the correct text that reflects the timezone Nginx would understand. It's unbelievable that not many on Google know the answer; so I've decided to walk you through exactly how to find your timezone.

Use Unix command tzselect and follow the prompt to find the text that represents the desired time zone.

If you are looking for a list of valid time zones look no further. Simply use the Linux command tzselect to pinpoint the time zone you need.
Suppose you are looking for Taiwan's timezone here's the trace of running the command tzselect on my Ubuntu box.
$ tzselect
Please identify a location so that time zone rules can be set correctly.
Please select a continent or ocean.
 1) Africa
 2) Americas
 3) Antarctica
 4) Arctic Ocean
 5) Asia
 6) Atlantic Ocean
 7) Australia
 8) Europe
 9) Indian Ocean
10) Pacific Ocean
11) none - I want to specify the time zone using the Posix TZ format.
#? 5
Please select a country.
 1) Afghanistan           18) Israel                35) Palestine
 2) Armenia               19) Japan                 36) Philippines
 3) Azerbaijan            20) Jordan                37) Qatar
 4) Bahrain               21) Kazakhstan            38) Russia
 5) Bangladesh            22) Korea (North)         39) Saudi Arabia
 6) Bhutan                23) Korea (South)         40) Singapore
 7) Brunei                24) Kuwait                41) Sri Lanka
 8) Cambodia              25) Kyrgyzstan            42) Syria
 9) China                 26) Laos                  43) Taiwan
10) Cyprus                27) Lebanon               44) Tajikistan
11) East Timor            28) Macau                 45) Thailand
12) Georgia               29) Malaysia              46) Turkmenistan
13) Hong Kong             30) Mongolia              47) United Arab Emirates
14) India                 31) Myanmar (Burma)       48) Uzbekistan
15) Indonesia             32) Nepal                 49) Vietnam
16) Iran                  33) Oman                  50) Yemen
17) Iraq                  34) Pakistan
#? 43

The following information has been given:


Therefore TZ='Asia/Taipei' will be used.
Local time is now:      Sun Jun  9 14:36:42 CST 2013.
Universal Time is now:  Sun Jun  9 06:36:42 UTC 2013.
Is the above information OK?
1) Yes
2) No
#? 1

You can make this change permanent for yourself by appending the line
        TZ='Asia/Taipei'; export TZ
to the file '.profile' in your home directory; then log out and log in again.

Here is that TZ value again, this time on standard output so that you
can use the /usr/bin/tzselect command in shell scripts:
As you can see the valid time zone string is Asia/Taipei. In the following step we'll be using this value.

Edit Nginx startup script
Edit Nginx startup script which is usually /etc/init.d/nginx. Mine looks like the following.
#! /bin/sh

# Provides:          nginx
# Required-Start:    $local_fs $remote_fs $network $syslog
# Required-Stop:     $local_fs $remote_fs $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the nginx web server
# Description:       starts nginx using start-stop-daemon
export TZ="Asia/Taipei"

As you can see I added export TZ="Asia/Taipei" at the very top. That's all you need for Nginx to understand that you would like to use the indicated time zone in log entries.

Restart Nginx
Restart Nginx. In my case the Unix command is sudo /etc/init.d/nginx restart. Once you reboot Nginx server you'll see that the web log is using the desired time zone as the following snippet shows.

[09/Jun/2013:06:37:39 +0000] "GET /g-by-guess-white-shorts.html HTTP/1.1" 200 6856 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"
[09/Jun/2013:14:37:53 +0800] "GET /h-m-leather-braided-bracelet.html HTTP/1.1" 200 9080 "-" "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_1 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8B117 Safari/6531.22.7 (compatible; Googlebot-Mobile/2.1; +http://www.google.com/bot.html)"

Easy when you know exactly what to do isn't it?

Questions? Let me know!
Please leave a comment here!
One Minute Information - by Michael Wen
ADVERTISING WITH US - Direct your advertising requests to Michael