Nginx をソースからインストール

Nginx とは

オープンソースのハイパフォーマンスHTTPサーバ及びリバースプロキシで、その高いパフォーマンスと安定性、豊富な機能、設定の容易さ、消費リソースの低さで知られています。 Nginxは従来のwwwサーバとは異なり、リクエストの処理をスレッドに依存していない代わりにスケーラブルな(非同期の)イベント駆動アーキテクチャを採用しています。 このアーキテクチャはメモリ使用量が少ないだけでなく、最も重要な事として、稼働時のメモリ使用量が予測可能であるということです。 Nginx は小規模な VPS から大規模なサーバからなるクラスタまで対応する拡張性を備えています。

ダウンロード

http://wiki.nginx.org/InstallJa

安定版

Nginx 1.0.12 2012年2月6日

http://nginx.org/download/nginx-1.0.12.tar.gz

インストール

nginx はインストール時に必要な機能をコンパイル時に指定します。apacheのように後からモジュールのみインストールする事はできません。 今回は /usr/local/nginx 配下にインストールします。

$ tar zxvf nginx-1.0.12.tar.gz
$ cd nginx-1.0.12


$ ./configure --prefix=/usr/local/nginx-1012 --user=www --group=www --pid-path=/var/run/nginx.pid --error-log-path=/var/log/nginx/error_log --http-log-path=/var/log/nginx/access_log --lock-path=/var/run/nginx.lock --with-http_ssl_module          --with-http_realip_module       --with-http_addition_module     --with-http_image_filter_module --with-http_geoip_module        --with-http_sub_module          --with-http_dav_module          --with-http_flv_module          --with-http_mp4_module          --with-http_gzip_static_module  --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module 
$ make

$ sudo make install
$ sudo ln -s /usr/local/nginx-1012 /usr/local/nginx

nginx設定ファイル

/usr/local/nginx/conf/nginx.conf

このコンフィグはポート80でlistenして起動させ /hello にアクセスがあるとバックエンドの http://127.0.0.1:8089 リバースプロクシする設定です。

nginxでの設定ファイルの書き方は他のwebサーバ(apache)等と比較しても大変分かりやすく楽に設定が書く事が出来ます。

user  www;
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    upstream backend {
        server localhost:8089;
    }

    server {
        listen       80;
        server_name  localhost;

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

        location /hello {

            proxy_pass http://backend;
            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;

            proxy_connect_timeout      90;
            proxy_send_timeout         90;
            proxy_read_timeout         90;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

FreeBSD用 rc.d スタートアップスクリプト

/usr/local/rc.d/nginx

#!/bin/sh
# $FreeBSD: ports/www/nginx/files/nginx.sh.in,v 1.11 2012/02/28 10:26:18 osa Exp $

# PROVIDE: nginx
# REQUIRE: LOGIN cleanvar
# KEYWORD: shutdown

#
# Add the following lines to /etc/rc.conf to enable nginx:
# nginx_enable (bool):          Set to "NO" by default.
#                               Set it to "YES" to enable nginx
# nginx_profiles (str):         Set to "" by default.
#                               Define your profiles here.
# nginxlimits_enable (bool):    Set to "NO" by default.
#                               Set it to yes to run `limits $limits_args`
#                               just before nginx starts.
# nginx_flags (str):            Set to "" by default.
#                               Extra flags passed to start command.
# nginxlimits_args (str):       Default to "-e -U %%WWWOWN%%"
#                               Arguments of pre-start limits run.

. /etc/rc.subr

name="nginx"
rcvar=nginx_enable

start_precmd="nginx_precmd"
restart_precmd="nginx_checkconfig"
reload_precmd="nginx_checkconfig"
configtest_cmd="nginx_checkconfig"
gracefulstop_cmd="nginx_gracefulstop"
upgrade_precmd="nginx_checkconfig"
upgrade_cmd="nginx_upgrade"
command="/usr/local/nginx/sbin/nginx"
_pidprefix="/var/run/nginx"
pidfile="${_pidprefix}.pid"
required_files=/usr/local/nginx/conf/nginx.conf
extra_commands="reload configtest upgrade gracefulstop"

[ -z "$nginx_enable" ]          && nginx_enable="NO"
[ -z "$nginxlimits_enable" ]    && nginxlimits_enable="NO"
[ -z "$nginxlimits_args" ]      && nginxlimits_args="-e -U %%WWWOWN%%"

load_rc_config $name

if [ -n "$2" ]; then
        profile="$2"
        if [ "x${nginx_profiles}" != "x" ]; then
                pidfile="${_pidprefix}.${profile}.pid"
                eval nginx_configfile="\${nginx_${profile}_configfile:-}"
                if [ "x${nginx_configfile}" = "x" ]; then
                        echo "You must define a configuration file (nginx_${profile}_configfile)"
                        exit 1
                fi
                required_files="${nginx_configfile}"
                eval nginx_enable="\${nginx_${profile}_enable:-${nginx_enable}}"
                eval nginx_flags="\${nginx_${profile}_flags:-${nginx_flags}}"
                eval nginxlimits_enable="\${nginxlimits_${profile}_enable:-${nginxlimits_enable}}"
                eval nginxlimits_args="\${nginxlimits_${profile}_args:-${nginxlimits_args}}"
                nginx_flags="-c ${nginx_configfile} -g \"pid ${pidfile};\" ${nginx_flags}"
        else
                echo "$0: extra argument ignored"
        fi
else
        if [ "x${nginx_profiles}" != "x" -a "x$1" != "x" ]; then
                for profile in ${nginx_profiles}; do
                        echo "===> nginx profile: ${profile}"
                        /usr/local/etc/rc.d/nginx $1 ${profile}
                        retcode="$?"
                        if [ "0${retcode}" -ne 0 ]; then
                                failed="${profile} (${retcode}) ${failed:-}"
                        else
                                success="${profile} ${success:-}"
                        fi
                done
                exit 0
        fi
fi

nginx_checkconfig()
{
        echo "Performing sanity check on nginx configuration:"
        eval ${command} ${nginx_flags} -t
}

nginx_gracefulstop()
{
        echo "Performing a graceful stop:"
        sig_stop="QUIT"
        run_rc_command ${rc_prefix}stop $rc_extra_args || return 1
}

nginx_upgrade()
{
        echo "Upgrading nginx binary:"

        reload_precmd=""
        sig_reload="USR2"
        run_rc_command ${rc_prefix}reload $rc_extra_args || return 1

        sleep 1

        echo "Stopping old binary:"

        sig_reload="QUIT"
        pidfile="$pidfile.oldbin"
        run_rc_command ${rc_prefix}reload $rc_extra_args || return 1
}

nginx_precmd()
{
        nginx_checkconfig

        if checkyesno nginxlimits_enable
        then
                eval `/usr/bin/limits ${nginxlimits_args}` 2>/dev/null
        else
                return 0
        fi
}

run_rc_command "$1"

自動起動設定

/etc/rc.conf

nginx_enable="YES"

nginx 起動・ストップ・再起動

$ sudo /usr/local/etc/rc.d/nginx start
$ sudo /usr/local/etc/rc.d/nginx stop
$ sudo /usr/local/etc/rc.d/nginx restart
created:

Back to top