Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirecting with htaccess to nodejs app

I'm trying to run a small app I've wrote in nodejs on our server with forever. When I start my app like this:

forever app.js

In my folder /home/me/apps/myapp/ and the app is listening on port 61000

What should be the content of my .htaccess file under mydomain.me/myapp/?

Current .htaccess content (not working):

RewriteEngine On
# Redirect a whole subdirectory:
RewriteRule ^myapp/(.*) http://localhost:61000/$1 [P]
like image 940
fabianmoronzirfas Avatar asked Jul 13 '15 15:07

fabianmoronzirfas


2 Answers

You should use Apache mod_proxy not mod_rewrite to run a Node.js app in Apache:

<VirtualHost :80>
    ServerName example.com

    ProxyRequests off

    <Proxy *>
        Order deny,allow
        Allow from all
    </Proxy>

    <Location /myapp>
        ProxyPass http://localhost:61000/
        ProxyPassReverse http://localhost:61000/
    </Location>
</VirtualHost>

If you can't add a virtual host for your Node app you can try with htaccess and something like this:

RewriteEngine On

RewriteRule ^/myapp$ http://127.0.0.1:61000/ [P,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/myapp/(.*)$ http://127.0.0.1:61000/$1 [P,L]
like image 194
michelem Avatar answered Oct 12 '22 14:10

michelem


I will answer my own question, even though I think Michelems answer is also right.

My initial .htaccess file works.

RewriteEngine On
RewriteRule ^myapp/(.*) http://localhost:61000/$1 [P]

The thing I did wrong was creating actually the folder mydomain.de/myapp/ and put the .htaccess there. I have now the rewirte settings in my DocumentRoot/.htaccess, no public folder called myapp and it works fine.

like image 23
fabianmoronzirfas Avatar answered Oct 12 '22 16:10

fabianmoronzirfas