2012年12月11日火曜日

[nodejs]nodejs インストール

概要
nodejs をソースからインストールする。
インストール

[other]nginx インストールと設定

概要
nginx をリバースプロキシとして使ってみる。
インストール
パッケージからとソースからビルドの2通りある。
  • パッケージからインストール
    注意は任意のモジュールをインストールできない。必要なモジュールがあるならソースから。 基本的にhttp://wiki.nginx.org/InstallJa 参照で。 設定での注意としては、/etc/nginx/nginx.conf が設定ファイルだけど中で include /etc/nginx/conf.d/*.conf; みたいに 設定ファイルが読み込まれてる。/etc/nginx/nginx.conf を必死に設定しても設定かわらないときはこのあたりを疑ってみても。 設定、後勝ち??
  • ソースからインストール
    1. tar -zxvf nginx-1.2.2.tar.gz
    2. cd nginx-1.2.2
    3. yum -y install openssl-devel pcre-devel libxml2-devel libxslt-devel patch
      環境によるけど
    4. git clone https://github.com/yaoweibin/nginx_tcp_proxy_module.git
      websocket をリバースプロキシする nginx_tcp_proxy_module をモジュール追加するので http://d.hatena.ne.jp/shim0mura/20120118/1326916953
    5. patch -p1< nginx_tcp_proxy_module/tcp.patch
    6. ./configure
      ./configure --prefix='/usr/local/nginx' \
      --with-http_ssl_module \
      --with-http_stub_status_module \
      --with-http_xslt_module \
      --with-http_realip_module \
      --with-http_gzip_static_module \
      --add-module=nginx_tcp_proxy_module/
    7. make
    8. sudo make install
    9. vi /etc/init.d/nginx
      #!/bin/sh
      #
      # nginx - this script starts and stops the nginx daemon
      #
      # chkconfig:   - 85 15
      # description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
      #               proxy and IMAP/POP3 proxy server
      # processname: nginx
      # config:      /etc/nginx/nginx.conf
      # config:      /etc/sysconfig/nginx
      # 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/sbin/nginx"
      nginx="/usr/local/nginx/sbin/nginx"
      prog=$(basename $nginx)
      
      #NGINX_CONF_FILE="/etc/nginx/nginx.conf"
      NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
      
      [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
      
      lockfile=/var/lock/subsys/nginx
      
      make_dirs() {
         # make required directories
         user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
         options=`$nginx -V 2>&1 | grep 'configure arguments:'`
         for opt in $options; do
             if [ `echo $opt | grep '.*-temp-path'` ]; then
                 value=`echo $opt | cut -d "=" -f 2`
                 if [ ! -d "$value" ]; then
                     # echo "creating" $value
                     mkdir -p $value && chown -R $user $value
                 fi
             fi
         done
      }
      
      start() {
          [ -x $nginx ] || exit 5
          [ -f $NGINX_CONF_FILE ] || exit 6
          make_dirs
          echo -n $"Starting $prog: "
          daemon $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
          sleep 1
          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 にある
HTTP リバースプロキシ
HTTP リバースプロキシとして使うための設定。/ のアクセスを upstream backend のいずれかに転送する。 proxy_pass の http://backend は upstream の backend と対応しいている。
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


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

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    upstream backend {
      server xxxx.xxxx.xxxx.xxx:port;
      server xxxx.xxxx.xxxx.xxx:port;
      server xxxx.xxxx.xxxx.xxx:port;
    }

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            #root   /usr/share/nginx/html;
            #index  index.html index.htm;
            proxy_pass http://backend;
            proxy_set_header  X-Real-IP  $remote_addr;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

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


    # HTTPS server
    #
    #server {
    #    listen       443;
    #    server_name  localhost;

    #    ssl                  on;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_timeout  5m;

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers   on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
}
WebSocket リバースプロキシ
WebSocket 通信(というかTCP/IP??)をリバースプロキシする。 socket.io なアプリレベルで気をつけとくのは、例えば以下の設定でHTTP通信で 60001 に割り振られた時、 javascript で io.connect(‘http://host:port/‘) するときに、ポートは 60001 にしておくこと。 リバースプロキシだからといって ポート 80 で再度割り振らせると正常に動作しなくなる。
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}

tcp {
  upstream websockets {
    server 127.0.0.1:60001;
    server 127.0.0.1:60002;
    server 127.0.0.1:60003;

    check interval=3000 rise=2 fall=5 timeout=1000;
  }


  server {
    listen  0.0.0.0:80;
    server_name _;

    tcp_nodelay on;
    proxy_pass websockets;


  }
}



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

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       8888;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

        location /websocket_status {
            check_status;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        #error_page   500 502 503 504  /50x.html;
        #location = /50x.html {
        #    root   html;
        #}

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

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


    # HTTPS server
    #
    #server {
    #    listen       443;
    #    server_name  localhost;

    #    ssl                  on;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_timeout  5m;

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers   on;

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

}