2

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.

Donm
  • 41
  • 1
  • 8

2 Answers2

1

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

piotrp
  • 3,755
  • 1
  • 24
  • 26
  • So i would have to for example proxy pass from location / to location /test.html in order to cache /test.html ? – Donm Sep 12 '18 at 06:20
  • So I made another server and used proxy pass and then used proxy cache and now it works. Additionaly i moved X-Cache header to the location where is use proxy cache. Now it works as expected. I guess it was silly mistake, but sometimes those are hardest to find. Thanks for the help. – Donm Sep 12 '18 at 07:39
1

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.

Chao Zhang
  • 61
  • 1
  • 8
  • I have tried putting this header inside sever block and location block ass well, it still doesn't show in headers. – Donm Sep 12 '18 at 07:19