Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS Express and Apache on same server

I have a VPS with multiple virtural hosts specified in Apache config (/etc/apache2/sites-available/000-default.conf).

#Example config
<VirtualHost *:80>
    ServerName example.com
    Redirect / https://example.com/
    DocumentRoot /var/www/example
    ServerAdmin [email protected]
</VirtualHost>

<VirtualHost *:80>
    ServerName sub.example.com
    DocumentRoot /var/www/sub.example
    ServerAdmin [email protected]
</VirtualHost>

Also, I have Nodejs app on same server running with port *:3000. The problem is, that some ISP blocks that port and user can't use the app (e.g. in my school I can't access to this port). I want to that this app will run on port *:443, but I still want to use Apache to others host. Is there any way how to have app on same port as Apache and use that app only if user visit domain https://socket.example.com, otherwise it will use Apache. I'm adding example config of my Apache server and part of nodejs app.

var fs = require('fs');
var http = require('http');
var https = require('https');
var privateKey = fs.readFileSync('/etc/letsencrypt/live/socket.example.com/privkey.pem', 'utf8');
var certificate = fs.readFileSync('/etc/letsencrypt/live/socket.example.com/cert.pem', 'utf8');
var bodyParser = require("body-parser");
var credentials = { key: privateKey, cert: certificate };
var express = require('express');
var app = express();
var port = 3000;
var httpsServer = https.createServer(credentials, app);


httpsServer.listen(port, function () {
    console.log('Server running on port: ' + port);
});


var urlencodedParser = bodyParser.urlencoded({ extended: false });

app.use(function (req, res, next) {
    res.header('Access-Control-Allow-Origin', req.headers.origin);
    res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
    res.header('Access-Control-Allow-Credentials', 'true');
    res.header('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type');

    if ('OPTIONS' == req.method) {
        res.writeHead(200);
        res.end();
    } else {
        next();
    }
});


app.get('/', function (req, res) {
    res.send(400);
    console.log("visit");
});

If I forgot about something just let me know and I will add it.

like image 948
Alex Avatar asked Sep 23 '26 21:09

Alex


1 Answers

Create an Apache virtual host which will proxy the request for a node.js application. you can run node.js application to any available port, and proxy it via apache.

<VirtualHost *:443>
   ServerName yourdomain.com
   ServerAlias www.yourdomain.com
   DocumentRoot /var/www/defaultapp/
   Options -Indexes
   ErrorDocument 503 /check.html

   SSLProxyEngine On
   ProxyPass /check.html !
   ProxyPass / https://localhost:3000
   ProxyPassReverse / https://localhost:3000
   ProxyPreserveHost On

   SSLEngine on
   SSLCertificateFile /etc/apache2/ssl/domain.com.crt
   SSLCertificateKeyFile /etc/apache2/ssl/domain.com.key
   SSLCertificateChainFile /etc/apache2/ssl/domain.com:3000.ca-bundle
</VirtualHost>

Please check https://linuxtogether.org/configuring-reverse-proxy-for-node-using-apache-mod-proxy/ for details

like image 191
anand Avatar answered Sep 25 '26 21:09

anand



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!