哪个最有效:通过nginx或通过nginx反向代理直接提供静态文件? [英] Which is most efficient : serving static files directly by nginx or by node via nginx reverse proxy?

查看:549
本文介绍了哪个最有效:通过nginx或通过nginx反向代理直接提供静态文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 nginx 作为 reverse proxy 来提供我的 node.js webapps 3000> 80 例如。实际上,我使用 express.static 中间件在节点应用程序中提供资产。

I already use nginx as reverse proxy to serve my node.js webapps 3000<->80 for example. Actually, I serve my assets in the node app, using express.static middleware.

我再次阅读并阅读nginx是非常有效的服务静态文件。

I read and read again that nginx is extremely efficient to serve static files.

问题是,最好的是什么?正如我已经做过的那样或者配置nginx来直接为静态文件提供资源?

The question is, what is the best ? Serving assets as I already do or configuring nginx to serve the static files itself directly ?

或者几乎相同?

推荐答案

最好的方法是使用nginx服务器为您提供静态文件,让您的node.js服务器处理动态内容。

The best way is to use nginx server to serve you static file and let you node.js server handle the dynamic content.

通常,最优化的解决方案可以减少您的node.js服务器上比nginx更慢的服务器静态文件的请求数量,例如:

It is usually the most optimized solution to reduce the amount of requests on your node.js server that is slower to server static files than nginx for example :

如果您已经为nodejs应用程序设置了一个反向代理,那么配置将非常简单。

The configuration to achieve that is very easy if you already set a reverse proxy for you nodejs app.

nd nginx配置可能是

nd nginx configuration could be

   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;
    }

每个请求与/ public /在url的第一部分将被处理通过nginx和所有其他请求将被代理到您的 IPADRESSOFNODEJSSERVER节点js应用程序:NODEJSPORT 通常 IPADRESSOFNODEJSSERVER 是本地主机

Every request with /public/ at the first part of the url will be handled by nginx and every other request will be proxied to you nodejs app at your IPADRESSOFNODEJSSERVER:NODEJSPORT usually the IPADRESSOFNODEJSSERVER is the localhost

该表达式的文档部分表示 http://expressjs.com/en/advanced/best-practice-performance.html#proxy

The doc section of express tell that 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将让您轻松定义缓存规则所以对于不改变的静态资产,可以通过一行加快你的应用程序。

Moreover nginx will let you easily define caching rules so for static assets that doesn't change it can speed up your app also with one line.

location /public/ {
            expires 10d;
            alias /home/myapp/public/;
        }

您可以找到很多文章比较互联网上的两种方法,例如:
http://blog.modulus.io/supercharge- your-nodejs-applications-with-nginx

You can find a lot of articles that compare the both methods on internet for example: http://blog.modulus.io/supercharge-your-nodejs-applications-with-nginx

这篇关于哪个最有效:通过nginx或通过nginx反向代理直接提供静态文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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