【问题标题】:Nginx and multiple domainsNginx 和多个域
【发布时间】:2014-05-25 05:20:35
【问题描述】:

我在godaddy.com(即mydomain.com)为我的Droplet 在digitalocean.com(即199.216.110.210)购买了一些域名。我在 droplet 的 80 端口上运行 nodejs 应用程序。从godaddy.com,我通过屏蔽mydomain.com 转发到199.216.110.210,我可以看到可能的应用程序。

现在我想在 199.216.110.210 上运行不同端口上的几个节点应用程序,使用 ngnix 作为反向代理。我按照这里的说明进行操作 (www.digitalocean.com/community/articles/how-to-host-multiple-node-js-applications-on-a-single-vps-with-nginx-forever-and-crontab)。

我的 nginx .conf 文件是

 server {
    listen 80;
    server_name mydomain.com;
    location / {
    proxy_pass http://localhost:3000;
    # same as in the link above
    }   
}

(并且我确信它已被读取:当 ngnix 启动时,如果我在那里输入错误,ngnix 会报告它)。

我在 3000 端口启动 nodejs 应用程序:

  • 我尝试mydomain.com,但 ngnix 始终显示欢迎页面。
  • 同样做mydomain.com: 3000也不行,
  • 它只适用于199.216.110.210:3000

从 godaddy.com,如果我将mydomain.com 转发到199.216.110.210:3000,我可以看到可能的应用程序。 但我不喜欢这个解决方案。我希望域指向我的 droplet,而不指定端口并使用 nginx 管理它们。

我如何获得一个域名以使用 ngnix 作为反向代理来选择我的应用程序,映射到不同端口上的不同域?我想从 godaddy.com 的转发在某种程度上是有限的。

【问题讨论】:

  • 这和node.js有什么关系?
  • 你是在浏览器中做 www.mydomain.com 而不是简单地 mydomain.com 吗?

标签: node.js nginx


【解决方案1】:

在您的服务器中转到/var/log/nginx 并执行tail -F *log。现在在另一个 shell 中重新启动 nginx。

我怀疑你的域名太长,nginx会抱怨它的hash_bucket_size太小。如果是这种情况,请打开 /etc/nginx/nginx.conf 并确保该行

server_names_hash_bucket_size 64;

存在,值为 64 且未注释。然后执行sudo service nginx reload,并检查是否一切正常。

【讨论】:

  • 我取消了它的注释并且重定向工作;如果我将端口 4000 重定向到在端口 3000 上运行的 nodejs 应用程序,它就可以工作。但是在 80 端口上,我从日志中得到了消息:conflicting server name "day2log.com" on 0.0.0.0:80, ignored
  • 您的配置中有两个条目在端口 80 上监听 day2log.com。
  • 是的,你是对的;我在 ngnix.conf #include /etc/nginx/sites-enabled/*; 中发表了评论,它适用于端口 80
【解决方案2】:

我将逐步详细说明如何在我的 aws ec2 实例中做到这一点;

  1. 我为我的实例设置了 DNS 记录,因此我可以将 mydomain.com 设置为 192.168.123.123(我的特定 IP)。

  2. 在我的实例中,我一直在端口 3000 中运行我的 node.js 应用程序(我通过从命令行发出 curl localhost:3000 来测试它是否工作)

  3. 然后我下载this .sh文件以正确实例化nginx; curl -o nginxStarter.sh https://gist.githubusercontent.com/renatoargh/dda1fbc854f7957ec7b3/raw/c0bc1a1ec76e50cdb4336182c53a0b222edb6c0e/start.sh

  4. 我用this configuration file配置了nginx。把这个文件放进去; /etc/nginx/nginx.conf

  5. 用这个命令启动nginx; sudo sh nginxStarter.sh start

PS.:对于多个应用程序,只需复制将请求路由到特定端口的行,非常简单...!如果不需要,您可以删除关于 SSL 的行。

【讨论】:

  • 我试过这个,但原来的 nginxStarter.sh 不能在 ubuntu 上工作。我解决了它改变功能和网络路径,但我仍然得到这个错误:grep: unrecognized option '--with-cc-opt='-g' and $Starting nginx: nginxStarter.sh: 54: nginxStarter.sh: daemon : 没找到
  • 事实上我在 Amazon Linux (Cent OS) 上使用它! ://
猜你喜欢
  • 2017-03-31
  • 2017-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-06
  • 2018-05-21
  • 2014-10-13
  • 2023-03-25
相关资源
最近更新 更多