What is most efficient: serving static files directly to nginx or node via nginx reverse proxy?

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

I read and read again that nginx is extremely efficient for serving static files.

The question is which is better? Asset maintenance, as I already did, or setting up nginx to directly serve static files themselves?

Or is it almost the same?

+7
reverse-proxy nginx express
source share
1 answer

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

This is usually the most optimized solution to reduce the number of requests on your node.js server, which is slower than the static server files than nginx, for example:

The configuration to achieve this goal is very simple if you have already installed the reverse proxy for the nodejs application.

Configuration

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

Each request from / public / in the first part of the URL will be processed by nginx, and each other request will be proxied to you by the IPADRESSOFNODEJSSERVER:NODEJSPORT application in your IPADRESSOFNODEJSSERVER:NODEJSPORT , usually IPADRESSOFNODEJSSERVER is localhost

The expression doc section says 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 Using reverse proxy for more information.

In addition, nginx will allow you to easily define caching rules, so for static assets that do not change, this can speed up your application with one line.

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

You can find many articles that compare both methods on the Internet, for example: http://blog.modulus.io/supercharge-your-nodejs-applications-with-nginx

+11
source share

All Articles