什么是更好的方法:使用 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 本身或用于服务的 nginx 前端静态文件? 或
- 仅使用 Node.js 与使用 Node.js 和 Apache/Nginx 或
- 哪个最有效:直接由 nginx 提供静态文件,还是通过 nginx 反向代理由节点提供服务?
- 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?