简体   繁体   English

Nginx(提供HTML)和Node.js设置

[英]Nginx (serving html) and Node.js setup

I was thinking of building a web app which would be single page, using only javascript to update the page (no reloads). 我正在考虑构建仅使用JavaScript即可更新页面的Web应用程序,该应用程序将是单个页面(无需重新加载)。 So I'm confused about what kind of setup to use. 所以我对使用哪种设置感到困惑。

I'm kinda new to both technologies, so I was wondering if you can set up nginx to serve html (+ js, css and other static resources) like a normal web server and then from those pages connect to a node.js websocket server (same host/ip) using something like socket.io? 我对这两种技术都比较陌生,所以我想知道是否可以设置nginx像普通的Web服务器一样提供html(+ js,css和其他静态资源)的服务,然后从这些页面连接到node.js websocket服务器(相同的主机/ ip)使用socket.io之类的内容?

Is this setup good or bad? 这个设置好不好? What would be a better approach? 有什么更好的方法? What advantage would I get if I serve html pages in node.js and get static resources (css, js, images, ...) from nginx? 如果我在node.js中提供html页面并从nginx获取静态资源(css,js,图像等),我将获得什么优势?

I dont think serving few images and static htmls from nodejs itself will ever be a bottleneck , ideally a front end proxy like nginx is required if you need to load balance between multiple servers and also for exposing you internal http services as https traffic. 我认为从nodejs本身提供少量图像和静态html不会成为瓶颈,理想情况下,如果您需要在多个服务器之间进行负载平衡并且还需要将内部http服务作为https流量公开,则需要像nginx这样的前端代理。 If you dont have that requirement it would be a overkill imho. 如果您没有该要求,那将是一个过大的恕我直言。

From various comments in the currently accepted answer, I wanted to note the following. 从当前接受的答案中的各种评论中,我要注意以下几点。

NodeJS itself does a pretty decent job of delivering static content, as good as nginx in many cases. NodeJS本身在交付静态内容方面做得相当不错,在许多情况下,它的效果与nginx一样。

Trying to proxy a WebSocket connection is problematic at best currently, as most proxy code simply doesn't support it. 目前最多只能尝试代理WebSocket连接,因为大多数代理代码根本不支持它。 Currently, it is best to use Node directly. 当前,最好直接使用Node。

If/When you have a need to deliver static content separately, best to use another domain and a CDN at that point. 如果/当您需要分别提供静态内容时,最好在此时使用另一个域和一个CDN。

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

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