Seltsam
Seltsam

Reputation: 944

Nginx Cache whole Node/Express Application

Jolly goody Morning! I'm trying to fully cache a Node/Express Application. The Site rarely changes, so I'd love to have a pretty string caching mechanism.

How would the Setting look to completely put the application (html, css, img, ...) into the Nginx cache (public Cache) for 5min, so the Page gets served "statically" form the Nginx Cache while the Express-App baiscally idles?

I feel this isn't the right setup here:

proxy_cache_path /cache levels=1:2 keys_zone=my_cache:10m max_size=10g
                 inactive=60m use_temp_path=off;
server {
        listen 80 default_server;
        listen [::]:80 default_server;

        location ^~ / {
                proxy_cache my_cache;
                proxy_pass http://localhost:3030;
                }
}

Upvotes: 1

Views: 1877

Answers (1)

Stef
Stef

Reputation: 85

proxy_cache_path /cache levels=1:2 keys_zone=my_cache:100m max_size=10g inactive=5m use_temp_path=off;
server {
        listen 80 default_server;
        listen [::]:80 default_server;

        location ^~ / {
                proxy_cache my_cache;
                proxy_pass http://localhost:3030;                
                # cache all requests of any type for 5m
                proxy_cache_methods HEAD GET POST;
                proxy_cache_valid any 5m;
                expires 5m;
                # default proxy cache key can be adjusted
                proxy_cache_key $scheme$proxy_host$request_uri;
                # set header - Host & Forwarded-For
                proxy_set_header Host $host;
                proxy_set_header Forwarded-For $proxy_add_x_forwarded_for;
                # cache on first use
                proxy_cache_min_uses 0;
                }
}

Module ngx_http_proxy_module

  • expire in 5 minutes
  • all content is valid for 5 minutes
  • clear inactive cache after 5 minutes

Keep up to date with nginx news

Upvotes: 1

Related Questions