ryandotsmith / nginx-buildpack

Run NGINX in front of your app server on Heroku
456 stars 783 forks source link

Proxy Cache not working on heroku #58

Open sub2u opened 9 years ago

sub2u commented 9 years ago

I have a config which works on my local machine with proxy reverse cache. It bypass the hitting the box every time.

I used nginx-build pack with proxy cache but on nginx every time it hit the box i can observe heroku[router] on logs

providing the config for reference in bottom.

Sample config

daemon off;

Heroku dynos have at least 4 cores.

worker_processes <%= ENV['NGINX_WORKERS'] || 4 %>;

events { use epoll; accept_mutex on; worker_connections 1024; }

http { gzip on; gzip_comp_level 2; gzip_min_length 512;

server_tokens off;

log_format l2met 'measure#nginx.service=$request_time request_id=$http_x_request_id';
access_log logs/nginx/access.log l2met;
error_log logs/nginx/error.log;

include mime.types;
default_type application/octet-stream;
sendfile on;

#Must read the body in 5 seconds.
client_body_timeout 5;

upstream app_server {
    server unix:/tmp/nginx.socket fail_timeout=0;
}

proxy_cache_path  /tmp/nginx  levels=1:2    keys_zone=STATIC:10m
                                 inactive=24h  max_size=1g;

server {
    listen <%= ENV["PORT"] %>;
    server_name _;
    keepalive_timeout 5;

    location  / {
        proxy_pass             http://app_server;
    }

    location  ~* ^/(.*)/(.*)/(.*).(jpe?g|png|[tg]iff?|svg) {
        proxy_pass             http://app_server;
        add_header Cache-Control public;
        add_header X-Cache-Status $upstream_cache_status
        proxy_set_header       Host $host;
        proxy_cache            STATIC;
        proxy_cache_valid      200  1d;
        proxy_cache_use_stale  error timeout invalid_header updating
                               http_500 http_502 http_503 http_504;
    }
}

}

GertSallaerts commented 8 years ago

Your NGINX server will run on every dyno and the Heroku Router is a layer in front of these dyno's, so when your app receives a request, it will always pass through the Heroku Router before hitting NGINX. This is why you see the router logs.

You can check the response header (X-Cache-Status from your example) to see if the NGINX cache is working.

stran12 commented 7 years ago

Did anyone figure this out?