繁体   English   中英

在 Ubuntu 上设置 Node.JS 服务器

[英]Setup Node.JS Server on Ubuntu

我正在使用 MEAN Stack,我已经可以运行我的前端了。 ng build之后,我将dist文件夹中的所有内容移动到var/www/html并且可以访问我的网站。 我正在使用 Apache,我的前端现在可以在线使用。 问题现在是我的后端。 我正在使用 Node.js 但我不知道如何让每个人都“在线”。 使用npm start server.js或 pm2 start pm2 start server.js我可以让我的 Node.js 服务器运行,一切正常,但它只对我有用。 当我的朋友访问我的网站时,后端没有运行(仅前端)。

所以我的问题是,我怎样才能让我的 node.js 服务器公开? 有没有办法在 Apache 中运行 node.js ?

实际上我做了一个 apache 代理,甚至使用 nginx 但似乎还没有工作......

我从这里开始执行此步骤: https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-14-04

也从这里: https://medium.com/@pierangelo1982/using-nodejs-app-with-apache-374b37c6140d

编辑:

I am able to server my backend with nginx but I have to stop my Apache server but my apache server is serving the angular frontend...

我应该怎么办?

编辑2:

感谢 Gouveia 的帮助,我能够使用 NGINX 为前端和后端服务

server {
    listen 80;

    server_name http://travelinked.vm.mi.hdm-stuttgart.de;

    location / {
        root /var/www/html;
    }

    location /api {
        proxy_pass http://141.62.65.110:3000;
        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;
    }
}

如您所见,该站点可用,但后端仍未在我的 Intranet 之外运行。 对我来说,后端正在加载,但对你们来说,我认为不是

http://travelinked.vm.mi.hdm-stuttgart.de是网站

当您运行npm start server.js时,您正在运行自己的服务器。

如果您希望通过 Apache 对 go 的所有请求,那么您可以使用 简单的反向代理从 Apache 连接到您的 nodejs 后端(假设您的节点 js 服务器在 port8008 上运行):

ProxyPass "/api"  "http://localhost:8080/"

linux 中的 Apache 配置文件通常位于: /etc/apache2/sites-available/

如果您想使用 Nginx 代替,您可以使用它提供 static 文件,同时将请求代理到 API

server {

    location / {
        # Path to your angular dist folder
        root /path/to/static/files;
    }

    location /api {
        # Address of your nodejs server
        proxy_pass http://localhost:8080/;
    }
}

linux 中的配置文件通常位于: /etc/nginx/nginx.conf

使用这两种方法,访问/api/*路径上的 Apache/Nginx 服务器将向 nodejs 服务器发出请求。

在 Nginx 情况下,路径 /api 包含在对 nodejs 服务器的请求路径中。 要删除该部分路径,您将需要一个重写规则:

location /api {
    # Address of your nodejs server
    proxy_pass http://localhost:8080/;
    # This removes the /api part in the request to the backend
    rewrite /api/(.*) /$1 break;
}

我在示例中使用了 localhost,因为我假设您在同一台机器上运行所有内容。

有关更多详细信息和配置,我建议查看链接文档。

暂无
暂无

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

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