简体   繁体   English

如何在专用服务器上配置我的node.js应用程序使其联机?

[英]How to configure my node.js application on a dedicated server to put it online?

I've installed node.js on my server(it is a virtual windows server). 我已经在服务器(这是一个虚拟Windows服务器)上安装了node.js。 I am also having the domain. 我也有域名。 I want to run my node.js application on a port 8001 so that when I open, say http://example.com:8001/ it will open my application. 我想在端口8001上运行我的node.js应用程序,以便在打开时说http://example.com:8001/它将打开我的应用程序。

Actually, I am also having a PHP site running on Apache server on port 80(XAMPP). 实际上,我还在Apache服务器上的端口80(XAMPP)上运行了一个PHP站点。 It is perfectly working fine when I open say, http://example.com . 当我打开说http://example.com时,它工作得很好。

Thanks 谢谢

In apache, create a new vhost. 在apache中,创建一个新的虚拟主机。 You have to proxy all requests through apache to your node app as apache is listening to port 80. 当apache正在监听端口80时,您必须通过apache将所有请求代理到您的节点应用程序。

<VirtualHost *:80>
    ServerName example.com

    ProxyRequests off

    <Proxy *>
        Order deny,allow
        Allow from all
    </Proxy>

    <Location />
        ProxyPass http://localhost:8001/
        ProxyPassReverse http://localhost:8001/
    </Location>
</VirtualHost>

If you are not on a name-based virtual host environment you can just start your Node.js server and it will be available to the world from the defined port. 如果您不在基于名称的虚拟主机环境中,则只需启动Node.js服务器,它就可以从定义的端口向世界公开。 But if the server IP address runs many services using different domain names and you want your Node.js server to reply only from http://example.com:8001 you can use eg the vhost module on your node server to listen to specific domain only: 但是,如果服务器IP地址使用不同的域名运行许多服务,并且您希望Node.js服务器仅从http://example.com:8001进行答复,则可以使用例如节点服务器上的vhost模块来侦听特定域只要:

var express = require('express'),
    host = require('vhost');

var app = express();
app.get('/', function(req, res) {
  res.send("Hello from vhost");
});

var main = express();
main.use(vhost('example.com', app));

if (!module.parent) {
  main.listen(8001);
}

You can also proxy the requests through a web server like Apache or nginx, but to have the service responding from port 8001 you would need to bind the web server to port 8001 in addition to 80 you are already using, and run your Node.js server on some other port. 您还可以通过Web服务器(例如Apache或nginx)代理请求,但是要使服务从端口8001响应,除了已经使用的80端口之外,还需要将Web服务器绑定到端口8001,然后运行Node.js。服务器在其他端口上。

Generally people prefer using the standard HTTP port and use a web server to reverse proxy traffic to the Node.js server running on a non-privileged port. 通常,人们更喜欢使用标准的HTTP端口,并使用Web服务器将代理流量反向传输到在非特权端口上运行的Node.js服务器。 As you already have the PHP application on your domain name you would then follow advice by @SamT and run your Node.js application from eg http://mynodeapp.example.com 由于您的域名上已有PHP应用程序,因此您将按照@SamT的建议运行,并从例如http://mynodeapp.example.com运行Node.js应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM