节点js-应用程序没有端口号

时间:2015-09-02 12:15:44

标签: node.js apache url port

我刚刚开始使用节点js,我已经在服务器上开发了一个应用程序和主机,但只有当我分配port number时它才会运行我希望它只运行域名。 我搜索了很多,但它不适合我 我试图重写服务器中的 httpd.conf 文件

  

/etc/httpd/conf/httpd.conf中

<VirtualHost 119.19.52.203:80>
ServerName aip.xyz
ProxyRequests off
<Proxy 119.19.52.203:80>
        Order allow,deny
        Allow from all
</Proxy>

ProxyPass / http://example.xyz:5000/
ProxyPassReverse / http://example.xyz:5000/
ProxyPreserveHost on

并按照此链接

  1. nodejs-Domain without port Number
  2. apache Server Configuration
  3. 这是我的 httpd.conf 文件代码(在服务器上自动生成)

    <VirtualHost 118.18.52.203:80>
    ServerName example.xyz
    ServerAlias www.example.xyz
    DocumentRoot /home/example/public_html
    ServerAdmin webmaster@example.xyz
    UseCanonicalName Off
    CustomLog /usr/local/apache/domlogs/example.xyz combined
    CustomLog /usr/local/apache/domlogs/aip.xyz-bytes_log "%{%s}t %I .\n%{%s}t %O ."
    ## User example # Needed for Cpanel::ApacheConf
    UserDir enabled aip
    <IfModule mod_suphp.c>
        suPHP_UserGroup aip aip
    </IfModule>
    <IfModule !mod_disable_suexec.c>
        <IfModule !mod_ruid2.c>
            SuexecUserGroup aip aip
        </IfModule>
    </IfModule>
    <IfModule mod_ruid2.c>
        RMode config
        RUidGid aip aip
    </IfModule>
    <IfModule itk.c>
        # For more information on MPM ITK, please read:
        #   http://mpm-itk.sesse.net/
        AssignUserID example example
    </IfModule>
    ScriptAlias /cgi-bin/ /home/aip/public_html/cgi-bin/
    # To customize this VirtualHost use an include file at the following location
    # Include "/usr/local/apache/conf/userdata/std/2_2/aip/aip.xyz/*.conf"
    

      

    之后,我已经插入此代码,只需打破配置

    Listen 80
    <VirtualHost example.xyz>
    ServerName example.xyz
    ProxyPass / http://example.xyz:5000/
    ProxyPassReverse / http://example.xyz:5000/
    ProxyPreserveHost On 
    </VirtualHost>
    
      

    这是我的路线代码// app.js

    var express = require('express');
    port = process.env.PORT || 5000
    var path = require('path');
    var favicon = require('serve-favicon');
    var logger = require('morgan');
    var cookieParser = require('cookie-parser');
    var bodyParser = require('body-parser');
    var routes = require('./routes/index');
    var users = require('./routes/users');
    var soft1 = require('./routes/soft1');
    var app = express();
    // view engine setup
    app.set('views', path.join(__dirname, 'views'));
    app.engine('hjs', require('hogan-express'));
    app.set('view engine', 'hjs');
    app.set('layout', 'layout/default');
    app.set('partials', {
    mainHead: "include/main/head",
    mainContent:  "include/main/maincontent",
    mainSlider:  "include/main/slider",
    mainLogo:  "include/main/logo",
    mainTopmenu:  "include/main/topmenu",
    mainSocial: "include/main/socialicons",
    mainPortfolio: "include/main/portfolio",
    mainSubmenu: "include/main/sbumenu"
    });
    app.use(logger('dev'));
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({ extended: false }));
    app.use(cookieParser());
    app.use(express.static(path.join(__dirname, 'public')));
    app.use('/', routes);
    app.use('/users', users);
    app.use('/soft1', soft1);
    app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
    });
    if (app.get('env') === 'development') {
    app.use(function(err, req, res, next) {
        res.status(err.status || 500);
        res.render('error', {
            message: err.message,
            error: err
    });
    });
    }
    app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
        message:err.message,
        error: {}
    });
    });
    app.listen(port, function() {
    console.log('Listening on port ' + port)
    })
    module.exports = app;
    

    它还没有工作,没有正确的路线。当我访问 example.xyz 时,它的show目录结构(应用程序的所有文件和文件夹)。

1 个答案:

答案 0 :(得分:5)

这里有2个进程:

  • NodeJS流程,为您的应用程序提供服务
  • httpd(Apache)进程,通常用作在一个域上提供多个应用程序的入口点。

他们都需要收听端口才能访问。

通常做的是让Apache或Nginx进程在默认端口80上进行侦听,默认端口80是唯一对外部连接开放的http端口。 然后使用此Apache进程代理您在其他端口上运行的应用程序(如5000,不应为外部连接打开)。

在您的httpd conf中,您可以在您的域上,默认端口(80)上有一个入口点,并使用反向代理在端口5000上与您联系节点应用程序:

Listen 80
<VirtualHost aip.xyz>
  ServerName aip.xyz
  ProxyPass / http://aip.xyz:5000/
  ProxyPassReverse / http://aip.xyz:5000/
  ProxyPreserveHost On
</VirtualHost>

当你想在1个端口(通常是80)后面托管多个应用时,这很有用,例如:

Listen 80
<VirtualHost aip.xyz>
  ServerName aip.xyz
  ProxyPass /app1/ http://aip.xyz:5000/
  ProxyPassReverse /app1/ http://aip.xyz:5000/
  ProxyPass /appX/ http://aip.xyz:500X/
  ProxyPassReverse /appX/ http://aip.xyz:500X/
  ProxyPreserveHost On
</VirtualHost>