【问题标题】:Setup Node.JS Server on Ubuntu在 Ubuntu 上设置 Node.JS 服务器
【发布时间】:2021-08-05 14:38:09
【问题描述】:

我正在使用 MEAN Stack,我已经可以运行我的前端了。在ng build 之后,我将dist 文件夹中的所有内容移动到var/www/html 并且可以访问我的网站。我正在使用 Apache,我的前端现在可以在线使用。问题现在是我的后端。我正在使用 Node.js,但我不知道如何让每个人都“在线”。使用npm start server.jspm2 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

编辑:

我可以使用 nginx 为我的后端提供服务,但我必须停止我的 Apache 服务器,但我的 apache 服务器正在为 angular 前端提供服务...

我该怎么办?

EDIT2:

感谢 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是网站

【问题讨论】:

    标签: node.js linux apache nginx server


    【解决方案1】:

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

    如果您希望所有请求都通过 Apache,那么您可以使用 simple reverse proxy 从 Apache 连接到您的 nodejs 后端(假设您的 node js 服务器在端口 8080 上运行):

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

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

    如果你想改用 Nginx,你可以 serve your static files 使用它,而 proxying the requests to the 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,因为我假设您在同一台机器上运行所有内容。

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

    【讨论】:

    • 嘿,谢谢!我编辑了我的帖子!即使使用 NGINX,后端仍然只是在本地运行,我还缺少什么吗?
    • 您的前端代码似乎指向后端的 IP 地址 (141.62.65.110:3000/gettoptours) 而不是 http://travelinked.vm.mi.hdm-stuttgart.de/api/gettoptours
    • 嘿,又是我,由于某种原因,当我访问 http://travelinked.vm.mi.hdm-stuttgart.de/api 时,它无法获得 /api Hello World 应该在那里,但我却获得了 Cannot GET /api
    • 目前我不确定,但您可能需要更改节点 js 代码中的路由路径以包含 /api 或更改 nginx 配置以重写路径。见serverfault.com/questions/379675/…根据答案,你的情况是这样的:rewrite /api/(.*) /$1 break;
    • 重写路径确实有帮助!现在它终于奏效了!非常感谢。
    猜你喜欢
    • 2018-07-26
    • 2021-09-06
    • 1970-01-01
    • 2012-01-14
    • 1970-01-01
    • 1970-01-01
    • 2013-06-11
    • 1970-01-01
    • 2015-04-27
    相关资源
    最近更新 更多