简体   繁体   English

Nginx Linux服务器上的Node.js Express应用程序

[英]Nodejs express application on nginx linux server

I have created my first nodejs application with nodejs express. 我已经用nodejs express创建了我的第一个nodejs应用程序。 The server is 服务器是

var express = require("express");
global.app = express();
require("src/app-modules/common/yamaha/yamaha.controller.api");
app.listen(8080, function() {
console.log("Listening on http://127.0.0.1:8080");
});

On windows development machine Is working. 在Windows开发机上正在工作。 Now I try to publish this app on a Synology NAS. 现在,我尝试在Synology NAS上发布此应用。 I access this application on url: //192.168.1.151/YamahaCtrl and I have a 404 error. 我通过url访问此应用程序://192.168.1.151/YamahaCtrl,出现404错误。 Update 2: I discover what is realy the problem: how to configure the nginx server fromn Routes are defined on yamaha.controller.api like this one: 更新2:我发现真正的问题是:如何在yamaha.controller.api上定义路由,如下所示:

    app.get("/api/yamaha/getBasicInfo", function (req, res) {
        //do something
    });

I found a nodejs 8 beta package to install on NAS, and now I have nodejs version 8. But the error still remain. 我找到了要在NAS上安装的nodejs 8 beta程序包,现在我有nodejs版本8。但是错误仍然存​​在。

So the nodejs app server is open on localhost:8080, but url access is http://192.168.1.151/YamahaCtrl , on 80. 因此,nodejs应用服务器在localhost:8080上打开,但URL访问是http://192.168.1.151/YamahaCtrl ,在80上。

That mean the problem is how to configure Virtual Host on NAS, and which port on node server I should use. 这意味着问题在于如何在NAS上配置虚拟主机,以及应该在节点服务器上使用哪个端口。

Update: The problem is: need to configure a redirect port on nginx server installed on Synology. 更新:问题是:需要在Synology上安装的Nginx服务器上配置重定向端口。 I found this article: https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-14-04 我找到了这篇文章: https : //www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-14-04

and I create configuration file /etc/nginx/sites-available/default: 然后创建配置文件/ etc / nginx / sites-available / default:

server {
listen 80;

server_name _;

location / {
    proxy_pass http://127.0.0.1:8080;
    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;
}

} }

And still not working: the html files are accessible , but api is available on port 8080 only. 仍然无法正常工作:可访问html文件,但api仅在端口8080上可用。

I figured out what the problem was: nodejs server is serving static files too, but my config for static files was wrong. 我发现了问题所在:nodejs服务器也正在提供静态文件,但是我的静态文件配置错误。 For development was working, and that why I didn't know that is wrong. 对于开发而言,这是行得通的,这就是为什么我不知道那是错误的。

So it's no need to configure something on NAS :). 因此,无需在NAS上进行任何配置:)。 To start nodejs application is enough. 要启动nodejs应用程序就足够了。

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

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