1

I have two Node.js apps in my server. They are listing to port 55555. Here is the directory structure.

/home/myuser
  /.htaccess
  /package.json
  /server.js

  /foo.com
    /node_modules
    /public
      /index.html
   /package.json
   /server.js

  /bar.com
    /node_modules
    /public
      /index.html
   /package.json
   /server.js

/home/myuser/server.js content

'use strict';

// Module dependencies.
var express = require('express');
var vhost = require('vhost');

var port = process.env.PORT || 55555;   

function createVirtualHost(domainName, nodeApp) {
    return vhost(domainName, nodeApp);
}

//Create server
var app = express();

var fooApp = require('./foo.com/server.js').app;
var barApp = require('./bar.com/server.js').app;

//Create the virtual hosts
var fooHost = createVirtualHost("www.foo.com", fooApp);
var barHost = createVirtualHost("www.bar.com", barApp);

//Use the virtual hosts
app.use(fooHost);
app.use(barHost);

//Start server
app.listen( port, function() {
    console.log( 'Express server listening on port %d in %s mode', port, app.settings.env );
});

/home/myuser/.htaccess content

RewriteEngine On
RewriteRule ^$ http://127.0.0.1:55555/ [P,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://127.0.0.1:55555/$1 [P,L]

When I trying to access www.foo.com, I am getting error as:

Cannot GET /foo.com/index.html.var

When I trying to access www.bar.com, I am getting error as:

Cannot GET /bar.com/index.html.var

But I can access both sites using port like www.foo.com:55555 and www.bar.com:55555

How could I access sites without using port?

Bishan
  • 15,211
  • 52
  • 164
  • 258
  • Is port 80 already used on your system? If you aren't running node, does the system respond with anything just using http://foo.com/ ? – SPlatten Aug 23 '17 at 09:11
  • @SPlatten I can't use port 80 since I am using managed hosting account. – Bishan Aug 23 '17 at 09:13
  • Then you can't access the URL without using a different port, you have answered your own question. – SPlatten Aug 23 '17 at 09:14
  • @Alex If I removed `.htaccess`, I am getting `You don't have permission to access / on this server.` when I trying to access `www.foo.com` – Bishan Aug 23 '17 at 09:17
  • htaccess is config file for web servers running Apache. Node runs its own server. They're 2 separate things – Alex Aug 23 '17 at 09:19
  • @SPlatten But isn't there a way to redirect users to `foo.com:55555` when they come to `foo.com`? – Bishan Aug 23 '17 at 09:20
  • @Alex I have followed this [article](https://www.a2hosting.com/kb/installable-applications/manual-installations/installing-node-js-on-managed-hosting-accounts) and in there, they said use `.htaccess` to integrate node app with web server. – Bishan Aug 23 '17 at 09:25
  • @Bishan, no because something else is listening on port 80 and when you don't specify a port is the "something" that is going to get the request. – SPlatten Aug 23 '17 at 09:32

1 Answers1

-1

Using Apache as reverse proxy for your app can solve your problem:

<VirtualHost *:80>
    ProxyPreserveHost On
    ProxyPass / http://127.0.0.1:8080/
    ProxyPassReverse / http://127.0.0.1:8080/
</VirtualHost>

Use mod_proxy extension for this. More information is provided in this digitalocean article.

tbking
  • 8,796
  • 2
  • 20
  • 33