# Change blank uwsgi\_cache\_key default, or log warning

**URL:** <https://www.ruby-forum.com/t/change-blank-uwsgi-cache-key-default-or-log-warning/231957>\
**Category:** NGINX\
**Created:** [September 9, 2014, 8:25pm UTC](https://www.ruby-forum.com/t/change-blank-uwsgi-cache-key-default-or-log-warning/231957 "2014-09-09T20:25:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![addis\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c4cdca/32.png) [@addis\_a](https://www.ruby-forum.com/u/addis_a)\
**Post date:** [September 9, 2014, 8:25pm UTC](https://www.ruby-forum.com/t/change-blank-uwsgi-cache-key-default-or-log-warning/231957/1 "2014-09-09T20:25:44Z")

</div>

Hello,

Because uwsgi\_cache\_key has no default value (or rather, has the empty  
string as its default value), a configuration with uwsgi\_cache set but  
uwsgi\_cache\_key not set behaves in a way that is very unlikely to be  
desired: Nginx caches the first publicly cacheable response it gets from  
upstream (for any request), and serves that cached response to _any_  
request  
mapping to the same location. The internals of that can be seen in the  
debug  
log, where the cache key is empty for all requests:

2014/09/09 17:41:02 [debug] 91211#0: \*13 http cache key: “”

This is in contrast to a configuration with proxy\_cache enabled but  
proxy\_cache\_key not set; that behaves OK because proxy\_cache\_key has a  
useful default.

Because of the _general_ correspondence between the http\_proxy and  
http\_uwsgi modules, it’s easy to fall into this trap, defining  
uwsgi\_cache  
but not uwsgi\_cache\_key. When that happens, and unexpected responses  
start  
coming back, the first place one looks is error.log, and there’s nothing  
there.

To get rid of this gotcha, I suggest either:

1. log a warning whenever a location/server/http block has uwsgi\_cache  
set  
but no uwsgi\_cache\_key set.

or

1. change the default value of uwsgi\_cache\_key to a more useful default,  
such as $scheme$host$request\_uri, similar to proxy\_cache\_key (not quite  
the  
same, because the proxy\_cache\_key has $proxy\_host in its default, and  
there  
is no corresponding $uwsgi\_host). You might not want to make such a  
default-behavior change in a subminor release — but as a  
counterargument,  
the current default seems quite unlikely to be relied on by anyone.

Cheers,

Gulli

Posted at Nginx Forum:

> **[Change blank uwsgi\_cache\_key default, or log warning](https://forum.nginx.org/read.php?2,253149,253149#msg-253149)**
>
> Hello, Because uwsgi\_cache\_key has no default value (or rather, has the empty string as its default value), a configuration with uwsgi\_cache set but uwsgi\_cache\_key not set behaves in a way that is very unlikely to be desired: Nginx caches the first...

---

<div class="post-metadata">

**Author:** ![gthb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/cc9497/32.png) [@gthb](https://www.ruby-forum.com/u/gthb)\
**Post date:** [September 10, 2014, 7:49pm UTC](https://www.ruby-forum.com/t/change-blank-uwsgi-cache-key-default-or-log-warning/231957/2 "2014-09-10T19:49:31Z")

</div>

Hello!

On Tue, Sep 09, 2014 at 02:25:15PM -0400, gthb wrote:

> 2014/09/09 17:41:02 [debug] 91211#0: \*13 http cache key: “”
> 
> To get rid of this gotcha, I suggest either:
> 
> 1. log a warning whenever a location/server/http block has uwsgi\_cache set  
> but no uwsgi\_cache\_key set.

Yes, this is mostly trivial and certainly makes sense.  
Patch below.

# HG changeset patch

# User Maxim D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

# Date 1410371072 -14400

# Wed Sep 10 21:44:32 2014 +0400

# Node ID bc4ee0b7cf2643fdcea310638302b9cadc7ac939

# Parent aaa82dc56c9460db1b4233fc1d4559fdd07ff7ed

Added warning about unset cache keys.

In fastcgi, scgi and uwsgi modules there are no default cache keys, and  
using a cache without a cache key set is likely meaningless.

diff --git a/src/http/modules/ngx\_http\_fastcgi\_module.c  
b/src/http/modules/ngx\_http\_fastcgi\_module.c  
— a/src/http/modules/ngx\_http\_fastcgi\_module.c  
+++ b/src/http/modules/ngx\_http\_fastcgi\_module.c  
@@ -2582,6 +2582,11 @@ ngx\_http\_fastcgi\_merge\_loc\_conf(ngx\_conf  
conf-\>cache\_key = prev-\>cache\_key;  
}

- if (conf-\>upstream.cache && conf-\>cache\_key.value.data == NULL) {
- 

```
   ngx_conf_log_error(NGX_LOG_WARN, cf, 0,

```

- 

```
                      "no \"fastcgi_cache_key\" for 

```

"fastcgi\_cache"");

- }
- ngx\_conf\_merge\_value(conf-\>upstream.cache\_lock,  
prev-\>upstream.cache\_lock, 0);

diff --git a/src/http/modules/ngx\_http\_scgi\_module.c  
b/src/http/modules/ngx\_http\_scgi\_module.c  
— a/src/http/modules/ngx\_http\_scgi\_module.c  
+++ b/src/http/modules/ngx\_http\_scgi\_module.c  
@@ -1337,6 +1337,11 @@ ngx\_http\_scgi\_merge\_loc\_conf(ngx\_conf\_t  
conf-\>cache\_key = prev-\>cache\_key;  
}

- if (conf-\>upstream.cache && conf-\>cache\_key.value.data == NULL) {
- 

```
   ngx_conf_log_error(NGX_LOG_WARN, cf, 0,

```

- 

```
                      "no \"scgi_cache_key\" for \"scgi_cache\"");

```

- }
- ngx\_conf\_merge\_value(conf-\>upstream.cache\_lock,  
prev-\>upstream.cache\_lock, 0);

diff --git a/src/http/modules/ngx\_http\_uwsgi\_module.c  
b/src/http/modules/ngx\_http\_uwsgi\_module.c  
— a/src/http/modules/ngx\_http\_uwsgi\_module.c  
+++ b/src/http/modules/ngx\_http\_uwsgi\_module.c  
@@ -1524,6 +1524,11 @@ ngx\_http\_uwsgi\_merge\_loc\_conf(ngx\_conf\_t  
conf-\>cache\_key = prev-\>cache\_key;  
}

- if (conf-\>upstream.cache && conf-\>cache\_key.value.data == NULL) {
- 

```
   ngx_conf_log_error(NGX_LOG_WARN, cf, 0,

```

- 

```
                      "no \"uwsgi_cache_key\" for 

```

"uwsgi\_cache"");

- }
- ngx\_conf\_merge\_value(conf-\>upstream.cache\_lock,  
prev-\>upstream.cache\_lock, 0);

–  
Maxim D.

> **[nginx](http://nginx.org/)**
