在Apache、.htaccess配置和PHP后面运行Node.js

在Apache、.htaccess配置和PHP后面运行Node.js,node.js,apache,.htaccess,proxy,behind,Node.js,Apache,.htaccess,Proxy,Behind,我正在从事一个项目,其中网站内容基于Apache服务器背后的PHP(CodeIgniter),我的职责是编写一个node.js作为即将发布的移动版应用程序的API。 (注意,我们的主机是托管的,我们无法访问virtualhost,因此无法使用proxypass) 我必须让我的节点文件在Apache后面运行,在类似“domainname/api”的URL中。 调用该URL时,Apache必须将该请求桥接到节点所在的localhost:port 这是我的node.js文件: var fs = req

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

我必须让我的节点文件在Apache后面运行,在类似“domainname/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]
重新编写引擎打开
重写规则^$/index.php[L]
重写cond%{HTTP_HOST}^我们的域名\.com
重写规则(.*)http://ourdomainname.com/$1[R=301,L]
1美元^(索引\.php | info.php |资源|系统|用户指南| bootstrap | robots\.txt | favicon\.ico | google.html)
重写规则^(.*)$/index.php/$1[L]
重写规则^/api$http://127.0.0.1:61000/ [P,L]
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_FILENAME}-D
重写规则^api/(*)$http://127.0.0.1:61000/$1[P,L]
尽管节点在本地工作(在托管托管服务器上)(我可以调用“lynx localhost:61000/”并查看正确的输出),但它在外部本地服务器上不工作。我们对“ourdomainname.com:61000/api”的所有调用都发送到CodeIgniter(PHP),而不是node.js文件


非常感谢您的帮助。

我在运行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>

重新启动发动机
重写基/
重写规则^api/(*)?$http://127.0.0.1:51900/$1[P,L]
重写规则^index\.php$-[L]
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_FILENAME}-D
重写规则/index.php[L]
希望这有帮助