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.
01$ tzselect
02Please identify a location so that time zone rules can be set correctly.
03Please select a continent or ocean.
04 1) Africa
05 2) Americas
06 3) Antarctica
07 4) Arctic Ocean
08 5) Asia
09 6) Atlantic Ocean
10 7) Australia
11 8) Europe
12 9) Indian Ocean
1310) Pacific Ocean
1411) none - I want to specify the time zone using the Posix TZ format.
15#? 5
16Please select a country.
17 1) Afghanistan           18) Israel                35) Palestine
18 2) Armenia               19) Japan                 36) Philippines
19 3) Azerbaijan            20) Jordan                37) Qatar
20 4) Bahrain               21) Kazakhstan            38) Russia
21 5) Bangladesh            22) Korea (North)         39) Saudi Arabia
22 6) Bhutan                23) Korea (South)         40) Singapore
23 7) Brunei                24) Kuwait                41) Sri Lanka
24 8) Cambodia              25) Kyrgyzstan            42) Syria
25 9) China                 26) Laos                  43) Taiwan
2610) Cyprus                27) Lebanon               44) Tajikistan
2711) East Timor            28) Macau                 45) Thailand
2812) Georgia               29) Malaysia              46) Turkmenistan
2913) Hong Kong             30) Mongolia              47) United Arab Emirates
3014) India                 31) Myanmar (Burma)       48) Uzbekistan
3115) Indonesia             32) Nepal                 49) Vietnam
3216) Iran                  33) Oman                  50) Yemen
3317) Iraq                  34) Pakistan
34#? 43
35 
36The following information has been given:
37 
38        Taiwan
39 
40Therefore TZ='Asia/Taipei' will be used.
41Local time is now:      Sun Jun  9 14:36:42 CST 2013.
42Universal Time is now:  Sun Jun  9 06:36:42 UTC 2013.
43Is the above information OK?
441) Yes
452) No
46#? 1
47 
48You can make this change permanent for yourself by appending the line
49        TZ='Asia/Taipei'; export TZ
50to the file '.profile' in your home directory; then log out and log in again.
51 
52Here is that TZ value again, this time on standard output so that you
53can use the /usr/bin/tzselect command in shell scripts:
54Asia/Taipei
55$
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.
01#! /bin/sh
02 
03### BEGIN INIT INFO
04# Provides:          nginx
05# Required-Start:    $local_fs $remote_fs $network $syslog
06# Required-Stop:     $local_fs $remote_fs $network $syslog
07# Default-Start:     2 3 4 5
08# Default-Stop:      0 1 6
09# Short-Description: starts the nginx web server
10# Description:       starts nginx using start-stop-daemon
11### END INIT INFO
12export TZ="Asia/Taipei"
13 
14PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/local/nginx/sbin:/usr/sbin:/usr/bin
15DAEMON=/usr/sbin/nginx
16NAME=nginx
17DESC=nginx
18...
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