【问题标题】:Apache / NodeJS / Proxy with support for subdirectories支持子目录的 Apache / NodeJS / 代理
【发布时间】:2015-11-12 06:08:46
【问题描述】:

我的 Apache 配置:

<VirtualHost *:80>
    ProxyPreserveHost On
    ProxyRequests Off
    ServerName mydomain.com
    ProxyPass / http://localhost:3000/
    ProxyPassReverse / http://localhost:3000/
</VirtualHost>

我的 NodeJS:

var app = require('express')();
var http = require('http').Server(app);

app.get('/', function(req, res){
  res.sendFile(__dirname + '/index.html');
});

http.listen(3000, function(){
  console.log('Listening on *:3000');
});

这目前在连接到mydomain.com 时有效:它将成功返回index.html。当我尝试连接到mydomain.com/foo 时出现问题。理想情况下,这只会转到http://localhost:3000/foo,但它会显示"Cannot GET /foo"

我如何将mydomain.com/* 重定向到我的 NodeJS 项目目录中的相应路径,而不需要大量:

app.get('/foo', function(req, res){
  res.sendFile(__dirname + '/foo');
});

app.get('/bar', function(req, res){
  res.sendFile(__dirname + '/bar');
});

app.get('/foo/bar', function(req, res){
  res.sendFile(__dirname + '/foo/bar');
});

?

【问题讨论】:

    标签: node.js apache mod-proxy


    【解决方案1】:

    你应该使用带有 express 的静态文件服务模块:

    http://expressjs.com/starter/static-files.html

    在你的情况下,类似的事情:

    app.use(express.static(__dirname, {
      index: 'index.html'
    }))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-16
      • 1970-01-01
      • 1970-01-01
      • 2011-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多