【问题标题】:Getting Nginx to serve more than one virtual server for node apps让 Nginx 为节点应用程序提供多个虚拟服务器
【发布时间】:2016-04-27 02:27:43
【问题描述】:

所以,我现在真的很难过。我有两个 nodejs 应用程序。一个在 8080 端口上运行,一个在 8081 上运行。它们都在相同的 IP 地址上运行。我有两个域,domain1.com 和 domain2.com。我使用 Nginx 作为反向代理,将 domain1.com 重定向到端口 8080,将 domain2.com 重定向到端口 8081。我目前的问题是 domain1.com 是唯一可行的。我只能通过 domain1.com:8081 或 domain2.com:8081 访问其他节点应用程序。

我的nginx文件结构:

domain1.com.conf:

server {
    listen 80;

    server_name domain1.com www.domain1.com;

    location / {
      proxy_pass http://127.0.0.1:8080;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection 'upgrade';
      proxy_set_header Host $host;
      proxy_cache_bypass $http_upgrade;
    }
}

domain2.com.conf:

server {
    listen 80;

    server_name domain1.com www.domain1.com;

    location / {
      proxy_pass http://127.0.0.1:8081;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection 'upgrade';
      proxy_set_header Host $host;
      proxy_cache_bypass $http_upgrade;
    }
}

nginx.conf:

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

任何帮助将不胜感激。我绞尽脑汁想了这么久,在网上找不到很多相关的答案。 顺便说一句,我在 CentOS 6.3 上运行所有这些

更新:在排除更多故障后。我发现我的问题可能不是 nginx 问题,因为我完全关闭了 nginx 服务并且我的节点应用程序仍在显示。这很奇怪,因为端口 80 上没有运行任何东西。我什至使用 netstat 命令检查端口 80 上是否有任何东西在运行。我现在很困惑。如果有人对如何解决此问题或如何进一步排除故障有任何想法。请告诉我。

【问题讨论】:

  • 为什么server_name 中有两个域?
  • 正如 Alexey 所说,您在 domain2.conf 中有错字。使用server_name domain2.com www.domain2.com; 而不是server_name domain1.com www.domain2.com;
  • 是我的错字,在实际的配置文件中不是这样

标签: node.js nginx reverse-proxy


【解决方案1】:

我不是 nginx 专家,但这种设置适合我:

upstream www.domain1.com {
  server 127.0.0.1:8080;
}

server {
    listen 80;
    server_name domain1.com,www.domain1.com;
    location / {
      proxy_pass http://www.domain1.com;
    }
}

# same for domain2

【讨论】:

  • 酷,我试试这个。
  • 我将为位置块添加右括号
【解决方案2】:

我不知道它是如何或为什么起作用的,但重新启动我的服务器似乎可以解决这个问题。我仍然没有丝毫线索是什么导致了这种情况,但是我家的电源闪烁,我的服务器重新启动,并且在我必须启动的配置下一切正常。感谢任何试图提供帮助的人。我将 ShanShan 的答案标记为正确的答案,因为他/她的配置有效且工作正常。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2018-06-16
  • 2019-01-13
  • 2015-08-15
  • 2020-09-27
  • 1970-01-01
  • 2020-10-31
  • 2017-11-03
  • 1970-01-01
相关资源
最近更新 更多