[英]How to set up two node JS applications on my nginx server
I have a node js application running successfully on app.example.com on port 4000. Now I want to run another node js application on www.example.com on port 5010. How would I do it? 我有一个节点js应用程序在端口4000上的app.example.com上成功运行。现在我想在端口5010的www.example.com上运行另一个节点js应用程序。我该怎么做?
My attempt. 我的尝试。 Create two files in sites-available folder. 在站点可用文件夹中创建两个文件。 one is www.example.com and one is app.example.com content of the files. 一种是www.example.com,另一种是app.example.com文件的内容。
app.example.com app.example.com
server {
listen 80;
server_name app.example.com;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header HOST $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:4000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_redirect off;
}
}
If I have this one alone. 如果我独自一人。 It works. 有用。 Now adding 现在添加
www.example.com www.example.com
server {
listen 80;
server_name www.example.com;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header HOST $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:5010;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_redirect off;
}
}
And both node apps are running on running on forever on respective port. 并且两个节点应用程序都在各自端口上永久运行。 Now it doesn't matter what url I give ie app.example.com or www.example.com It gives the same application ie Hello World. 现在我提供的URL都没有关系,即app.example.com或www.example.com,它给出的是相同的应用程序,即Hello World。 How do I achieve having two different applications on two different ports? 如何在两个不同的端口上拥有两个不同的应用程序?
EDIT It's happening if I give www.example.com:5010, but I want it without having to type 5010. How would I achieve that? 编辑如果我输入www.example.com:5010,就会发生这种情况,但是我不需要输入5010就可以了。我将如何实现?
Here is something that is not given anywhere. 这是任何地方都没有的东西。 You have to link your sites-enabled files into your ngnix.conf. 您必须将启用站点的文件链接到ngnix.conf中。 Your sites-enabled files ie www.domain.com, app1.domain.com app2.domain.com will not work . 您启用了网站的文件,即www.domain.com,app1.domain.com app2.domain.com将不起作用。 Unless you include them in nginx.conf. 除非您将它们包括在nginx.conf中。 Something like this. 这样的事情。
include /etc/nginx/sites-enabled/*.conf;
include /etc/nginx/sites-enabled/app.example.com;
include /etc/nginx/sites-enabled/www.example.com;
and your app.example.com and www.example.com sites-available file will look like that posted in question. 并且您的app.example.com和www.example.com网站可用文件看起来像是有问题的文件。 At last run your node apps from the port mentioned in your node apps. 最后,从节点应用程序中提到的端口运行节点应用程序。 Don't forget to have same port numbers in your app.domain.com and www.domain.com otherwise you will get a 502 Bad gateway error. 不要忘记在app.domain.com和www.domain.com中具有相同的端口号,否则您将收到502 Bad gateway错误。 after that sudo service nginx restart And you are good to go. 在该sudo服务nginx重新启动之后,您就可以开始了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.