Displaying posts tagged with

“nginx”

nginxでメンテナンス中画面を表示する

どのURLにアクセスされてもステータスコード503を返して /home/www/maintenance.html を表示する設定。

server {
    server_name  your.hostname; 
    error_page 503 /maintenance.html; 
    location / { 
        return 503;
    }
    location = /maintenance.html {
        root /home/www;
    }
}

同等の設定をApache(>=2.2)で書くと次のようになる。

<VirtualHost *:80>
    ServerName your.hostname
    DocumentRoot /home/www
 
    RewriteEngine On
    RewriteCond %{REQUEST_URI} !=/maintenance.html
    RewriteRule .* - [R=503]
 
    ErrorDocument 503 /maintenance.html
</VirtualHost>

CentOS 5にNginxをインストールする

CentOS 5.5にNginxをセットアップした記録。

EPELリポジトリにrpmパッケージもあるようだが、かなりバージョンが古かったので(0.6.39)ソースからインストールすることにした。現時点での最新版(stable)は0.8.53。

まず下準備。nginxユーザの追加とビルドに必要なパッケージのインストール。

sudo useradd -s /sbin/nologin -d /usr/local/nginx -M nginx
sudo yum install gcc openssl-devel pcre-devel zlib-devel

続いてソースをダウンロードしてコンパイルする。Nginxはコンパイル時にしか拡張モジュールの組み込みができないので、ビルドの過程はシェルスクリプトの形で残しておいた方が良い。コンパイルオプションについてはWikiを参照のこと。一番下のExample 5(RedHat向け)を参考に調整を行った。

#!/bin/sh
 
NGINX=nginx-0.8.53
 
# インストール先は /usr/local/nginx になる
cd $NGINX \
&& make clean \
&& ./configure \
  --conf-path=/etc/nginx/nginx.conf \
  --error-log-path=/var/log/nginx/error.log \
  --pid-path=/var/run/nginx/nginx.pid  \
  --lock-path=/var/lock/nginx.lock \
  --user=nginx \
  --group=nginx \
  --with-http_stub_status_module \
  --with-http_ssl_module \
  --with-http_gzip_static_module \
  --with-http_realip_module \
  --http-log-path=/var/log/nginx/access.log \
  --http-client-body-temp-path=/var/tmp/nginx/client/ \
  --http-proxy-temp-path=/var/tmp/nginx/proxy/ \
  --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
&& make

正常にビルドが完了したらインストール。足りないディレクトリも作っておく。

sudo make install
sudo mkdir /var/tmp/nginx/{proxy,client,fcgi}

この段階で正常に起動するかどうかを確認しておく。rootで ${prefix}/sbin/nginx を実行後、http://hostname/ にアクセスして、「Welcome to nginx!」と表示されたらOK。

sudo /usr/local/nginx/sbin/nginx
 
# 確認できたら終了しておく
sudo /usr/local/nginx/sbin/nginx -s quit

この後、設定ファイル(上のコンパイルオプションの場合は /etc/nginx/nginx.conf)を編集してサーバの設定を行うことになるが、そこは省略。

サービス起動ファイルを作る。CentOS(RedHat系Linux)ならひな形がWikiに用意されているので、必要な部分を書き換えた後 /etc/init.d/nginx に保存して、 chkconfig コマンドで設定する。

# 実行ファイルのパスなど変更しておく
vi /etc/init.d/nginx
 
chmod 755 /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx on
 
# 今すぐ起動するなら
service nginx start

続いてログのローテーションの設定。/etc/logrotate.d/nginx を以下の内容で作成する。パスやパラメータは各自で調整のこと。

/var/log/nginx/*log {
    missingok
    notifempty
    delaycompress
    sharedscripts
    postrotate
        /usr/local/nginx/sbin/nginx -s reopen
    endscript
}

設定ファイルのチェックも行っておく。

sudo logrotate -d /etc/logrotate.conf

設定ファイルの内容について後で書くかも。