Gzip compression not working with pages served through memcached

jaynyc nginx-forum at nginx.us
Wed Apr 11 15:17:46 UTC 2012


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;

    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: http://forum.nginx.org/read.php?2,225184,225184#msg-225184



More information about the nginx mailing list