0

We have a Node.js/Express application that is deployed as Docker containers into AWS ECS. This app has a few static resources (CSS, JS), and I would like requests for these assets to bypass Node.js and be served directly by Nginx for performance reasons.

Currently, we have docker-compose starting an Nginx container, and Nginx is configured with a proxy_pass rule for proxying and caching static resources from the Node container like this:

  location /static {
    proxy_cache STATIC;
    proxy_pass http://nodejs;
  }

The problem with this approach is that the first request for any static asset is still served by Node.js, and only then cached by Nginx.

I would like to by-pass Node.js entirely for these assets.

a) What is a better approach to achieve this goal?

b) I thought of using volumes, but in the context of ECS, if we do this and have multiple containers all deployed to the same AWS host (e.g. multiple copies of the Node, Nginx containers running in same EC2 instance), will this cause problems since they're all trying to mount the same directory in the filesystem?

Any guidance will be greatly appreciated, since we haven't been able to find any best-practice approach on how to handle this situation. Thanks.

  • have you understand how proxy Pass works? one request must be done to cache the Things and as Long the cache is valid it will be served from nginx, in case of not changing files you may host These files directly i. e. on the nginx host or a seperaten instances – djdomi Aug 4 '19 at 8:05
1

In case it's helpful to anyone else, here's the solution we landed on for now...

Configure docker-compose.yml with a sidecar Nginx container with a shared volume for static assets, like so:

version: '3'
 services:
  nodejs:
   build: ./
   volumes:
    - asset-volume:/opt/nodejs_app/static
  nginx:
   build: ./nginx
   ports:
    - 80:80
   volumes:
    - asset-volume:/var/lib/assets:ro

volumes:
 asset-volume:

Then, configure Nginx default.conf to alias requests to /static to this volume:

  location /static {
    alias /var/lib/assets;
    try_files $uri $uri/ =404;
  }

This avoids having to do a proxy_pass to Node.js for retrieving static assets, which can now be served directly by Nginx.

Any feedback on this implementation? Is there a better way to do this?

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.