哪个最有效:直接由 nginx 或通过 nginx 反向代理由节点提供静态文件?
Which is most efficient : serving static files directly by nginx or by node via nginx reverse proxy?
例如,我已经将 nginx
用作 reverse proxy
来为我的 node.js
webapps 3000<->80
提供服务。实际上,我使用 express.static
中间件在节点应用程序中提供我的资产。
我读了又读,nginx 对静态文件的处理效率极高。
问题是,什么是最好的?像我已经做的那样提供资产或配置 nginx 以直接提供静态文件本身?
还是差不多?
最好的方法是使用 nginx 服务器为您提供静态文件,让您 node.js 服务器处理动态内容。
它通常是减少 node.js 服务器上请求数量的最优化解决方案,它比 nginx 服务器静态文件慢:
如果您已经为您的 nodejs 应用程序设置了反向代理,那么实现该配置非常简单。
第二个 nginx 配置可以是
root /home/myapp;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name _;
location /public/ {
alias /home/myapp/public/;
}
location / {
proxy_pass http://IPADRESSOFNODEJSSERVER:8080;
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;
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
#try_files $uri $uri/ =404;
}
在 url 的第一部分带有 /public/ 的每个请求都将由 nginx 处理,并且每个其他请求通常会在 IPADRESSOFNODEJSSERVER:NODEJSPORT
处代理给您的 nodejs 应用程序IPADRESSOFNODEJSSERVER
是本地主机
express 的文档部分告诉 http://expressjs.com/en/advanced/best-practice-performance.html#proxy
An even better option is to use a reverse proxy to serve static files;
see Use a reverse proxy for more information.
此外,nginx 可以让您轻松定义缓存规则,因此对于不会更改的静态资产,它也可以通过一行加速您的应用程序。
location /public/ {
expires 10d;
alias /home/myapp/public/;
}
你可以在网上找到很多比较这两种方法的文章,例如:
http://blog.modulus.io/supercharge-your-nodejs-applications-with-nginx
例如,我已经将 nginx
用作 reverse proxy
来为我的 node.js
webapps 3000<->80
提供服务。实际上,我使用 express.static
中间件在节点应用程序中提供我的资产。
我读了又读,nginx 对静态文件的处理效率极高。
问题是,什么是最好的?像我已经做的那样提供资产或配置 nginx 以直接提供静态文件本身?
还是差不多?
最好的方法是使用 nginx 服务器为您提供静态文件,让您 node.js 服务器处理动态内容。
它通常是减少 node.js 服务器上请求数量的最优化解决方案,它比 nginx 服务器静态文件慢:
如果您已经为您的 nodejs 应用程序设置了反向代理,那么实现该配置非常简单。
第二个 nginx 配置可以是
root /home/myapp;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name _;
location /public/ {
alias /home/myapp/public/;
}
location / {
proxy_pass http://IPADRESSOFNODEJSSERVER:8080;
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;
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
#try_files $uri $uri/ =404;
}
在 url 的第一部分带有 /public/ 的每个请求都将由 nginx 处理,并且每个其他请求通常会在 IPADRESSOFNODEJSSERVER:NODEJSPORT
处代理给您的 nodejs 应用程序IPADRESSOFNODEJSSERVER
是本地主机
express 的文档部分告诉 http://expressjs.com/en/advanced/best-practice-performance.html#proxy
An even better option is to use a reverse proxy to serve static files; see Use a reverse proxy for more information.
此外,nginx 可以让您轻松定义缓存规则,因此对于不会更改的静态资产,它也可以通过一行加速您的应用程序。
location /public/ {
expires 10d;
alias /home/myapp/public/;
}
你可以在网上找到很多比较这两种方法的文章,例如: http://blog.modulus.io/supercharge-your-nodejs-applications-with-nginx