【问题标题】:Running Node.js behind Apache, .htaccess configuration together with PHP在 Apache 后面运行 Node.js,.htaccess 配置和 PHP
【发布时间】:2017-01-10 13:38:30
【问题描述】:

我正在开发一个网站内容基于 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 文件。

感谢任何帮助。

【问题讨论】:

    标签: node.js apache .htaccess proxy behind


    【解决方案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>
    

    希望这会有所帮助!

    【讨论】:

    • 需要注意的是,您应该在WP部分之前添加RewriteRule ^api/(.*)?$ http://127.0.0.1:51900/$1 [P,L]以避免当WP更新htaccess时过度使用规则
    猜你喜欢
    • 1970-01-01
    • 2021-05-01
    • 2015-12-01
    • 2021-10-26
    • 1970-01-01
    • 2011-03-28
    • 2015-12-16
    • 2017-03-27
    • 2019-02-28
    相关资源
    最近更新 更多