2024/02/13 22:13:39
nginx
リクエストを非同期処理する高速軽量なリバースプロクシ&WEBサーバ
nginx とは
nginx は、高速軽量なリバースプロクシであり、Webサーバです。リクエストをスレッドに依存せず、非同期のイベント駆動アーキテクチャを用いています。
設定が容易で、メモリ消費が少なく、小規模な VPS から大規模なサーバからなるクラスタまで対応する拡張性を備えています。
ダウンロードとインストール
事前準備
rewriteモジュール用に、pcre/pcre-devel と、SSLを有効にするなら openssl/openssl-devel あたりが必要になる。
# yum install pcre pcre-devel # yum install openssl-devel
インストール
# wget http://nginx.org/download/nginx-0.8.53.tar.gz # tar zxvf nginx-0.8.53.tar.gz # cd nginx-0.8.53 # ./configure # make && make install
コンパイルオプションはここに一覧があります。
configure オプション例
./configure --with-http_ssl_module --with-http_realip_module --with-http_sub_module --without-http_autoindex_module
| ファイル/ディレクトリ | パス |
|---|---|
| binary file | /usr/local/nginx/sbin/nginx |
| configuration | /usr/local/nginx/conf |
| configuration file | /usr/local/nginx/conf/nginx.conf |
| pid file | /usr/local/nginx/logs/nginx.pid |
起動/再起動コマンド
/etc/init.d/nginx
$ service nginx
Usage: /etc/init.d/nginx {start|stop|restart|reconf|relogs|status|test}
#!/bin/bash
# script of nginx controller
. /etc/rc.d/init.d/functions
#----- const
BASE_PATH=/usr/local/nginx
BIN_PATH=/sbin/nginx
PID_PATH="$BASE_PATH/logs/nginx.pid"
#----- start nginx
start(){
$BASE_PATH$BIN_PATH
}
#----- graceful shutdown
stop(){
kill -QUIT `cat $PID_PATH`
}
#----- reload config and graceful restart nginx
reconf(){
kill -HUP `cat $PID_PATH`
}
#----- reopen logs
relogs(){
kill -USR1 `cat $PID_PATH`
}
#----- show nginx procs
status(){
ps aux | grep 'nginx:' | grep -v grep
}
#----- check config
test(){
$BASE_PATH$BIN_PATH -t
return $?
}
case "$1" in
start)
echo 'start nginx'
start
;;
stop)
echo 'stop nginx'
stop
;;
restart)
stop
sleep 2
start
;;
reconf)
reconf
;;
relogs)
relogs
;;
status)
status
;;
st)
status
;;
test)
test
;;
t)
test
;;
*)
echo "Usage: $0 {start|stop|restart|reconf|relogs|status|test}"
esac
exit 0
地味に status=st, test=t っていうショートカットもあります。
nginx.conf 設定
サンプルたくさんあります。 http://wiki.nginx.org/NginxConfiguration
ログのローテート設定
[/root/bin/nginx_logrotate.sh]
DIR=/path/to/nginx/log_dir
TIME=`date -d '1 days ago' +%Y%m%d`
mv $DIR/error.log $DIR/error.log.${TIME}
mv $DIR/access.log $DIR/access.log.${TIME}
kill -USR1 `cat /path/to/master.nginx.pid`
[crontab]
# nginx logrotate 0 0 * * * cd /root/bin; ./nginx_logrotate.sh