Search code examples
nginxbrowser-cachepythonanywhere

Configure nginx server on Pythonanywhere


I am trying to "leverage browser caching" in order to increase site speed. The webapp is hosted on pythonanywhere and I guess I need to configure the nginx.conf file to include:

location ~* \.(css|js|gif|jpe?g|png)$ {
  expires 168h;
  add_header Pragma public;
  add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}

(from here: how to Leverage browser caching in django)

However I can't find the conf file anywhere. It is not in /etc/nginx, /usr/local/etc /usr/etc ...

Can this be done on pythonanywhere ?


Solution

  • PythonAnywhere dev here. Unfortunately you can't change the nginx settings on our system -- but the system-default settings are actually pretty much what you'd want. If you're using the "Static files" table on the "Web" tab to specify where they are, then:

    • When a browser requests a static file for the first time, it's sent back with a header saying when it was last modified (based on the file timestamp).
    • When the browser requests the static file after that, and it has a copy in its cache, it will normally send a "if-modified-since" header with the value of the last-modified header it got the first time around.
    • The server will check the file timestamp, and if the file hasn't changed, it will send back an HTTP 304 ("not modified") response with no content, so the browser knows it can just used the cached one. If the file has changed, then of course it sends back a normal 200 response with the new content and an updated last-modified timestamp for the browser to cache.