简体   繁体   English

将子域发送到node.js

[英]Send subdomain to node.js

My work runs a couple different internal web apps on an ubuntu server (10.10) running apache. 我的工作在运行apache的ubuntu服务器(10.10)上运行几个不同的内部Web应用程序。 I'm currently developing another web app, and am seriously considering developing on top of a custom-built node.js web server. 我目前正在开发另一个Web应用程序,我正在认真考虑在定制的node.js Web服务器上进行开发。 My reasoning for wanting to do this is: 我想要这样做的理由是:

  1. Speed/Scalability 速度/可扩展性
  2. Security - Pages will be served with a switch...case, instead of just serving the (potentially malicious) user whatever they ask for. 安全性 - 页面将通过交换机...案例提供,而不是仅仅为(潜在的恶意)用户提供他们要求的服务。
  3. Ease of setup - my intentions are for this to be an open-source project, and node.js is much easier for users to set up, rather than dealing with apache/IIS/etc. 易于设置 - 我的意图是这是一个开源项目,node.js更容易设置用户,而不是处理apache / IIS /等。

My question is, on a server where I've got apache listening to port 80, how can I pass off a certain subdomains to node.js. 我的问题是,在我有apache监听端口80的服务器上,如何将某个子域传递给node.js. I've seen a couple articles about using apache virtual hosts to pass it off, but that seems to defeat the purpose of using node.js. 我已经看过几篇关于使用apache虚拟主机传递它的文章,但这似乎打败了使用node.js的目的。 If I have to go through apache, then all three of my reasons for avoiding apache/IIS have voided themselves. 如果我必须通过apache,那么我避免apache / IIS的所有三个原因都已自我解决。

I know I could use a different port (:8080?), but from an end-user standpoint, it's pretty confusing having to put in custom ports. 我知道我可以使用不同的端口(:8080?),但从最终用户的角度来看,放入自定义端口非常困惑。 Any alternative ideas? 还有其他想法吗?

Thanks 谢谢

<VirtualHost *:80>
ServerName subdomain.yourdomain.com
ProxyPreserveHost on
ProxyPass / http://localhost:8080/
</VirtualHost>

Thanks to http://www.chrisshiplet.com/2013/how-to-use-node-js-with-apache-on-port-80/ 感谢http://www.chrisshiplet.com/2013/how-to-use-node-js-with-apache-on-port-80/

如何做反过来的事情:将节点绑定到端口80,处理针对子域的流量并将其用作apache的反向代理以用于其他所有事情?

if socket.io node is running, be sure to enable also few apache mods: 如果socket.io节点正在运行,请确保启用少量apache mods:

  1. a2enmod proxy a2enmod代理
  2. a2enmod proxy_balancer a2enmod proxy_balancer
  3. a2enmod proxy_express a2enmod proxy_express
  4. a2enmod proxy_http a2enmod proxy_http

in file /etc/apache2/sites-available/chat.example.com.conf 在文件/etc/apache2/sites-available/chat.example.com.conf中


<VirtualHost *:80>
    ServerName chat.example.com

    <Location "/">
        ProxyPreserveHost On
        ProxyPass http://localhost:3000/
        ProxyPassReverse http://localhost:3000/
    </Location>
</VirtualHost>

then of course service apache2 reload 当然service apache2 reload

Let me start from the ground up: 让我从头开始:

You have a DNS . 你有一个DNS And a dns server maps one DNS to one IP! 并且dns服务器将一个DNS映射到一个IP!

You then have apache running on your computer that listens for connections on port 80 for http:// and on port 443 for https:// . 然后,您的计算机上运行apache ,用于侦听端口 80上http://和端口443上https:// http://example/ is actually a request on http://example:80/ . http://example/实际上是http://example:80/的请求。

You can't use node.js to listen on the same machine on the same port as apache. 您不能使用node.js在与apache相同的端口上侦听同一台计算机。 That's why using port 8080 is viable. 这就是为什么使用端口8080是可行的。

You can also map the subdomain to a different IP. 您还可以将子域映射到其他IP。 The only caveat here is that you need to have a public IP Address. 这里唯一需要注意的是,您需要拥有一个公共IP地址。

You can't serve port 80 from both Apache and node.js. 您无法从Apache和node.js提供端口80。 Having Apache as a reverse proxy wouldn't be much efficient and that's why nginx is popular in this scenario. 将Apache作为反向代理并不会有效,这就是nginx在这种情况下很受欢迎的原因。 Other alternative than nginx based reverse proxy can be as Khez suggested mapping your subdomain to different IP address which will node.js program listen to or maybe use node.js itself as a reverse proxy for Apache. 除了基于nginx的反向代理之外的其他选择可以是Khez建议将您的子域映射到node.js程序监听的不同IP地址,或者可能使用node.js本身作为Apache的反向代理。

You could configure a virtual host in apache for your new site and add a permanent redirect within it to the localhost and port used by node.js. 您可以在apache中为新站点配置虚拟主机,并在其中添加永久重定向到node.js使用的localhost和端口。

This is how I do it on a server with several other virtual hosts and my node.js application running on port 3000: 这是我在具有多个其他虚拟主机的服务器上运行的方式,以及在端口3000上运行的node.js应用程序:

NameVirtualHost *:80 NameVirtualHost *:80

[Other virtual hosts omitted for brevity] [为简洁省略了其他虚拟主机]

... ...

ServerName mynewsite.com RedirectMatch (.*) http://localhost:3000 $1 ServerName mynewsite.com RedirectMatch(。*) http:// localhost:3000 $ 1

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

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