# Gzip compression not working with pages served through memcached

**URL:** <https://www.ruby-forum.com/t/gzip-compression-not-working-with-pages-served-through-memcached/218769>\
**Category:** NGINX\
**Created:** [April 11, 2012, 5:18pm UTC](https://www.ruby-forum.com/t/gzip-compression-not-working-with-pages-served-through-memcached/218769 "2012-04-11T17:18:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gurdipe\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b77776/32.png) [@Gurdipe\_D](https://www.ruby-forum.com/u/Gurdipe_D)\
**Post date:** [April 11, 2012, 5:18pm UTC](https://www.ruby-forum.com/t/gzip-compression-not-working-with-pages-served-through-memcached/218769/1 "2012-04-11T17:18:35Z")

</div>

Hi,

I’m trying to set up nginx so that it serves entire pages from  
memcached. If it can’t find the requested URI in memcached, it proxies  
the request to Apache (running on port 8080 on the same server). It  
seems to work so far, the only problem being that gzip compression does  
not work for pages served through memcached. Also, I’m using Ubuntu 11.

Here are my config files:

* * *

/etc/nginx/nginx.conf

user nginx;  
worker\_processes 1;

error\_log /var/log/nginx/error.log warn;  
pid /var/run/nginx.pid;

events {  
worker\_connections 1024;  
}

http {  
include /etc/nginx/mime.types;  
default\_type application/octet-stream;

```
log_format main '$remote_addr - $remote_user [$time_local]

```

“$request” ’  
'$status $body\_bytes\_sent “$http\_referer” ’  
‘“$http\_user\_agent” “$http\_x\_forwarded\_for”’;

```
access_log /var/log/nginx/access.log main;

sendfile on;
#tcp_nopush on;

keepalive_timeout 65;

gzip on;
gzip_proxied any;

include /etc/nginx/conf.d/*.conf;

```

## }

/etc/nginx/conf.d/default.conf

server {  
listen 80;  
server\_name localhost [test.mydomain.com](http://test.mydomain.com);

```
access_log /var/log/nginx/log/host.access.log main;

location / {
   proxy_set_header Host $host;
   proxy_set_header X-Real-IP $remote_addr;
   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
   proxy_set_header Accept-Encoding gzip,deflate;

   # we never cache post requests
   if ($request_method = POST)
   {
    proxy_pass http://test.mydomain.com:8080;
    break;
   }

   # root /usr/share/nginx/html;
   # index index.html index.htm;
   default_type "text/html; charset=utf-8";
   source_charset utf-8;
   charset utf-8;
   set $memcached_key $uri;
   memcached_pass 127.0.0.1:11211;
   error_page 404 @notcached;
}

location @notcached {
  internal;
  # if the file is not found, forward request to proxy
  proxy_set_header Host $host;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

  proxy_pass http://test.mydomain.com:8080;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
    root /usr/share/nginx/html;
}

# proxy PHP scripts and static images to Apache listening on

```

127.0.0.1:80  
#  
location ~ .(php|css|js|jpeg|jpg|gif|png)$ {  
proxy\_set\_header Host $host;  
proxy\_set\_header X-Real-IP $remote\_addr;  
proxy\_set\_header X-Forwarded-For $proxy\_add\_x\_forwarded\_for;

```
    proxy_pass http://test.mydomain.com:8080;
}

```

## }

Thanks in advance for your help!

Regards,

- Jay

Posted at Nginx Forum:

> **[Gzip compression not working with pages served through memcached](https://forum.nginx.org/read.php?2,225184,225184#msg-225184)**
>
> Hi, I'm trying to set up nginx so that it serves entire pages from memcached. If it can't find the requested URI in memcached, it proxies the request to Apache (running on port 8080 on the same server). It seems to work so far, the only problem being...

---

<div class="post-metadata">

**Author:** ![jaynyc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dbc845/32.png) [@jaynyc](https://www.ruby-forum.com/u/jaynyc)\
**Post date:** [April 11, 2012, 11:28pm UTC](https://www.ruby-forum.com/t/gzip-compression-not-working-with-pages-served-through-memcached/218769/2 "2012-04-11T23:28:02Z")

</div>

Hello!

On Wed, Apr 11, 2012 at 11:17:46AM -0400, jaynyc wrote:

> Hi,
> 
> I’m trying to set up nginx so that it serves entire pages from  
> memcached. If it can’t find the requested URI in memcached, it proxies  
> the request to Apache (running on port 8080 on the same server). It  
> seems to work so far, the only problem being that gzip compression does  
> not work for pages served through memcached. Also, I’m using Ubuntu 11.

[…]

> ```
> default_type "text/html; charset=utf-8";
> source_charset utf-8;
> charset utf-8;
> 
> ```

Use

default\_type text/html;  
charset utf-8;

instead. Your attempt to put charset into default\_type confuses  
gzip\_types matching and gzip isn’t activated as it thinks response  
content type isn’t listed in gzip\_types.

Maxim D.

---

<div class="post-metadata">

**Author:** ![jaynyc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dbc845/32.png) [@jaynyc](https://www.ruby-forum.com/u/jaynyc)\
**Post date:** [April 12, 2012, 5:58pm UTC](https://www.ruby-forum.com/t/gzip-compression-not-working-with-pages-served-through-memcached/218769/3 "2012-04-12T17:58:09Z")

</div>

Hi Maxim,

thanks a lot for your help. That fixed it!

Regards,

- Jay

Posted at Nginx Forum:

> **[Re: Gzip compression not working with pages served through memcached](https://forum.nginx.org/read.php?2,225184,225250#msg-225250)**
>
> Hi, I'm trying to set up nginx so that it serves entire pages from memcached. If it can't find the requested URI in memcached, it proxies the request to Apache (running on port 8080 on the same server). It seems to work so far, the only problem being...
