Making a droplet's nodejs accessible - along with apache

October 24, 2014 2.6k views

Hi,

I have just installed nodejs, and I have a script running that is listening on port 1337

ie.

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337);
console.log('Server running at 1337/');

I have run:

iptables -F 
iptables -A INPUT -p tcp -m tcp --dport 1337 -j ACCEPT

I did - and stiil do have apache running. And there is an SSL certificate operating on the Ubuntu server.

https://xxx:1337 (where xxx is known by the SSL cert) says 'This webpage is not available'.

Of course, https://xxx brings up the page via apache.

Can I access nodejs while apache is running? What am I missing here?

Colin G

1 comment
  • As long as Apache and the node app are listening on separate ports, there shouldn't be any problems running them together. On the server, what's the output of netstat -plunt ?

2 Answers

https://xxx:1337 (where xxx is known by the SSL cert) says 'This webpage is not available'.

You are accessing port 1337 on your server, which goes directly to your nodejs app. Your app isn't configured to use SSL, so you'll get an error when you try to access it via https://.

In this case I would recommend using Apache as a reverse proxy to your nodejs app which will also serve as an SSL termination proxy so your app wouldn't have to know anything about SSL -- it would just work.

See How To Use Apache HTTP Server As Reverse-Proxy Using mod_proxy Extension | DigitalOcean.

by O.S. Tezer
In this DigitalOcean article, we are going to see set up Apache on Ubuntu 13 and use it as a reverse-proxy to welcome incoming connections and redirect them to application server(s) running on the same network. For this purpose, we are going to use and work with the mod_proxy extension and several other related Apache modules.
  • Does this also apply when not using https?

    eg.
    http://162.243.211.165:1337/ also gets 'webpage not available'

  • That means that your nodejs app isn't accessible. I believe @asb meant netstat instead of namp -- can you run sudo netstat -plunt and post its output?

  • I first started my nodejs script, then ran netstat.

    netstat -plunt
    Active Internet connections (only servers)
    Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
    tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 672/mysqld

    tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 9152/apache2

    tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 31400/sshd

    tcp 0 0 127.0.0.1:1337 0.0.0.0:* LISTEN 13539/nodejs

    tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 9152/apache2

    tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 695/php-fpm.conf)
    tcp6 0 0 :::22 :::* LISTEN 31400/sshd

  • Node.js is listening on 127.0.0.1 i.e. the loopback address. This means that it's only accessible from the droplet itself. Edit your app's main file so that it listens on 0.0.0.0 instead and restart it.

  • Thanks - that was it!

    Now I want to enable access to nodejs via https. Can I do this?

    I have enabled the mod proxy components (proxyhttp, proxyhtml, etc) using a2enmod.

    I see that /etc/apache2/httpd.conf contains the virtual host container for SSL - as below:

    <VirtualHost 162.243.211.165:443>
    ServerAdmin admin@ritc.io
    DocumentRoot /var/www
    ServerName <<<<server name supplied>>>>
    ErrorLog /var/www/logs/error_log
    SSLEngine on
    SSLProtocol all
    SSLCertificateFile /etc/apache2/<<<<name supplied>>>>
    SSLCertificateKeyFile /etc/apache2/<<<<name supplied>>>>
    SSLCACertificateFile /etc/apache2/<<<<name supplied>>>>
    ServerPath /var/www
    <Directory "/var/www">
    </Directory>
    </VirtualHost>

    According to:
    https://www.digitalocean.com/community/tutorials/how-to-use-apache-http-server-as-reverse-proxy-using-mod_proxy-extension

    I need to specify things associated with proxy service.

    My question is: Do I append the following into httpd.conf? I tried this but it did not work.
    <VirtualHost *:*>
    ProxyPreserveHost On
    ProxyPass / http://0.0.0.0:1337/
    ProxyPassReverse / http://0.0.0.0:1337/
    ServerName localhost
    </VirtualHost>

    Or do I place the above virtual host container into
    /etc/apache2/sites-enabled/000-default

    Or do I add the lines into the existing container in httpd.conf? (What to do with ServerName localhost - it is already being used)

    ProxyPreserveHost On
    ProxyPass / http://0.0.0.0:1337/
    ProxyPassReverse / http://0.0.0.0:1337/

    by O.S. Tezer
    In this DigitalOcean article, we are going to see set up Apache on Ubuntu 13 and use it as a reverse-proxy to welcome incoming connections and redirect them to application server(s) running on the same network. For this purpose, we are going to use and work with the mod_proxy extension and several other related Apache modules.

Here is the nmap output:

Starting Nmap 5.21 ( http://nmap.org ) at 2014-10-25 11:57 UTC
Found no matches for the service mask 'lunt' and your specified protocols
QUITTING!

Have another answer? Share your knowledge.