NGINX Configuration

Below you will find detailed NGINX configuration recommendations for the features provided by this library. The examples are tested with NGINX version 1.4.6.

NGINX cache is a set of key/value pairs. The key is built with elements taken from the requests (URI, cookies, http headers etc) as specified by proxy_cache_key directive.

When we interact with the cache to purge/refresh entries we must send to NGINX a request which has the very same values, for the elements used for building the key, as the request that create the entry. In this way NGINX can build the correct key and apply the required operation to the entry.

By default NGINX key is built with $scheme$proxy_host$request_uri. For a full list of the elements you can use in the key see this page from the official documentation.


NGINX does not support purge functionality out of the box but you can easily add it with ngx_cache_purge module. You just need to compile NGINX from sources adding ngx_cache_purge with --add-module.

You can check the script tests/ to get an idea about the steps you need to perform.

Then configure NGINX for purge requests:

 1worker_processes 4;
 3events {
 4    worker_connections 768;
 7http {
 9    log_format proxy_cache '$time_local '
10        '"$upstream_cache_status | X-Refresh: $http_x_refresh" '
11        '"$request" ($status) '
12        '"$http_user_agent" ';
14    error_log /tmp/fos_nginx_error.log debug;
15    access_log /tmp/fos_nginx_access.log proxy_cache;
17    proxy_cache_path /tmp/foshttpcache-nginx keys_zone=FOS_CACHE:10m;
19    # Add an HTTP header with the cache status. Required for FOSHttpCache tests.
20    add_header X-Cache $upstream_cache_status;
22    server {
24        listen;
26        server_name localhost
28                    ;
30        proxy_set_header   Host             $host;
31        proxy_set_header   X-Real-IP        $remote_addr;
32        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
34        location / {
35            proxy_cache FOS_CACHE;
36            proxy_pass http://localhost:8080;
37            proxy_set_header Host $host;
38            proxy_cache_key $uri$is_args$args;
39            proxy_cache_valid 200 302 301 404 1m;
41            proxy_cache_purge PURGE from;
43            # For refresh
44            proxy_cache_bypass $http_x_refresh;
45        }
47        # This must be the same as the $purgeLocation supplied
48        # in the Nginx class constructor
49        location ~ /purge(/.*) {
50            allow;
51            deny all;
52            proxy_cache_purge FOS_CACHE $1$is_args$args;
53        }
54    }

Please refer to the ngx_cache_purge module documentation for more on configuring NGINX to support purge requests.


If you want to invalidate cached objects by forcing a refresh you have to use the built-in proxy_cache_bypass directive. This directive defines conditions under which the response will not be taken from a cache. This library uses a custom HTTP header named X-Refresh, so add a line like the following to your config:

            proxy_cache_bypass $http_x_refresh;


Configure your Nginx to set a custom header (X-Cache) that shows whether a cache hit or miss occurred:

add_header X-Cache $upstream_cache_status;