Pointing my domain to my node.js instance
Asked Answered
A

3

1

I own a server running whm / cpanel, and I have recently been experimenting with node.js. I have a little node test server running and i would like to point a domain at it. Up till now, I have been using whm to create accounts and deal with domains, but this is a different scenario, and I have little knowledge about how to make a domain point to something on my server when there are multiple other domains pointing to other different things on my server at the same time.

Thanks

Aeneous answered 22/4, 2011 at 16:34 Comment(0)
L
2

Ok, I have never tried this, but you could use a rewrite rule in a .htaccess file and rewrite everything to goto to port 8000 where you have your node.js server running.

So first, set up your node.js server to listen to port 8000.

Then, create your domain like normal in cpanel, and in the doc root add a .htaccess file with this rewrite:

RewriteRule ^ "\ http: \ / \ / 127.0.0.1 \:% (8000) REQUEST_URI" [P, QSA, L]

That should just forward everything internally to port 8000 where node.js will take care of it. But I don't know how persistent connections / websockets will work with this strategy. It may be better to skip apache in those cases by going directly to the port 8000 on your server.

Source: http://es.w3support.net/index.php?db=sf&id=59994

Lacombe answered 22/4, 2011 at 17:20 Comment(0)
L
0

This is a little bit tricky, since cpanel is going to use apache to configure the domains, and apache has already taken port 80 without a doubt, so you can't share port 80 between apache and node.js. You will not be able to configure this through cpanel.

You could just point the domain to your server and have node listen to another port such as 8000.

Then go to http://mydomain.com:8000/

So apache/cpanel handles requests to ports 80 and 443, and node handles requests to port 8000.

It is very possible to run multiple web servers on one box, but they each need their own port(s)

Lacombe answered 22/4, 2011 at 17:5 Comment(2)
That is a very helpful answer and you completely understand my situation, thanks. I have one more questions though, is it possible to point a domain at a specific port on my server without having the port number there, for example: node is living at 123.123.123:8000 and I want to point my domain to that exact ip with that port number, can this be done?Aeneous
No that can't be done actually, but I just found another hack you could try. Will post it as another answer.Lacombe
O
0

I managed to do it like that:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{HTTPS} !=on
  RewriteCond %{SERVER_PORT} !^3000$
  RewriteRule ^ http://%{HTTP_HOST}:3000%{REQUEST_URI} [P,QSA,L]
</IfModule>

Anything else didn't worked for me :(

Here I check if the port is not 3000 and if the protocol is not https then I point the domain to port 3000 (you can put the port you use with node.js) you can remove the RewriteCond %{HTTPS} !=on in case you don't care to check for the protocol.

I do some R&D locally but in production I believe I will use nginx + node instead of apache.

Ostrogoth answered 16/1, 2012 at 5:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.