简体   繁体   English

在Node.js中处理虚拟主机的最佳实践

[英]Best practice to handle vhosts in nodejs

What is the best practice to handle virtual hosts in node.js? 在node.js中处理虚拟主机的最佳实践是什么?

I need to route domains to each individual http server.. 我需要将域路由到每个单独的http服务器。

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

I am using express http 我正在使用快递http

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

Here's an example of a server (vhost) config. 这是服务器(虚拟主机)配置的示例。 You would create one server { } block, and change the server_name for each vhost: 您将创建一个server { }块,并为每个虚拟主机更改server_name:

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;
  }
}

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

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