在Node.js中处理虚拟主机的最佳实践 [英] Best practice to handle vhosts in nodejs

查看:114
本文介绍了在Node.js中处理虚拟主机的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在node.js中处理虚拟主机的最佳实践是什么?

What is the best practice to handle virtual hosts in node.js?

我需要将域路由到每个单独的http服务器.

I need to route domains to each individual http server..

http://api.localhost:8080 => localhost:9000
http://www.localhost:8080 => localhost:9001
https://secure.localhost:8080 => localhost:9002 // this request is HTTPS

我正在使用快递http

I am using express http

推荐答案

在端口80上使用nginx,然后使用对节点服务器的反向代理定义nginx中的服务器(虚拟主机)是很常见的.之所以如此普遍,是因为nginx在提供静态内容方面表现出色,因此您可以通过告诉它您的公共目录位置来做到这一点.

It's very common to use nginx on port 80, and then define servers (vhosts) within nginx with a reverse proxy to your node servers. The reason it's so common is because nginx is exceptional at serving static content, so you let it do just that by telling it your public directory location.

这是服务器(虚拟主机)配置的示例.您将创建一个server { }块,并为每个虚拟主机更改server_name:

Here's an example of a server (vhost) config. You would create one server { } block, and change the server_name for each vhost:

server {
  listen 80;
  server_name website.com;

  location / {
    proxy_pass http://127.0.0.1:3001;
  }
  location ~* ^.+\.(jpg|png|gif|woff|ico|map|js|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|flv|swf|html|htm)$ {
    root /home/empurium/code/davinci/public;
  }
}

这篇关于在Node.js中处理虚拟主机的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆