Set up nginx as a reverse proxy to node.js

- nginx.conf

http {
……
upstream backend {
server 127.0.0.1:1337;
}
……
server {
listen 80;
server_name example.com;

location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://backend/;
proxy_redirect off;
}
}

- node.js

[root@yongbok ~]# cd /usr/local/src
[root@yongbok ~]# wget http://nodejs.org/dist/v0.8.1/node-v0.8.1.tar.gz
[root@yongbok ~]# tar xzvf node-v0.8.1.tar.gz
[root@yongbok ~]# cd node-v0.8.1
[root@yongbok ~]# ./configure –prefix=/usr/local/node.js && make && make install
[root@yongbok ~]# echo ‘PATH=$PATH:/usr/local/node.js’ >> ~/.bash_profile && export PATH

[root@yongbok ~]# nano /home/ruo91/node.js/app.js
var http = require(‘http’);

http.createServer(function (req, res){
res.writeHead(200, {‘Content-Type’: ‘text/plain’});
res.end(‘Hello World\n’);
}).listen(1337, ’127.0.0.1′);

console.log(‘Server running at http://127.0.0.1:1337′);

[root@yongbok ~]# node /home/ruo91/node.js/app.js
Server running at http://127.0.0.1:1337

-test

[root@yongbok ~]# GET http://example.com/
Hello World

Loading Disqus Comments ...
Loading Facebook Comments ...

No Trackbacks.