Bharat D Bhadresha
Bharat D Bhadresha

Reputation: 779

Prevent Nginx from caching nodejs responses

I am using Nginx to proxy requests to server based on directory user want to access

server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;


server_name localhost;

location / {

    proxy_pass http://****.***/;

}
location /app/{
    proxy_no_cache '1';
    proxy_cache_bypass  '1';

    proxy_buffering off;

    include proxy_params;
    proxy_pass http://localhost:3000/;

}
}

This is nginx configuration. A Node app is running on 3000 port Problem I am facing is

example of Node app

.....
app.get("/",function(req,res){
***Sends login page or home page based on session***
});
app.get("/processLogin",function(req,res){
***redirects to / after setting session****
});
.....

Upvotes: 1

Views: 2358

Answers (3)

Pierre Ghislain
Pierre Ghislain

Reputation: 362

One question: do you use pm2 for your Node application ? Many use Nginx + pm2. For the development phase of pm2, you need to put the --watch flag while launching your App. pm2 load all the Node.js in memory and dont check file change on the hardisk. You have then a cache phenomenon.

So, during the development phase, in place of

pm2 start MyApp.js

do

pm2 start MyApp.js --watch

Honestly, I dont see how a browser cache or Nginx can cache variable responses given by node.js programs. It had to be the pm2, in my case.

Upvotes: 0

Bharat D Bhadresha
Bharat D Bhadresha

Reputation: 779

Adding no-cache headers in nodejs helped to solve the problem.

Upvotes: 0

Kevin Sandow
Kevin Sandow

Reputation: 4033

In proxy mode nginx is using Expires header to reduce load on the backend server...

So simply set expires off; in the proxy location block and caching should be gone.


In case the caching occurs in the browser, you'll need to set the cache control header to no-cache:

add_header Cache-Control no-cache;

Upvotes: 3

Related Questions