简体   繁体   English

使用proxy_cache在nginx中无法使用缓存。

[英]Caching is not working in nginx using proxy_cache.

I'am trying to set up basic caching in my openresty nginx webserver. 我正在尝试在我的openresty nginx Web服务器中设置基本缓存。 I have tried milion different combinations from many different tutorials, but I can't get it right. 我从许多不同的教程中尝试了milion的不同组合,但是我做不到。 Here is my nginx.conf file 这是我的nginx.conf文件

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). 缓存应该可以正常工作,error.log或access.log中没有任何内容,缓存系统文件夹为空,当我从curl(curl -I)获取标头时,甚至没有显示带有$ upstream_cache_status的X-Cache标头。 Now in my nginx (openresty) configuration there is no --without-ngx_http_proxy_module flag so the module is there. 现在在我的nginx(openresty)配置中,没有--with-ngx_http_proxy_module标志,因此模块在那里。 I have no idea what am I doing wrong please help. 我不知道我在做什么错,请帮忙。

您没有定义任何可以缓存的内容:proxy_cache与proxy_pass proxy_cache工作。

The add_header defined inside the http block will be covered the one defined in the server block. http块内定义的add_header将覆盖server块中定义的add_header Here is the snippet from the document about add_header 这是有关add_header的文档片段

There could be several add_header directives. 可能有几个add_header指令。 These directives are inherited from the previous level if and only if there are no add_header directives defined on the current level . 当且仅当当前级别上没有定义add_header指令时,这些指令才从上一级继承

If the always parameter is specified (1.7.5), the header field will be added regardless of the response code. 如果指定了always参数(1.7.5),则无论响应代码如何,都将添加标头字段。

So you cannot see the X-Cache header as expected. 因此,您将无法看到X-Cache标头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM