1

我有一个侦听端口 8080 的应用程序我想将所有 :80 连接传递到 :8080 然后到 :80 以查看 Web 服务器中的 php 页面

这是我的 app.js

var http   = require('http'),
    sockjs = require('sockjs'),
    fs     = require('fs'),
    url    = require('url'),
    path   = require('path'),
    mime   = require('mime'),
    zlib   = require('zlib'),
    shared = require('./js/shared.js');

var compressible_cache = {};

function static_server (request, response) {

  function get_filepath(request, response, callback){
    function response_not_found(response){
      response.writeHead(404, {'Content-Type': 'text/plain'});
      response.write('404 Not Found');
      response.end();
    }

    var uri = url.parse(request.url).pathname;
    var filepath = path.join('/var/www', 'webroot', path.normalize(uri));
    fs.exists(filepath, function(exists){
      if(!exists){
        response_not_found(response);
      }else{
        if(fs.statSync(filepath).isDirectory()) filepath = path.join(filepath, 'index.html');
        fs.exists(filepath, function(exists){
          if(!exists){
            response_not_found(response);
          }else{
            callback(filepath);
          }
        });
      }
    });
  }

  function compression_type(request, mimetype){
    const compressible_mimetypes = {'application/javascript': true, 'text/html': true,
                                    'text/css': true, 'model/vnd.collada+xml': true };
    if(!(mimetype in compressible_mimetypes)) return null;
    var accept_encoding = request.headers['accept-encoding'];
    if(!accept_encoding) return null;
    if(accept_encoding.match(/\bdeflate\b/))
      return 'deflate';
    else if(accept_encoding.match(/\bgzip\b/))
      return 'gzip';
    else
      return null;
  }

  function consume_stream(stream, callback){
    var buffer = new Buffer(0);
    stream.resume();
    stream.on('data', function(data){ buffer = Buffer.concat([buffer, data]); });
    stream.on('end',  function(){ callback(buffer); });
  }

  function send_compressed(response, mimetype, compression, data){
    response.writeHead(200, {'Content-Type': mimetype, 'Content-Encoding': compression });
    response.end(data);
  }

  get_filepath(request, response, function(filepath){
    var mimetype = mime.lookup(filepath);
    var compression = compression_type(request, mimetype);
    if(!compression){
      response.writeHead(200, {'Content-Type': mimetype});
      fs.createReadStream(filepath).pipe(response);
    }else{
      if(!(filepath in compressible_cache)) compressible_cache[filepath] = {};
      if(!(compression in compressible_cache[filepath])){
        var compressor = (compression == 'deflate') ? zlib.createDeflate(): zlib.createGzip();
        consume_stream(fs.createReadStream(filepath).pipe(compressor), function(data){
          compressible_cache[filepath][compression] = data;
          send_compressed(response, mimetype, compression, data);
        });
      }else{
        send_compressed(response, mimetype, compression, compressible_cache[filepath][compression]);
      }
    }
  });
}
//HERE A LOT OF FUNCTION TATH DON'T TO KNOW NEED
var app = http.createServer(static_server);
comms.installHandlers(app, {prefix: '/comm'});
app.listen(8080);

这是我的 httpd.conf

NameVirtualHost *:80
<VirtualHost *:80>
    ServerName moneycheckers.net
    ServerAlias www.moneycheckers.net
    DocumentRoot /var/www/html
    ProxyRequests Off
    ProxyPreserveHost On
    <Location />
        ProxyPass http://localhost:8080/
        ProxyPassReverse http://localhost:8080/
    </Location>
</VirtualHost>

但是当我去 www.mywebsite/page.php 我得到“404 Not Found” 因为服务器执行应用程序而不是 apache 我希望服务器执行 apache 但将客户端信息发送到 app.js

4

2 回答 2

0

您的虚拟主机配置看起来不错,您是否将 url 添加到主机文件中?

于 2015-03-10T21:20:29.463 回答
0

您可能还需要为您的 8080 端口创建一个虚拟主机?

于 2014-01-17T20:24:39.103 回答