2

我正在开发一个网站内容基于 Apache 服务器后面的 PHP(CodeIgniter) 的项目,我的职责是将 node.js 作为 API 编写到即将推出的应用程序移动版本。(注意,我们的主机是托管的,我们无法访问虚拟主机,因此无法使用 proxypass)

我必须让我的节点文件在 Apache 后面运行,在“域名/api”之类的 URL 中。当调用上述 URL 时,Apache 必须将此请求桥接到节点所在的 localhost:port。

这是我的 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]

虽然节点在本地工作(在我们的托管托管服务器上)(我可以调用“lynx localhost:61000/”并查看正确的输出),但它在本地外部不起作用。我们对“ourdomainname.com:61000/api”的所有调用都发送到 CodeIgniter(PHP),而不是我们的 node.js 文件。

任何帮助表示赞赏。

4

1 回答 1

1

我在运行 Node.js 和 wordpress 应用程序时遇到了类似的问题。根目录中的 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>

希望这可以帮助!

于 2016-10-27T22:45:48.543 回答