• 흐림동두천 1.0℃
  • 흐림강릉 1.3℃
  • 서울 3.2℃
  • 대전 3.3℃
  • 대구 6.8℃
  • 울산 6.6℃
  • 광주 8.3℃
  • 부산 7.7℃
  • 흐림고창 6.7℃
  • 흐림제주 10.7℃
  • 흐림강화 2.2℃
  • 흐림보은 3.2℃
  • 흐림금산 4.4℃
  • 흐림강진군 8.7℃
  • 흐림경주시 6.7℃
  • 흐림거제 8.0℃
기상청 제공

nginx를 이용한 웹서버 만들기

  • 등록 -0001.11.30 00:00:00

# wget http://nginx.org/download/nginx-0.7.64.tar.gz
# tar xvzf nginx-0.7.64.tar.gz
# cd nginx-0.7.64

# ./configure --pid-path=/usr/local/nginx/logs/nginx.pid --sbin-path=/usr/local/sbin/nginx --with-md5=/usr/lib --with-sha1=/usr/lib --with-http_ssl_module --with-http_dav_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module
# make
# make install


설치 후 /etc/rc.d/init.d/nginx라는 파일에 아래 내용을 넣어 만들어 준다...

그리고 chmod 750 /etc/rc.d/init.d/nginx로 실행 권한을 준다~


#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /var/run/nginx.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

nginx="/usr/local/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"


lockfile=/var/lock/subsys/nginx

start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
$nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}

stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}

restart() {
configtest || return $?
stop
start
}

reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}

force_reload() {
restart
}

configtest() {
$nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
status $prog
}

rh_status_q() {
rh_status >/dev/null 2>&1
}

case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac


그리고 /usr/local/nginx/conf/nginx.conf 파일을 열어 수정~


worker_processes  8;


nginx의 쓰레드 갯수를 정해주는 부분...

cpu 갯수만큼 적어주었다...

한개의 쓰레드는 한개의 cpu만 사용한다고 하니 갯수에 맞게 적어주면 아무래도 cpu를 잘 활용 할듯해서...

8인 이유는 쿼드코어 cpu 2개가 있는 서버이기 때문에... 4 x 2 = 8


events {
    worker_connections  512;
}


매뉴얼에 보면 max_clients = worker_processes * worker_connections라고 한다...

위 설정에서 max_clients는 8 * 512 = 4096

(참고 http://wiki.nginx.org/NginxHttpMainModule#worker_processes)


http {
    ...

    keepalive_timeout  5 5;
    keepalive_requests 5;

    ...

}


keepalive_timeout의 기본 값은 65인데...

이걸 0으로 해도 keepalive가 off로 되지는 않는듯 하다...

알아보니 이렇게 설정한다고 한다.... 정확한지는;;;;

(참고 http://phpschool.com/gnuboard4/bbs/board.php?bo_table=tipntech&wr_id=68979)


http {
    ...

    server {

        ...

        listen 80;

        ...

    }

    ...

}


요건 뭐 서비스할 port설정이고...


http {
    ...

    server {

        ...

        server_name 111.111.111.111 www.mydomain.com;

        ...

    }

    ...

}


서비스할 ip주소나 도메인 설정~


http {
    ...

    server {

        ...

        location / {

               root    /home/www;

            index    index.html index.htm;

            expires 24h;     

        }

        ...

    }

    ...

}


root에는 document root를 설정하고...

index는 index파일의 우선 순위...

이미지 서버이니 expires를 설정해주었다...

(참고 http://wiki.nginx.org/NginxHttpHeadersModule#expires)


좀 더 자세한 설정은 nginx wiki를 참고 하시고~;;

자 이제 start~


/etc/rc.d/init.d/nginx start


ps aux | grep nginx로 확인해보면...

위에서 설정해준대로 8개의 쓰레드가 확인된다...


nobody    7139  0.0  0.0  40992  1548 ?        S    11:49   0:06 nginx: worker process                                   
nobody    7140  0.1  0.0  40992  1548 ?        S    11:49   0:07 nginx: worker process                                   
nobody    7141  0.0  0.0  41252  1736 ?        S    11:49   0:06 nginx: worker process                                   
nobody    7142  0.0  0.0  40992  1548 ?        S    11:49   0:06 nginx: worker process                                   
nobody    7143  0.1  0.0  41116  1664 ?        S    11:49   0:07 nginx: worker process                                   
nobody    7144  0.0  0.0  40992  1548 ?        S    11:49   0:07 nginx: worker process                                   
nobody    7145  0.0  0.0  40992  1548 ?        D    11:49   0:06 nginx: worker process                                   
nobody    7146  0.1  0.0  40992  1548 ?        S    11:49   0:07 nginx: worker process                                   
root     21459  0.0  0.0  40776  1308 ?        Ss   11:18   0:00 nginx: master process /usr/local/sbin/nginx -c /usr/local/nginx/conf/nginx.conf


설치 해서 돌려보니 꽤 만족스러운 성능을 내주고 있다...


nginx에 대한 support는 여기에서~

http://wiki.nginx.org/Main