简体   繁体   中英

Running Node.js behind Apache, .htaccess configuration together with PHP

I'm working on a project where website content is based on PHP(CodeIgniter) behind Apache server, and my duty is to write a node.js as an API to upcoming mobile version of the app. (Note, our hosting is managed, we do not have access to virtualhost so using proxypass is out of reach)

I have to make my node file run behind Apache, in a URL like "domainname/api". When said URL is called, Apache has to bridge this request to localhost:port, where node is living on.

Here is my node.js file:

var fs = require('fs');
var http = require('http');
var app = require('express')();
var options = {
};

app.get('/', function (req, res) {
       res.send('Hello World!');
});

http.createServer( app).listen(61000, function () {
       console.log('Started!');
});

Here is my .htaccess file:

RewriteEngine on 
RewriteRule ^$ /index.php [L] 
RewriteCond %{HTTP_HOST} !^ourdomainname\.com
RewriteRule (.*) http://ourdomainname.com/$1 [R=301,L]
RewriteCond $1 !^(index\.php|info.php|resource|system|user_guide|bootstrap|robots\.txt|favicon\.ico|google<somevalues>.html) 
RewriteRule ^(.*)$ /index.php/$1 [L]

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

Although node works locally(on our managed hosting server) (I can call "lynx localhost:61000/" and see proper output), it doesn't work on outside local. All our calls to "ourdomainname.com:61000/api" is sent to CodeIgniter(PHP), not to our node.js file.

Any help is appreciated.

I had a similiar issue with running Node.js alongside a wordpress application. Wordpress in the root, and the node app in www.website.com/api . Here is a copy of our .htaccess file. We struggled with this a lot. What finally worked (thanks to a suggestion on stackoverflow) was to move the /api redirect to the top of the file. You also don't need the initial '/' before the directory api.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

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

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

</IfModule>

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM