在端口80上使用apache运行Nodejs Server

时间:2013-09-12 12:49:42

标签: apache node.js proxy port

我有一个应用程序可以侦听端口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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

您的vhost配置看起来很好,您是否已将url添加到主机文件中?