Pointing my domain to my node.js instance

2019-04-15 00:40发布

问题:

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

回答1:

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



回答2:

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)



回答3:

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.