有什么更好的方法:通过Express或nginx提供静态文件? [英] What's the better approach: serving static files with Express or 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, mainly because of flexibility it provides... you can change your static location and structure very easily during development
用于生产: nginx ,因为它的速度要快得多.节点/表达式对于执行逻辑很有用,但是对于提供原始内容……没有什么可以击败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?
这篇关于有什么更好的方法:通过Express或nginx提供静态文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!