Ahmed Fouad
Ahmed Fouad

Reputation: 300

How to run my NodeJS server behind Apache using .htaccess?

This is my setup.

Here is my .htaccess:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteRule ^server/(.*)?$ http://127.0.0.1:3000/$1 [P,L]

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

</IfModule>

I still can't access my nodejs server from browser or do a call to it from my script (whether its ajax or socket.io)

Please if anyone got a work around for this problem, I've been trying to solve it without any success.

EDIT: This is my server.js

var express = require('express');
var path = require('path');
var mysql = require('mysql');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
var db = require('./db');
var operators = new Array;

// socket.io connection
io.on('connection', function(socket){
    socket.emit('login', { operators: operators });
});

// Request chat button
app.get('/get_button/:key', function(req, res){
    db.connect(function(err){ if (err){ throw err; } else { console.log('DB connected'); }});
    db.query('select * from `wp_oboxchat_keys` where `key` = `' + req.params.key + '` limit 1', function(err, result) {
        if (err) {
            throw err;
        } else {
            res.sendFile(path.join(__dirname, '../', 'app/widget.html'));
            console.log('DB successful query: get_button');
        }
    });
});

// Listen to server
server.listen(3000, function(){ console.log('Server started.'); });

The server runs without any errors on my console with node server.js but its not accessible from a web browser.

Upvotes: 5

Views: 5843

Answers (2)

Anubhav Talukdar
Anubhav Talukdar

Reputation: 121

This helped in my case:

So I had to configure it for a sub-domain. Say subdomain.domain.com was linked to the app/ folder inside the public_html/ folder. I used terminal access to run the NodeJS server(on port 3000) using pm2 inside the app/ folder. In the public_html/app/ folder I added a .htaccess file with the following code inside it:

RewriteEngine on
RewriteRule (.*) http://0.0.0.0:3000/$1 [P,L]
DirectoryIndex disabled

Works like a charm. Added notes:

  1. You can have a .htaccess file inside every folder. So the public_html/test/ folder was running a React app so it had it's own .htaccess file independent of the .htaccess file one directory up.

  2. The address in the .htaccess file can either have 127.0.0.1:3000 or localhost:3000 too.

Upvotes: 3

Ahmed Fouad
Ahmed Fouad

Reputation: 300

Resolved.

in my server.js I had to do this:

var server = app.listen(3000);
var io = require('socket.io').listen(server);

And then in my .htaccess I used mod rewrite as following: (I had my nodeserver in /server/ directory relative to my domain root (where wordpress is installed)

RewriteEngine On
RewriteBase /
RewriteRule ^server/(.*)?$ http://127.0.0.1:3000/$1 [P,L]

That's it. I hope this helps anyone who comes across similar issue.

Upvotes: 3

Related Questions