Donm
Donm

Reputation: 51

Caching is not working in nginx using proxy_cache.

I'am trying to set up basic caching in my openresty nginx webserver. I have tried milion different combinations from many different tutorials, but I can't get it right. Here is my nginx.conf file

user www-data;
worker_processes 4;
pid /run/openresty.pid;  
worker_rlimit_nofile 30000;

events {
worker_connections  20000;
}
http {

proxy_cache_path /tmp/nginx/cache levels=1:2 keys_zone=cache:10m max_size=100m inactive=60m;

proxy_cache_key "$scheme$request_method$host$request_uri";

add_header X-Cache $upstream_cache_status;

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

access_log /var/log/openresty/access.log;
error_log /var/log/openresty/error.log;

include ../sites/*;

lua_package_cpath '/usr/local/lib/lua/5.1/?.so;;';


}

And here is my server configuration

server {
# Listen on port 8080.
listen 8080;
listen [::]:8080;

# The document root.
root /var/www/cache;

# Add index.php if you are using PHP.
index index.php index.html index.htm;

# The server name, which isn't relevant in this case, because we only have one.
server_name cache.com;

# Redirect server error pages to the static page /50x.html.
error_page   500 502 503 504  /50x.html;
location = /50x.html {
    root /var/www/cache;
}

location /test.html {

 root /var/www/cache;
 default_type text/plain;
 try_files $uri /$uri;
 expires 1h;
 add_header Cache-Control "public";
 proxy_cache cache;
 proxy_cache_valid 200 301 302 60m;
}
}

Caching should work fine, there is nothing in error.log or access.log, caching system folder is empty, X-Cache header with $upstream_cache_status is not even showing, when I get headers from curl (curl -I). Now in my nginx (openresty) configuration there is no --without-ngx_http_proxy_module flag so the module is there. I have no idea what am I doing wrong please help.

Upvotes: 2

Views: 3279

Answers (2)

Chao Zhang
Chao Zhang

Reputation: 61

The add_header defined inside the http block will be covered the one defined in the server block. Here is the snippet from the document about add_header

There could be several add_header directives. These directives are inherited from the previous level if and only if there are no add_header directives defined on the current level.

If the always parameter is specified (1.7.5), the header field will be added regardless of the response code.

So you cannot see the X-Cache header as expected.

Upvotes: 1

piotrp
piotrp

Reputation: 3854

You didn't define anything that can be cached: proxy_cache works togeher with proxy_pass.

Upvotes: 1

Related Questions