월요일, 5월 27, 2024
HomeLinuxubuntu NGINX cronolog 적용하기

ubuntu NGINX cronolog 적용하기

ubuntu NGINX cronolog 적용하기

1. cronolog 다운로드

wget --no-check-certificate https://vps.googlecode.com/files/cronolog-1.6.2.tar.gz
tar xf cronolog-1.6.2.tar.gz
    
cd cronolog-1.6.2/

2.configure

./configure


creating cache ./config.cache
checking for a BSD compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking whether make sets ${MAKE}... no
checking for working aclocal... missing
checking for working autoconf... missing
checking for working automake... missing
checking for working autoheader... missing
checking for working makeinfo... missing
checking for gcc... gcc
checking whether the C compiler (gcc  ) works... yes
checking whether the C compiler (gcc  ) is a cross-compiler... no
checking whether we are using GNU C... yes
checking whether gcc accepts -g... yes
checking for a BSD compatible install... /usr/bin/install -c
checking whether ln -s works... yes
checking for ranlib... ranlib
checking for perl... /usr/bin/perl
checking how to run the C preprocessor... gcc -E
checking for ANSI C header files... yes
checking whether stat file-mode macros are broken... no
checking whether time.h and sys/time.h may both be included... yes
checking whether struct tm is in sys/time.h or time.h... time.h
checking for tm_zone in struct tm... yes
checking for fcntl.h... yes
checking for limits.h... yes
checking for unistd.h... yes
checking for working const... yes
checking for size_t... yes
checking whether struct tm is in sys/time.h or time.h... (cached) time.h
checking for strftime... yes
checking for vprintf... yes
checking for mkdir... yes
checking for mktime... yes
checking for putenv... yes
checking for strptime... yes
checking for localtime_r... yes
updating cache ./config.cache
creating ./config.status
creating Makefile
creating lib/Makefile
creating src/Makefile
creating doc/Makefile
creating testsuite/Makefile
creating src/cronosplit

3.make

make

................
gcc  -g -O2  -o cronolog  cronolog.o cronoutils.o ../lib/libutil.a 
make[1]: Leaving directory `/root/cronolog-1.6.2/src'
Making all in doc
make[1]: Entering directory `/root/cronolog-1.6.2/doc'
make[1]: Nothing to be done for `all'.
make[1]: Leaving directory `/root/cronolog-1.6.2/doc'
Making all in testsuite
make[1]: Entering directory `/root/cronolog-1.6.2/testsuite'
make[1]: Nothing to be done for `all'.
make[1]: Leaving directory `/root/cronolog-1.6.2/testsuite'
make[1]: Entering directory `/root/cronolog-1.6.2'
make[1]: Nothing to be done for `all-am'.
make[1]: Leaving directory `/root/cronolog-1.6.2'

4.make install

make install

................
make[3]: Leaving directory `/root/cronolog-1.6.2/doc'
make[2]: Leaving directory `/root/cronolog-1.6.2/doc'
make[1]: Leaving directory `/root/cronolog-1.6.2/doc'
Making install in testsuite
make[1]: Entering directory `/root/cronolog-1.6.2/testsuite'
make[2]: Entering directory `/root/cronolog-1.6.2/testsuite'
make[2]: Nothing to be done for `install-exec-am'.
make[2]: Nothing to be done for `install-data-am'.
make[2]: Leaving directory `/root/cronolog-1.6.2/testsuite'
make[1]: Leaving directory `/root/cronolog-1.6.2/testsuite'
make[1]: Entering directory `/root/cronolog-1.6.2'
make[2]: Entering directory `/root/cronolog-1.6.2'
make[2]: Nothing to be done for `install-exec-am'.
make[2]: Nothing to be done for `install-data-am'.
make[2]: Leaving directory `/root/cronolog-1.6.2'
make[1]: Leaving directory `/root/cronolog-1.6.2'

5. nginx conf 수정

vi /server/nginx/conf/nginx.conf

    user                            www-data www-data;
    
    worker_processes                1;
    worker_rlimit_nofile            100000;
    worker_rlimit_sigpending        10000;
    
    error_log                       /server/nginx/logs/pipe/error_pipe99       debug;
    pid				    /server/nginx/run/nginx.pid;

    events {
            worker_connections      1024;
            accept_mutex_delay      100ms;
            multi_accept            on;
            use epoll;
    }
    
    
    http {
            #--------------------------------------------------------------------------#
            #--- default --------------------------------------------------------------#
            #--------------------------------------------------------------------------#
            include                 mime.types;
    
            sendfile                on;
            tcp_nopush              on;
            gzip                    on;
            tcp_nodelay             on;
            keepalive_timeout       30; 
            keepalive_requests      100000;
            send_timeout            2;
            server_tokens           off;
            server_name_in_redirect off;
     
            default_type            application/octet-stream;
            index                   index.html
    
            #--------------------------------------------------------------------------#
            #--- time -----------------------------------------------------------------#
            #--------------------------------------------------------------------------#
            client_body_timeout     10;
            client_header_timeout   10;
    
            #--------------------------------------------------------------------------#
            #--- proxy ----------------------------------------------------------------#
            #--------------------------------------------------------------------------#
            proxy_redirect          off;
            proxy_set_header        Host            $host;
            proxy_set_header        X-Real-IP       $remote_addr;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
            client_max_body_size    10m;
            client_body_buffer_size 128k;
            proxy_connect_timeout   300;
            proxy_send_timeout      120;
            proxy_read_timeout      300;
            proxy_buffers           32 32k;
            fastcgi_buffer_size     64k;
            fastcgi_buffers         4 64k;
            proxy_ignore_client_abort on;

            proxy_cache_path        /server/nginx/cache/one 
                                    levels=1:2
                                    keys_zone=one:10m
                                    max_size=1500m
                                    inactive=600m; 
            proxy_cache_key         "$scheme$host$request_uri";
            proxy_temp_path         /server/nginx/cache/tmp;
    
            #--------------------------------------------------------------------------#
            #--- log ------------------------------------------------------------------#
            #--------------------------------------------------------------------------#
            log_format main '$remote_addr - $remote_user [$time_local-$msec]  $status '
                            '"$request" $request_time $body_bytes_sent "$http_referer" '
                            '"$http_user_agent" "$http_x_forwarded_for"';
    
            #--------------------------------------------------------------------------#
            #--- - http ---------------------------------------------------------------#
            #--------------------------------------------------------------------------#
            include /server/nginx/conf/conf.d/default.conf;
    
    }

5. server 모듈 분리

mkdir -p /server/nginx/conf/conf.d

vi /server/nginx/conf/conf.d/default.conf

server {
	listen 80;
	server_name localhost;

	charset utf-8;

	access_log  /server/nginx/logs/pipe/access_pipe11  main;
	error_log   /server/nginx/logs/pipe/access_pipe11  debug;

	location / {
	    root   html;
	    index  index.html index.htm;
	}

	location ~ \.php$ {
		try_files $uri =404;
		fastcgi_split_path_info ^(.+\.php)(.*)$;
		fastcgi_pass unix:/var/run/php5-fpm.sock;
		fastcgi_index index.php;
		fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
		include fastcgi_params;
		include mime.types;
	}
}

6. nginx_cronolog script 생성

touch /server/nginx/sbin/nginx_cronolog 
chmod u+x /server/nginx/sbin/nginx_cronolog 

vi /server/nginx/sbin/nginx_cronolog

#!/bin/sh

PATH_NGINX_LOG=/server/nginx/logs
PATH_LOG_PIPE=$PATH_NGINX_LOG/pipe

if [ -f $PATH_NGINX_LOG/pipe ] 
then
	echo ''
else
	mkdir -p $PATH_NGINX_LOG/pipe
fi

CHANNEL11=default

#---------------------------------------------------------------------------------------------------------------------
# delete previous pids
#---------------------------------------------------------------------------------------------------------------------
pids=$(ps -ef | grep /server/nginx/logs/pipe | grep -v grep| awk '{print $2}')

for i in $pids ; do
kill -9 $i
done


#---------------------------------------------------------------------------------------------------------------------
# delete previous pipe
#---------------------------------------------------------------------------------------------------------------------
rm -f $PATH_LOG_PIPE/access*
rm -f $PATH_LOG_PIPE/error*


#---------------------------------------------------------------------------------------------------------------------
# make cronolog pipe
#---------------------------------------------------------------------------------------------------------------------
for j in 11 99; do

`mkfifo $PATH_LOG_PIPE/access_pipe$j`
`mkfifo $PATH_LOG_PIPE/error_pipe$j`

if [ $j -eq 11 ] ; then
    CHANNEL=$CHANNEL11 ; echo $j : $CHANNEL
elif [ $j -eq 99 ] ; then
    CHANNEL=webserver
fi

/bin/sh -c "/bin/cat $PATH_LOG_PIPE/access_pipe$j | /usr/local/sbin/cronolog $PATH_NGINX_LOG/%Y%m/$CHANNEL-access-%Y%m%d.log &"
/bin/sh -c "/bin/cat $PATH_LOG_PIPE/error_pipe$j  | /usr/local/sbin/cronolog $PATH_NGINX_LOG/%Y%m/$CHANNEL-error-%Y%m%d.log &"

done

7. /etc/init.d/nginx script 수정

vi /etc/init.d/nginx

#!/bin/bash
    #
    # description: Starts and stops the Nginx Server
    
    # nginx Service script for Linux
    
    start()
    {
        su -c "/server/nginx/sbin/nginx_cronolog"
        su -c "/server/nginx/sbin/nginx"
    }
    
    stop()
    {
        su -c "/server/nginx/sbin/nginx -s stop"
    }
    
    reload()
    {
        su -c "/server/nginx/sbin/nginx -s reload"
    }
    
    # See how we were called.
    case "$1" in
      start)
            start
            ;;
      stop)
            stop
            ;;
      reload)
            reload
            ;;
      restart)
            stop
            sleep 2
            start
            ;;
      *)
            echo $" Usage {start|stop|restart|reload}"
            exit 1
    esac

8. nginx 재기동

/etc/init.d/nginx restart

9. 로그화일 및 폴더 생성확인

root@ubuntu:/server/nginx/logs$ ll
total 16
drwxr-xr-x 4 www-data www-data 4096 Feb 13 13:07 ./
drwxr-xr-x 8 www-data www-data 4096 Feb 13 11:58 ../
drwxr-xr-x 2 root     root     4096 Feb 13 13:07 201502/
drwxr-xr-x 2 root     root     4096 Feb 13 13:06 pipe/

root@ubuntu:/server/nginx/logs$ ll 201502/
total 16
drwxr-xr-x 2 root     root     4096 Feb 13 13:07 ./
drwxr-xr-x 4 www-data www-data 4096 Feb 13 13:07 ../
-rw-r--r-- 1 root     root      577 Feb 13 13:07 default-access-20150213.log
-rw-r--r-- 1 root     root      926 Feb 13 13:07 webserver-error-20150213.log
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Time limit exceeded. Please complete the captcha once again.

Most Popular