W
W
WhatIsHTML2018-07-11 19:22:08
Nginx
WhatIsHTML, 2018-07-11 19:22:08

How to properly organize caching on Nginx + NodeJS?

Site on NodeJs. Nginx redirects requests to the port that NodeJs listens on - everything is simple, but probably because of this, caching cannot be enabled. Missed something.
Followed this article to configure file caching on the server. As a result, nothing is cached. Also followed these tips - did not help. After each change I restart nginx sudo systemctl restart nginx
Where is the error and how to enable caching?
Site config file:

/etc/nginx/sites-available/example.com
server {
    listen 80;
    server_name example.com;
    
    location / {
        proxy_pass http://IP_ADDRESS:PORT;
        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;
     }

}

UPDATE#1
Static files are located in the folder at
/home/example.com/public/dist
NodeJs gives the folder /home/example.com/public
UPDATE#2
in the node there is such an opportunity. Nginx not needed
app.use(express.static(__dirname + '/../public', { maxAge: '30 days' }));

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
ky0, 2018-07-11
@ky0

Add a location /publicwithout proxying, but with an indication of the directory where the files are located. If the node will also give them - just add the appropriate headers to the location - Cache-Controland so on.

location /public {
   root /home/example.com/public/;
   add_header Cache-Control "private, max-age=86400";
}

location / {
...
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question