什么是更好的方法:使用 Express 或 nginx 提供静态文件? [英] What's the better approach: serving static files with Express or nginx?

查看:39
本文介绍了什么是更好的方法:使用 Express 或 nginx 提供静态文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个 Node.js 应用程序,并使用 nginx 作为反向代理.我的应用程序有一些我需要提供的静态文件和一个 Socket.io 服务器.

I'm building a Node.js applications and I'm using nginx as a reverse proxy. My application has some static files I need to serve and a Socket.io server.

我知道我可以直接使用 Express 提供静态文件(使用 express.static 中间件).我也可以将 nginx 直接指向我的静态文件所在的目录,这样它们就会由 nginx 提供服务.

I know that I can serve static files directly with Express (using express.static middleware). Also I can point nginx directly to the directory where my static files are located, so they would be served by nginx.

那么,问题是:哪种方法更好?使用每种方法时我会面临哪些利弊?

So, the question: which one is the better approach? Which pros and cons can I face while using each approach?

推荐答案

for development: express,主要是因为它提供了灵活性...你可以在开发过程中很容易地改变你的静态位置和结构

for development: express, mainly because of flexibility it provides... you can change your static location and structure very easily during development

用于生产:nginx,因为它要快得多.Node/express 很适合执行逻辑,但对于提供原始内容......没有什么能打败 nginx.您还可以获得额外的功能,例如 gzip、负载平衡...

for production: nginx, because its much much faster. Node/express are good for executing logic, but for serving raw content... nothing can beat nginx. You also get additional capabilities such as gzip, load balancing...

尽管如此,这个问题已经在stackoverflow中被问过很多次了:见

Nevertheless, this question has been asked in stackoverflow a number of times already: see

  • node.js itself or nginx frontend for serving static files? or
  • Using Node.js only vs. using Node.js with Apache/Nginx or
  • Which is most efficient : serving static files directly by nginx or by node via nginx reverse proxy?

这篇关于什么是更好的方法:使用 Express 或 nginx 提供静态文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆