deepak
deepak

Reputation: 349

How to configure NGINX to start a cacheRefresher program on server startup

Is there any way i can configure a python module to be invoked as part of the startup,when ever the NGINX server starts.

The role of the python module is to query database and cache it in memory database like redis. It also has to refresh the cache periodically every 5 mins for example.

I am using NGINX as reverse proxy server and uWSGI as the application server. The idea of caching is to reduce the application server response time to milliseconds from mins

Upvotes: 0

Views: 185

Answers (1)

Harry
Harry

Reputation: 11668

Originally your question sounded like you an nginx caching proxy...

https://www.nginx.com/resources/wiki/start/topics/examples/reverseproxycachingexample/

Preloading an application cache like Redis is a different problem. What goes in the cache and in what format is hard to generalize ie you may want to cache html fragments and I may want to cache JSON data from an API, someone else may want to cache queries from a database.

Personally if you're serving requests and your applications uses the cache then use the application to preload the cache. A concrete example that I've used with Squid but this will also work with nginx.

  1. Parse logs and gets stats for most used URL's. This is your heatmap.
  2. Start Nginx.
  3. Hit those URL's.

This loads the recently used items into the cache. I've also used the following method ie get id's from database and then hit the app... (forgive my python)

import grequests

base_url = "http://localhost:70/?id="
big_list = ['1', '2', '3', '4', '5']

for i in range(len(big_list)):
      big_list[i] = base_url + big_list[i]

rs = (grequests.get(u) for u in big_list)
grequests.map(rs)

This loops over a list of id's and hits the URL. The app then caches everything it needs.

You can use any combination of the above ie I'd rather load only what's needed to avoid caching rubbish that's rarely requested.

The old answer follows...

Your question doesn't read very well but anytime I hear nginx and caching it's normally about how to configure it to cache pages. Some more details here...

https://serversforhackers.com/nginx-caching/

Have a look here for nginx caching with timeouts ie look for

http://nginx.org/en/docs/http/ngx_http_proxy_module.html

look for proxy_cache_valid on that page.

Upvotes: 1

Related Questions