Sunday, November 7, 2010

apache と nginx から node.js に proxy させる方法

node.js と nginx のインストールメモと、apache と nginx から proxy させる方法メモ

node.js のインストール

git からとってきて ./configure してみる

cd /home/sikaku/src
git clone git://github.com/ry/node.git
cd node
./configure
しかし、環境が古くて ./configure が通らない
../src/platform_linux.cc: In static member function `static void node::OS::SetProcessTitle(char*)':
../src/platform_linux.cc:29: error: `PR_SET_NAME' was not declared in this scope
Waf: Leaving directory `/home/sikaku/src/node/build'
Build failed:  -> task failed (err #1):
        {task: cxx platform_linux.cc -> platform_linux_4.o}
gmake: *** [all] Error 1
以下を見ると、ほぼ debug 用のようなので、
http://stackoverflow.com/questions/778085/how-to-name-a-thread-in-linux
以下のようなパッチをあててしのぐことにする
[192.168.162.128] diff -ru src/platform_linux.cc src/platform_linux.cc.NEW
--- src/platform_linux.cc       2010-10-22 11:09:49.000000000 +0900
+++ src/platform_linux.cc.NEW   2010-10-22 11:09:46.000000000 +0900
@@ -26,7 +26,7 @@
 void OS::SetProcessTitle(char *title) {
   if (process_title) free(process_title);
   process_title = strdup(title);
-  prctl(PR_SET_NAME, process_title);
+//  prctl(PR_SET_NAME, process_title);
 }
openssl が入っていなかったので、yum install しておく
sudo yum install openssl-devel
ようやく make install まで成功
./configure
make
sudo make install
以下のようなコードを書いて、実行してみる
[192.168.162.128] cat http.js
    var sys = require('sys'),
        http = require('http'),
        port = 8000; 
    http.createServer(function(request, response) {
        response.writeHead(200, {
            'Content-Type': 'text/html'
        });

        response.end( request.url + ' - Hello World\n');
    }).listen(port);
    sys.puts('Server listening on port ' + port);
起動してみる
node http.js &
アクセスしてみる
[192.168.162.128] curl "http://192.168.162.128:8000/test"
/test - Hello World

apache から node.js に proxy させる

rewrite で P をフラグをつければいいだけ
[192.168.162.128] cat /etc/httpd/conf.d/custom.conf
    RewriteEngine on
    RewriteRule /(.*) http://192.168.162.128:8000/$1 [P,L]
再起動
sudo //etc/init.d/httpd restart
apache にアクセスすると動いた。
[192.168.162.128] curl "http://192.168.162.128/test"
/test - Hello World

nginx のインストール

まったく問題なく make install まで成功した
cd /home/sikaku/src
wget "http://www.nginx.org/download/nginx-0.8.53.tar.gz"
tar zxvf nginx-0.8.53.tar.gz
cd nginx-0.8.53
./configure 
make
sudo make install
ポートを変えて起動してみる
[192.168.162.128] diff -ru /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.NEW
--- /usr/local/nginx/conf/nginx.conf    2010-10-22 13:38:25.000000000 +0900
+++ /usr/local/nginx/conf/nginx.conf.NEW        2010-10-22 13:38:33.000000000 +0900
@@ -33,7 +33,7 @@
     #gzip  on;

     server {
-        listen       80;
+        listen       8001;
         server_name  localhost;

         #charset koi8-r;
アクセスしてみる。
[192.168.162.128] curl "http://192.168.162.128:8001/"
<html>
<head>
<title>Welcome to nginx!</title>
</head>
<body bgcolor="white" text="black">
<center><h1>Welcome to nginx!</h1></center>
</body>
</html>

nginx から node.js に proxy させる

以下のように、nginx.conf を書き換えて、nginx を再起動する
[192.168.162.128] cat /usr/local/nginx/conf/nginx.conf | perl -nle 'print unless(/\s*#/ || /^\s*$/)'
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       8001;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        location ~ .*$ {
            proxy_pass   http://192.168.162.128:8000;
        }
    }
}
問題なく動いた
[192.168.162.128] curl "http://192.168.162.128:8001/test"
/test - Hello World