Speeding up web development with local API call caching using nginx on Windows

Photo by Inactive. on Unsplash

When developing web apps you sometimes have to call APIs which you cannot host locally. This can get very annoying and time consuming if the API responses are very large, for example requesting high-resolution image data, and you have to call the API over and over again. One solution is to locally cache the API responses using a reverse proxy. This saved time and bandwidth and is surprisingly easy to set up on Windows.

The idea: set up an nginx webserver and configure it to act as reverse proxy for our API server and then call the local nginx server.

Content of nginx-1.17.0

All we need is nginx for windows and the reverse proxy configuration. Download the archive and extract the folder somewhere. No installation required. Inside the folder you find the nginx.exe and a few subfolders. Go to the conf subfolder and open nginx.conf. Replace all the content with the content from the following gist, which is an adjusted version of the configuration provided by nginx.

The essential lines are lines 15–26. Line 15 defined the cache path (temp), how the files are stores (levels), how many files can be stored (cache keys), how long the files will be stored after the last access, and the maximum cache size. In this case the path is temp and relative to the root folder of the nginx.exe file. Line 18 defines the port on the local computer where the reverse proxy is listening and line 20 defines the target server. The other parameters can be left unchanged, for details see the nginx documentation.

Next you need to start the server. You can just click the program and the server starts. However, nginx has no UI, the webserver is running as task, so to stop the server or check if the server is running you need to start a terminal (for example cmd) and check the tasklist: tasklist /fi “imagename eq nginx.exe”

To shut down the server navigate the terminal to the folder where nginx.exe is located and run nginx -s quit to gracefully shutdown the server or nginx -s kill to kill the process immediately.

Now you can call the remote API via your reverse-proxy and nginx is caching the responses on your computer. In my case where I request several high-resolution images from a remote server, the speedup was over 80%.

API call time reduced by over 5 seconds (84%)

One important remark: be aware that nginx is storing the responses unencrypted on your computer. Everybody with access to your computer can read out the files with a text editor. If this is not an issue for you, have fun! :-)

Machine learning and neuroscience | Coding python (and recently JS and Kotlin) | Building apps you love

Love podcasts or audiobooks? Learn on the go with our new app.