1

I'm using nginx as a load balancer. here is the simplified version of my nginx config file.

upstream myUpstream {
   server server1.com;
   server server2.com;
}

server{
    location / {
        proxy_pass https://myUpstream;
    }
}

to leverage browser caching for my rest api calls I set my backend servers to set cache headers which expires in 1 hour.

when I send my requests directly to server1.com or server2.com everything works perfectly and cache response headers are fine.

enter image description here

but when I send my requests to my load balancer server I get two cache response headers one from load balancer and another one from my backend server and browser dont cache my response.

enter image description here

my problem is how to configure my load balancer server so it wont put its own cache headers to my rest api responses?

here is my full nginx config file:

limit_req_zone $binary_remote_addr zone=mylimit:100m rate=10r/s;

upstream myUpstream {
       ip_hash;
       server server1.com weight=1;
       server server2.com weight=1;
}


server {
    #---- Server Domain neme
    server_name www.server.com;

    keepalive_requests 100;
    keepalive_timeout 20s;


    listen 443 ssl http2 ;
    listen [::]:443 ssl http2 ;

    # ssl config
    ssl_certificate path.pem;
    ssl_certificate_key privateKeyPath.pem;

    ssl_session_cache   shared:SSL:10m;
    ssl_session_timeout 5m;
    ssl_ciphers HIGH:SALT;
    ssl_prefer_server_ciphers   on;

    location / {
        limit_req zone=mylimit burst=10 nodelay;

        proxy_pass https://myUpstream;
        proxy_set_header  X-Scheme $scheme;

        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

server {
    if ($host = server.com) {
        return 301 https://$host$request_uri;
    }
    listen         80;
    server_name server.com;
    return         301 https://$server_name$request_uri;
}

server {
    if ($host = www.server.com) {
        return 301 https://$host$request_uri;
    }
    listen         80;
    server_name www.server.com;
    return         301 https://$server_name$request_uri;
}

server {
    listen 443 ssl http2 ;
    listen [::]:443 ssl http2 ;
    ssl_certificate path.pem;
    ssl_certificate_key privatekeyPath.pem;

   server_name server.com;
   return         301 https://www.$server_name$request_uri;

}
Kibo
  • 111
  • 5

0 Answers0