# Cache some API requests in Nginx

**URL:** <https://www.ruby-forum.com/t/cache-some-api-requests-in-nginx/243911>\
**Category:** NGINX\
**Created:** [January 25, 2016, 7:53am UTC](https://www.ruby-forum.com/t/cache-some-api-requests-in-nginx/243911 "2016-01-25T07:53:06Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sadaf\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@Sadaf\_N](https://www.ruby-forum.com/u/Sadaf_N)\
**Post date:** [January 25, 2016, 7:53am UTC](https://www.ruby-forum.com/t/cache-some-api-requests-in-nginx/243911/1 "2016-01-25T07:53:06Z")

</div>

I’m seeking advise from experts here.

We have the following scenario. We have a java application. Java app is  
running on tomcat7. tomcat7 acting as API server. User interface files (  
Static html and css ) are served by nginx. Nginx is acting as reverse  
proxy  
here. All API request are passed to API server and rest are being server  
by  
nginx directly.

What we want is to implement cache mechanism here. That is means we want  
to  
enable cache for all but with few exception. We want to exclude some API  
requests from being cached.

Our configuration is like as shown below

server {

listen 443 ssl;  
server\_name ~^(?.+).ourdomain.com$;  
add\_header X-Frame-Options “SAMEORIGIN”;  
add\_header X-XSS-Protection “1; mode=block”;

if ($request\_method !~ ^(GET|HEAD|POST)$ )  
{  
return 405;  
}

open\_file\_cache max=1000 inactive=20s;  
open\_file\_cache\_valid 30s;  
open\_file\_cache\_min\_uses 2;  
open\_file\_cache\_errors on;

location / {

root /var/www/html/userUI;

location ~\* .(?:css|js)$ {  
expires 1M;  
access\_log off;  
add\_header Pragma public;  
add\_header Cache-Control “public, must-revalidate, proxy-revalidate”;

}

location ~\*  
.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$  
{  
expires 1M;  
access\_log off;  
add\_header Pragma public;  
add\_header Cache-Control “public, must-revalidate, proxy-revalidate”;

}

}

location /server {

proxy\_pass [http://upstream/server](http://upstream/server);  
proxy\_set\_header Host $[subdomain.ourdomain.com](http://subdomain.ourdomain.com);  
proxy\_connect\_timeout 600;  
proxy\_send\_timeout 600;  
proxy\_read\_timeout 600;  
send\_timeout 600;  
proxy\_buffer\_size 4k;  
proxy\_buffers 4 32k;  
proxy\_busy\_buffers\_size 64k;  
proxy\_temp\_file\_write\_size 64k;  
proxy\_temp\_path /var/nginx/proxy\_temp;  
proxy\_next\_upstream error timeout invalid\_header http\_500 http\_502  
http\_503  
http\_504;  
proxy\_set\_header X-Forwarded-Host $host;  
proxy\_set\_header X-Forwarded-For $proxy\_add\_x\_forwarded\_for;  
proxy\_set\_header X-Forwarded-Server $host;  
proxy\_set\_header X-Real-IP $remote\_addr;  
proxy\_set\_header X-Forwarded-Proto https;  
proxy\_http\_version 1.1;  
proxy\_set\_header Upgrade $http\_upgrade;  
proxy\_set\_header Connection “upgrade”;  
proxy\_set\_header Host $host;  
proxy\_redirect off;  
proxy\_cache sd6;  
add\_header X-Proxy-Cache $upstream\_cache\_status;  
proxy\_cache\_bypass $http\_cache\_control;

}

ssl on;  
ssl\_certificate /etc/nginx/ssl/ourdomain.com.bundle.crt;  
ssl\_certificate\_key /etc/nginx/ssl/ourdomain.com.key;  
ssl\_protocols TLSv1 TLSv1.1 TLSv1.2;  
#ssl\_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;  
ssl\_ciphers “EECDH+ECDSA+AESGCM EECDH+aRSA+AESGCM EECDH+ECDSA+SHA384  
EECDH+ECDSA+SHA256 EECDH+aRSA+SHA384 EECDH+aRSA+SHA256 EECDH+aRSA+RC4  
EECDH  
EDH+aRSA HIGH !RC4 !aNULL !eNULL !LOW !3DES !MD5 !EXP !PSK !SRP !DSS”;  
ssl\_dhparam /etc/nginx/ssl/dhparams.pem;  
ssl\_session\_cache builtin:1000 shared:SSL:10m;  
ssl\_prefer\_server\_ciphers on;  
ssl\_session\_timeout 24h;  
keepalive\_timeout 300;

As above, we use cache only for static files located in  
/var/www/html/userUI

We want to implement as such in location /server. This our api server.  
Means  
nginx passes api request to tomcat7 ( upstream ) server. We want to  
enable  
cache for specific API requests only but need to disable cache for rest  
of  
all requests.

We want to do the following

Exclude all json requests from cache and but need to enable cache for  
few.

Request url will be something like as shown below

Request  
URL:[https://ourdomain.com/server/user/api/v7/userProfileImage/get?loginName=user1&\_=1453442399073](https://ourdomain.com/server/user/api/v7/userProfileImage/get?loginName=user1&_=1453442399073)

What this url does is to get the Profile image. We want to enable cache  
for  
this specific url. So condition we would like to use is , if request url  
contains “/userProfileImage/get” we want to set cache and all other  
requests  
shouldn’t cache.

All our api request goes through  
[https://ourdomain.com/server/user/api/v7/](https://ourdomain.com/server/user/api/v7/)  
…

To achieve this we changed the settings to following

location /server {

set $no\_cache 0;

if ($request\_uri ~\* “/server/user/api/v7/userProfileImage/get\*”)

{  
set $no\_cache 1;  
}

proxy\_pass [http://upstream/server](http://upstream/server);  
proxy\_set\_header Host $[subdomain.ourdomain.com](http://subdomain.ourdomain.com);  
proxy\_connect\_timeout 600;  
proxy\_send\_timeout 600;  
proxy\_read\_timeout 600;  
send\_timeout 600;  
proxy\_buffer\_size 4k;  
proxy\_buffers 4 32k;  
proxy\_busy\_buffers\_size 64k;  
proxy\_temp\_file\_write\_size 64k;  
proxy\_temp\_path /var/nginx/proxy\_temp;  
proxy\_next\_upstream error timeout invalid\_header http\_500 http\_502  
http\_503  
http\_504;  
proxy\_set\_header X-Forwarded-Host $host;  
proxy\_set\_header X-Forwarded-For $proxy\_add\_x\_forwarded\_for;  
proxy\_set\_header X-Forwarded-Server $host;  
proxy\_set\_header X-Real-IP $remote\_addr;  
proxy\_set\_header X-Forwarded-Proto https;  
proxy\_http\_version 1.1;  
proxy\_set\_header Upgrade $http\_upgrade;  
proxy\_set\_header Connection “upgrade”;  
proxy\_set\_header Host $host;  
proxy\_redirect off;  
proxy\_cache sd6;  
add\_header X-Proxy-Cache $upstream\_cache\_status;  
proxy\_no\_cache $no\_cache;  
proxy\_cache\_bypass $no\_cache;

}

Below are the results of http responses

General :

Request URL:[https://ourdomain.com/server/common/...oginName=user1](https://ourdomain.com/server/common/...oginName=user1)  
Request Method:GET  
Status Code:200 OK  
Remote Address:131.212.98.12:443

Response Headers :

Cache-Control:no-cache, no-store, must-revalidate  
Connection:keep-alive  
Content-Type:image/png;charset=UTF-8  
Date:Fri, 22 Jan 2016 07:36:56 GMT  
Expires:Thu, 01 Jan 1970 00:00:00 GMT  
Pragma:no-cache  
Server:nginx  
Transfer-Encoding:chunked  
X-Proxy-Cache:MISS

It would be great if someone could provide a solution.

Posted at Nginx Forum:

> **[Cache some API requests in Nginx](https://forum.nginx.org/read.php?2,264091,264091#msg-264091)**
>
> I'm seeking advise from experts here. We have the following scenario. We have a java application. Java app is running on tomcat7. tomcat7 acting as API server. User interface files ( Static html and css ) are served by nginx. Nginx is acting as...
