Best practice to handle vhosts in nodejs

爷,独闯天下 提交于 2019-12-10 12:19:51

问题


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

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

I am using express http


回答1:


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.

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


来源:https://stackoverflow.com/questions/22364524/best-practice-to-handle-vhosts-in-nodejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!