How to run node.js Web Server on nginx in Centos 6.4

My previous blog post was about how to run your node.js Webserver on Apache in CentOS 6.4. For any of you who prefer nginx more today I have a quick guide on how to run node.js Web Server on nginx in CentOS 6.4 (Or Linux Machines that uses yum).

First make sure you have nginx installed on your machine.

sudo yum install nginx  

After you have nginx installed you want to modify your virtual.conf file

sudo vim /etc/nginx/conf.d/virtual.conf  

In this file you want to have something like this

server {  
    listen 0.0.0.0:80;
    server_name <yourdomain>;
    access_log /var/log/nginx/<yourdomain>.log;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header HOST $http_host;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://127.0.0.1:<port>;
        proxy_redirect off;
    }

Now you have to change the line with <yourdomain> to whatever your domain name that you will be using and for your node.js server change the <port> accordingly.

You are pretty much done and all you need to do now is run nginx

sudo service nginx start  

If you have it started you will need to restart the service instead

sudo service nginx restart  

It seems to be easier to use nginx than apache to run node.js app but again this is my personal opinion.