Nginx 似乎忽略了缓存设置

Nginx 似乎忽略了缓存设置

我有以下内容location

location /content/ {
  proxy_cache my_cache;
  proxy_cache_valid any 3m;
  proxy_cache_revalidate on;
  proxy_cache_min_uses 3;
  proxy_cache_lock on;
  proxy_ignore_headers X-Accel-Expires Expires Cache-Control Vary;
  proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
  try_files $uri $uri/ =404;

配置如下cache-path

  proxy_cache_methods GET HEAD;
  proxy_cache_path
    /var/cache/nginx
    levels=1:2
    keys_zone=my_cache:3m
    max_size=4g
    inactive=360m;

我运行此脚本来测试我的location

# for x in {1..1000}; do curl -sD- http://172.30.3.19/content/tst.txt; sleep .3; done

HTTP/1.1 200 OK
Server: nginx
Date: Wed, 21 Feb 2018 13:14:12 GMT
Content-Type: text/plain
Content-Length: 8
Last-Modified: Wed, 21 Feb 2018 13:13:54 GMT
Connection: keep-alive
ETag: "5a8d7092-8"
Accept-Ranges: bytes

test 01

然后我不断改变tst.txt似乎没有被缓存的内容。

可能是什么问题呢?

缓存目录:

# ls -ld /var/cache/nginx/
drwx------ 2 www-data www-data 4096 May 23  2017 /var/cache/nginx/

Nginx 进程:

# ps -elf | grep nginx
5 S root      1983     1  0  80   0 - 50344 -      12:18 ?        00:00:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
5 S www-data  2292  1983  0  80   0 - 51114 -      13:06 ?        00:00:00 nginx: worker process
5 S www-data  2293  1983  0  80   0 - 51114 -      13:06 ?        00:00:00 nginx: worker process
5 S www-data  2294  1983  0  80   0 - 51114 -      13:06 ?        00:00:00 nginx: worker process
5 S www-data  2295  1983  0  80   0 - 51114 SyS_ep 13:06 ?        00:00:00 nginx: worker process
5 S www-data  2296  1983  0  80   0 - 50344 -      13:06 ?        00:00:00 nginx: cache manager process
0 S root      2351  2050  0  80   0 -  2043 wait_w 13:10 pts/2    00:00:00 less nginx.conf
0 S root      2375  2068  0  80   0 -  3182 -      13:17 pts/4    00:00:00 grep nginx

Nginx版本:

# nginx -V
nginx version: nginx/1.10.3
built with OpenSSL 1.0.2l  25 May 2017
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -fPIE -fstack-protector-strong -Wformat -Werror=format-security -fPIC -D_FORTIFY_SOURCE=2' --with-ld-opt='-fPIE -pie -Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_flv_module --with-http_geoip_module=dynamic --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_mp4_module --with-http_perl_module=dynamic --with-http_random_index_module --with-http_secure_link_module --with-http_sub_module --with-http_xslt_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-stream=dynamic --with-stream_ssl_module --add-dynamic-module=/build/nginx-1.10.3/debian/modules/headers-more-nginx-module --add-dynamic-module=/build/nginx-1.10.3/debian/modules/nginx-auth-pam --add-dynamic-module=/build/nginx-1.10.3/debian/modules/nginx-cache-purge --add-dynamic-module=/build/nginx-1.10.3/debian/modules/nginx-dav-ext-module --add-dynamic-module=/build/nginx-1.10.3/debian/modules/nginx-development-kit --add-dynamic-module=/build/nginx-1.10.3/debian/modules/nginx-echo --add-dynamic-module=/build/nginx-1.10.3/debian/modules/ngx-fancyindex --add-dynamic-module=/build/nginx-1.10.3/debian/modules/nchan --add-dynamic-module=/build/nginx-1.10.3/debian/modules/nginx-lua --add-dynamic-module=/build/nginx-1.10.3/debian/modules/nginx-upload-progress --add-dynamic-module=/build/nginx-1.10.3/debian/modules/nginx-upstream-fair --add-dynamic-module=/build/nginx-1.10.3/debian/modules/ngx_http_substitutions_filter_module

答案1

看来您误解了 Nginx 缓存功能的概念。

就您所描述的情况而言,您正在提供静态内容。在这里,您可以为其他缓存系统(如浏览器、CDN 或代理服务器)设置适当的缓存标头。此外,如果您将硬盘上的静态内容缓存在硬盘上,则不会获得性能提升。

如果要使用 Nginx 的缓存功能,则必须设置源。这是缓存内容的来源服务。您可以使用 HTTP 服务器 (Nginx 代理模块)或 fast_cgi 后端(Nginx FastCGI 模块)。

但是你可以将 proxy_pass 指令发送到 Nginx 本身:

server {
  listen *:80;
  location /content/ {
    proxy_cache   my_cache;
    proxy_pass    http://127.0.0.1:8080;
  }
}
server {
  listen  *:8080;
  root    /var/www/my-site.com;
  location /content/ {
    expires     3m;
    try_files   $uri $uri/ =404;
  }
}

相关内容