在Apache后运行Node.js,与PHP一起使用的.htaccess配置

3
我正在处理一个项目,该项目的网站内容基于 Apache 服务器上的 PHP(CodeIgniter),我的职责是编写一个 node.js 作为即将推出的移动应用程序的 API。 (请注意,我们的托管是受管理的,我们无法访问虚拟主机,因此使用 proxypass 是不可行的)
我需要让我的节点文件在 Apache 后面运行,以 URL 形式呈现为“domainname/api”。 当调用该 URL 时,Apache 必须将此请求桥接到 localhost:port,其中 node 正在运行。
以下是我的 node.js 文件:
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!');
});

这是我的 .htaccess 文件:

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]

虽然 Node 在本地(在我们的托管服务器上)可以正常工作(我可以调用“lynx localhost:61000/”并看到适当的输出),但它在本地外部无法工作。我们所有对“ourdomainname.com:61000/api”的调用都被发送到 CodeIgniter(PHP),而不是我们的 node.js 文件。
任何帮助都将不胜感激。
1个回答

2

我曾经遇到过类似的问题,就是在WordPress应用程序的根目录下运行Node.js。 WordPress在根目录下,而节点应用程序在www.website.com/api中。 这是我们.htaccess文件的副本。 我们花费了很多时间来解决这个问题。 最终起作用的方法(感谢stackoverflow上的建议)是将/api重定向移到文件顶部。你也不需要在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>

希望这有所帮助!

需要注意的是,在 WP 部分更新 htaccess 时,应该在 WP 部分之前添加 RewriteRule ^api/(.*)?$ http://127.0.0.1:51900/$1 [P,L],以避免覆盖规则。 - scylla

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接