简体   繁体   English

如何在我的Nginx服务器上设置两个节点JS应用程序

[英]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.

相关问题 如何使用Nginx和Node.js应用程序设置Jenkins - How to set up Jenkins with Nginx and a Node.js application Node.js - 如何在我的本地计算机上设置服务器并允许某人访问它(用于测试) - Node.js - How to set up a server on my local machine and allow someone to access it(for testing) 如何设置工作网络上其他人可以使用node.js访问的服务器? - How to set up a server that others on my work network can access using node.js? 如何在Node.js中设置两个意图不同的工作程序? - How to set up two workers in Node.js with different intentions? node.js和nginx代理设置 - 出了点问题,但是什么? - node.js & nginx proxy set up — something is wrong, but what? 尝试将 Traefik 设置为 node.js 和 nginx 的反向代理 - Trying to set up Traefik as a reverse proxy for node.js and nginx 如何设置我的 nginx 文件以连接到我的服务器? - How do I set up my nginx files to connect to my server? Nginx / Node.JS-仅在节点服务器完全启动后启动Nginx - Nginx/Node.JS - start Nginx only after node server is fully up 如何在bitnami MEAN堆栈服务器上托管两个节点应用程序? - How to host two node applications on bitnami MEAN stack server? 如何使用多个节点应用程序设置 nginx 反向代理 - How to set up nginx reverse proxy with multiple node apps
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM