简体   繁体   English

通过 nginx 运行多个 express 应用程序,显示端口已在使用中

[英]Running multiple express apps through nginx showing port already in use

Here's my nginx file:这是我的 nginx 文件:

server {
    server_name domain.net www.domain.net;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_pass http://localhost:3000;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/domain.net/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/domain.net/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
    if ($host = www.domain.net) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    if ($host = domain.net) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    listen 80;
    server_name domain.net www.domain.net;
    return 404; # managed by Certbot
}

server {
   listen 4000;
   server_name <I input the IP address of the server here without http or slash at the end>;    

   location / {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_pass http://localhost:4000;
   }
}

When I visit domain.net, it's showing whatever app I'm running on port 3000, however, when I run npm start in the app that has port 4000 in it, it's returning "port already in use".当我访问 domain.net 时,它显示了我在端口 3000 上运行的任何应用程序,但是,当我在具有端口 4000 的应用程序中运行npm start时,它返回“端口已在使用中”。 I don't know why this is happening.我不知道为什么会这样。

This is happening because the NGINX process as well as the Node process trying to bind to port 4000. If you start NGINX before your node process node will fail to start with this error message or vise versa.发生这种情况是因为 NGINX 进程以及试图绑定到端口 4000 的节点进程。如果您在节点进程之前启动 NGINX,则节点将无法启动此错误消息,反之亦然。

If you want to proxy your nodeJS app with NGINX and they are running on the same server you have to use two different ports.如果你想用 NGINX 代理你的 nodeJS 应用程序并且它们在同一台服务器上运行,你必须使用两个不同的端口。

You can not have two different processes listening on the same port at the same time... how do you think the request will find the right service?您不能让两个不同的进程同时在同一个端口上侦听...您认为请求如何找到正确的服务?

I ended up doing:我最终做了:

server {
   listen <server-ip>:80;

   location / {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_pass http://localhost:4000;
   }
}

And running my app on the main IP port (80).并在主 IP 端口 (80) 上运行我的应用程序。 If anyone knows a solution to my original question, I would really appreciate it.如果有人知道我原来的问题的解决方案,我将不胜感激。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM