victormejia
victormejia

Reputation: 1184

deploying Node.js app for production

Most of the tutorials I've come across, you set up a Node.js web app by setting the server to listen on a port, and access it in the browser by specifying that port.. However, how would I deploy a Node.js app to be fully accessible by say a domain like foobar.com?

Upvotes: 4

Views: 660

Answers (5)

Richard Torcato
Richard Torcato

Reputation: 2762

Use pm2 to run your node apps on the server.

Then use Nginx to proxy to your node server. I know this sounds weird but that's the way it's done. Eventually if you need to set up a load balancer you do that all in Nginx too.

server {
listen 80;

server_name example.com;

location / {
    proxy_pass http://APP_PRIVATE_IP_ADDRESS:8080;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
}

}

This is the best tutorial I've found on setting up node.js for production.

https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-14-04

For performance you also setup nginx to serve your public files.

location /public {
    allow all;
    access_log off;
root /opt/www/site;

}

Upvotes: 1

alditis
alditis

Reputation: 4817

An alternative would be to redirect:

http://www.foobar.com to http://www.foobar.com:82

Regards.

Upvotes: 1

chovy
chovy

Reputation: 75656

I just created an "A record" at my registrar pointing to my web server's ip address. Then you can start your node app on port 80.

Upvotes: 1

nkuttler
nkuttler

Reputation: 56

Your question is a little vague.. If your DNS is already configured you could bind to port 80 and be done with it. However, if you already have apache or some other httpd running on port 80 to serve other hosts that obviously won't work.

If you prefer to run the node process as non-root (and you should) it's much more likely that you're looking for a reverse proxy. My main httpd is nginx, the relevant option is proxy_pass. If you're using apache you probably want mod_proxy.

Upvotes: 1

jwchang
jwchang

Reputation: 10864

You have to bind your domain's apex (naked domain) and usually www with your web server's ip or it's CNAME.

Since you cannot bind apex domain with CNAME, you have to specify server IP or IPs or load balancers' IPs

Upvotes: 1

Related Questions