简体   繁体   English

如何在Ngnix上为生产环境配置Node.js

[英]How to config nodejs for production enviroment on ngnix

I have to develop a chat system using angularjs and nodejs. 我必须使用angularjs和nodejs开发一个聊天系统。 To send and receive message i use socket.io. 要发送和接收消息,我使用socket.io。 For node js i create nodejs server using localhost cmd. 对于节点js,我使用localhost cmd创建nodejs服务器。

this is working fine but now i have to put this to live. 这工作正常,但现在我必须将其付诸实践。

So i do not understand how to put nodejs code on live 所以我不明白如何将Node.js代码实时发布

i have vps server where node js is working 我有节点js工作的vps服务器

This is my angularjs system url 这是我的angularjs系统网址

https://app.twodegrees.io/#/ https://app.twodegrees.io/#/

this is nodejs server link where socket is working 这是套接字工作的nodejs服务器链接

https://app.twodegrees.io:8080/socket.io/socket.io.js https://app.twodegrees.io:8080/socket.io/socket.io.js

So my question is : How i use nodejs code for production enviroment on ngnix server thanks 所以我的问题是:我如何在ngnix服务器上使用nodejs代码进行生产环境谢谢

One way of doing this is to configure nginx as a reverse proxy. 一种方法是将nginx配置为反向代理。 This will redirect requests arriving at your server to the nodeJs application running on that server. 这会将到达您服务器的请求重定向到该服务器上运行的nodeJs应用程序。

Here is an example block that you could add to your /etc/nginx/sites-available/default file, this block will redirect requests from app.twodegrees.io to port 3000 on the local machine. 这是一个示例块,您可以将其添加到/etc/nginx/sites-available/default文件中,该块会将请求从app.twodegrees.io重定向到本地计算机上的端口3000。

server {
    listen 80;

    server_name app.twodegrees.io;

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

for more detailed instructions, you could follow the post here: 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

Could you provide your directory structure? 您能提供您的目录结构吗? I think it depends on if you want do serve your frontend via nginx or nodejs ... 我认为这取决于您是否希望通过nginx或nodejs服务您的前端...

As I understand the example configuration above only works in your case if nodejs is serving the frontend. 据我了解,以上示例配置仅在nodejs服务于前端时才适用。

  1. create a new directory in your node.js backend application called public and put in your whole frontend application 在您的node.js后端应用程序中创建一个名为public的新目录,并将其放在整个前端应用程序中
  2. add app.use(express.static(__dirname + '/public')); 添加app.use(express.static(__dirname + '/public')); before you call listen in your server.js 致电之前,请在server.js listen

UPDATE: 更新:

Configuration for angular frontend 角度前端的配置

server {
    listen 80;
    server_name app.twodegrees.io;
    root /usr/share/html;
    index index.html;

    location / {
        try_files $uri $uri/ /index.html;
    }
}

Configuration for Socket.io backend Socket.io后端的配置

server {
    listen 80;

    server_name socket.twodegrees.io;

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

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

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