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 的快取功能,你必須設定一個 origin。這是快取內容的來源服務。您可以使用 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;
  }
}

相關內容