我有Nginx缓存服务器构建在Ubuntu 18和与码头图像nginx:1.19.10-高寒。
Ubuntu 18磁盘使用详细信息如下供参考
ubuntu@host_name:~$ df -h
Filesystem Size Used Avail Use% Mounted on
udev 126G 0 126G 0% /dev
tmpfs 26G 1.4M 26G 1% /run
/dev/mapper/vg_system-root 193G 8.9G 176G 5% /
tmpfs 126G 0 126G 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 126G 0 126G 0% /sys/fs/cgroup
/dev/mapper/vg_data-srv 24T 369G 24T 2% /srv
/dev/sda1 453M 364M 62M 86% /boot
overlay 193G 8.9G 176G 5% /var/lib/docker/overlay2/64_characters_random/merged
tmpfs 26G 0 26G 0% /run/user/1646269961
码头集装箱详情
ubuntu@host_name:~$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
contnr_idxyz nginx:1.19.10-alpine "/docker-entrypoint.…" 5 days ago Up 9 hours contnr_name
Nginx配置可供参考
user@host-name:/srv/mytool/nginx/config$ cat proxy.conf
access_log off;
root /var/log/nginx;
open_log_file_cache max=100;
log_format mytoollogformat
'$time_iso8601 $remote_addr $status $request_time $body_bytes_sent '
'$upstream_cache_status "$request" "$http_user_agent"';
proxy_http_version 1.1;
client_max_body_size 10g;
# R/W timeout for origin server
proxy_read_timeout 15m;
proxy_send_timeout 15m;
# R/W timeout for clients
client_body_timeout 15m;
client_header_timeout 15m;
send_timeout 15m;
# TODO: ssl_stapling and ssl_ciphers
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
proxy_set_header Connection "";
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-mytool-Cache-Host $scheme://$host;
proxy_redirect off;
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=mytool:10m max_size=22000g inactive=180d;
proxy_cache_key $host$uri$is_args$args$slice_range;
proxy_set_header Range $slice_range;
proxy_cache_valid 200 206 2y;
proxy_cache_revalidate on;
add_header X-Cache-Status $upstream_cache_status;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_429;
删除服务器块,因为它脱离了当前问题的上下文,也是出于安全原因。
让我详细解释一下我的问题。我们有一个主服务器(proxy_passed服务器),它有数以百计的文件,是静态资源。当我们设置缓存服务器时,它很好地从缓存中填充缓存和服务文件。但是随着时间的推移,缓存大小并没有增加到344 is以上。
user@host_name:/srv/mytool/nginx$ sudo du -sh ./*
344G ./cache
52K ./config
1004K ./log
user@host_name:/srv/mytool/nginx$
我已经写了一个脚本来下载大约500 to的文件。但是它从未将缓存的大小增加到344 of以上。
迄今所做的实验
添加了max_size=100000g
(连同旧值min_free=1000g)
修改后的max_size=22000g
(设置值小于/srv大小(24 is ))
已删除的min_free=1000g
(假设min_free正在以某种方式清除缓存)
已修改
proxy_cache_valid 200 206 1h;
到proxy_cache_valid 200 206 2y;
对于以上所有的实验,在配置更改之后,我重新启动了docker容器,并运行了通过缓存服务器下载500 of文件的脚本。但是,即使缓存大小达到380到400 GB,但在一个小时内,它突然下降到344 GB。
我不知道为什么缓存没有完全填满,即使我已经为/srv分配了24 up。
和Nginx有问题吗?我的意思是,对于免费版的Nginx,可能会有任何限制。我应该和Nginx一起去吗?或者可能是配置错误。
任何猜测都对我有帮助。提前感谢
更新
缓存服务器上的最大打开文件的软限制和硬限制是什么?
$ cat /proc/sys/fs/file-max
26375980
$ ulimit -Hn
1048576
$ ulimit -Sn
1024
您是否使用worker_rlimit_nofile在您的nginx中设置了限制?
目前没有worker_rlimit_nofile的设置
/ # cat /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
错误日志中有什么东西吗?
在给定的过滤/不同日志下面
$ cat /srv/mytool/nginx/log/error.log
2022/01/09 05:56:35 [warn] 22#22: *10 an upstream response is buffered to a temporary file /var/cache/nginx/proxy_temp/1/00/0000000001 while reading upstream, client: masked_IPv6_address, server: dev.mytool-region.mycompany.com, request: "GET /dists/58.2.A.0.409/semc/binary-arm/Packages HTTP/1.1", upstream: "https://[IPv6_address]:443/masked_path/Packages", host: "dev.mytool-region.mycompany.com"
2022/01/09 06:09:21 [warn] 22#22: *35 a client request body is buffered to a temporary file /var/cache/nginx/client_temp/0000000006, client: masked_IPv6_address, server: dev.mytool-region.mycompany.com, request: "POST /masked_path/all HTTP/1.1", host: "dev.mytool-region.mycompany.com"
2022/01/09 08:19:01 [error] 22#22: *120 etag mismatch in slice response while reading response header from upstream, client: masked_IPv6_address, server: dev.mytool-region.mycompany.com, request: "GET /masked_path/xyz.zip HTTP/1.1", subrequest: "/masked_path/xyz.zip", upstream: "https://[IPv6_address]:443/masked_path/xyz.zip", host: "dev.mytool-region.mycompany.com"
2022/01/09 18:19:12 [warn] 22#22: *1566 upstream server temporarily disabled while reading response header from upstream, client: masked_IPv6_address, server: dev.mytool-region.mycompany.com, request: "GET /masked_path/xyz.zip HTTP/1.1", upstream: "https://[masked_IPv6_address]:443/masked_path/xyz.zip", host: "dev.mytool-region.mycompany.com"
2022/01/10 01:23:20 [error] 22#22: *2920 etag mismatch in slice response while reading response header from upstream, client: masked_IPv6_address, server: dev.mytool-region.mycompany.com, request: "GET /masked_path/xyz.zip HTTP/1.1", subrequest: "/masked_path/xyz.zip", upstream: "https://[IPv6_address]:443/masked_path/xyz.zip", host: "dev.mytool-region.mycompany.com"
2022/01/21 07:43:47 [error] 36#36: *441913 upstream timed out (110: Operation timed out) while SSL handshaking to upstream, client: masked_IPv6_address, server: dev.mytool-region.mycompany.com, request: "GET /masked_path/xyz.zip HTTP/1.1", upstream: "https://[IPv6_address]:443/masked_path/xyz.zip", host: "dev.mytool-region.mycompany.com"
2022/01/21 07:46:17 [warn] 37#37: *442070 upstream server temporarily disabled while SSL handshaking to upstream, client: masked_IPv6_address, server: dev.mytool-region.mycompany.com, request: "GET /masked_path/xyz.zip HTTP/1.1", upstream: "https://[IPv6_address]:443/masked_path/xyz.zip", host: "dev.mytool-region.mycompany.com"
Total 25k rows same as below within 10 days of logs
2022/01/11 05:36:58 [alert] 70#70: ignore long locked inactive cache entry 55a25a5037f198bbec6cd49100bb1b76, count:1
2022/01/11 05:36:58 [alert] 70#70: ignore long locked inactive cache entry e996d5e104f405444a579cd491faf3a8, count:1
2022/01/11 05:36:58 [alert] 70#70: ignore long locked inactive cache entry 394517a8ed8e43949003b3f7538dc471, count:1
2022/01/11 05:37:08 [alert] 70#70: ignore long locked inactive cache entry 4f92d3a72f64b7bafdbb3f0b66d8e638, count:1
2022/01/11 05:37:08 [alert] 70#70: ignore long locked inactive cache entry be41b259a3e8f9698e0976639883a423, count:1
2022/01/11 05:37:08 [alert] 70#70: ignore long locked inactive cache entry 1da19b571ea4bce1428251689f0a7c69, count:1
2022/01/11 05:37:08 [alert] 70#70: ignore long locked inactive cache entry 2a4cac0c28ea430e7eef3f808cf1e06f, count:1
2022/01/11 05:37:18 [alert] 70#70: ignore long locked inactive cache entry 53a826f6931cf0f16020bcae100af347, count:1
更新2:对nginx:perl容器进行了同样的尝试。它也没有工作,缓存大小观察到,即使它增长超过392‘t,但在几个小时内突然下降到344’t。命令,用于启动下面给定的容器
sudo docker run \
--detach \
--restart unless-stopped \
--volume /srv/mytool/nginx/config:/etc/nginx/conf.d:ro \
--volume /srv/mytool/nginx/cache:/var/cache/nginx \
--volume /srv/mytool/nginx/log:/var/log/nginx \
nginx:perl
再更新一次
避免了码头集装箱Nginx :1.19.10-高寒,并做了简单的Nginx配置,如下所示
sudo apt install nginx
systemctl status nginx
$ sudo ufw app list
Available applications:
Nginx Full
Nginx HTTP
Nginx HTTPS
OpenSSH
$ sudo ufw allow 'Nginx Full'
Rules updated
Rules updated (v6)
修改/etc/nginx/site中的default.conf -可用
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=custom_cache:10m inactive=180d;
upstream origin_server {
server dev.mytool-region.mycompany.com;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
location / {
include proxy_params;
proxy_pass http://origin_server;
}
location ~ ^/(path1|path2|path3|path4)/ {
slice 5m;
proxy_cache custom_cache;
proxy_pass http://origin_server;
proxy_cache_valid 200 206 2y;
add_header X-Proxy-Cache $upstream_cache_status;
}
}
下载了大约500。它正常工作,并按预期填充缓存。
ubuntu@host_name:/var/cache$ sudo du -sh ./*
128K ./apparmor
82M ./apt
4.8M ./debconf
20K ./ldconfig
1.2M ./man
0 ./motd-news
518G ./nginx
4.0K ./pollinate
20K ./snapd
ubuntu@host_name:/var/cache$
但仍然不知道确切的原因或我的配置有什么问题。审判还在继续。
再试一次
采用旧配置(坞号nginx:1.19.10-高寒,并将proxy_cache_valid 200 206 2y;
移到里面
location ~ ^/(path1|path2|path3|path4)/ {
但这也行不通。
发布于 2022-02-06 08:14:46
nginx缓存切片的问题是当您配置缓存切片(例如5MB)时。它最终将在缓存目录中创建切片缓存文件。可以缓存的文件数与keys_zone大小keys_zone=mytool:10m
成正比。
由于我有10m(10兆字节)的缓存密钥,它允许最多71203个文件。文件上说
此外,所有有关数据的活动键和信息都存储在共享内存区域中,其名称和大小由keys_zone参数配置。一个兆字节的区域可以存储大约8000个密钥。 作为商业订阅的一部分,共享内存区域还存储扩展缓存信息,因此需要为相同数目的键指定更大的区域大小。例如,一个兆字节区域可以存储大约4000个密钥。
因此,将keys_zone修改为更大的值keys_zone=mytool:1000m
固定问题。
您可以观察到keys_zone=mytool:10m
在71203之后缓存文件计数没有增长。
user@host_name:/srv/mytool/nginx$ sudo du -sh ./*
326G ./cache
52K ./config
406M ./log
user@host_name:/srv/mytool/nginx$ sudo find cache/ -type f | wc -l
71203
但是通过允许keys_zone=mytool:1000m
无缝地缓存文件,它的大小开始增长。
user@host_name:/srv/mytool/nginx$ sudo du -sh ./*
518G ./cache
52K ./config
4.6M ./log
user@host_name:/srv/mytool/nginx$ sudo find cache/ -type f | wc -l
107243
发布于 2022-01-26 15:57:49
我非常怀疑您的后端服务在nginx询问“如果-修改-自”时响应“是”。
还有你的背景
proxy_cache_revalidate on;
删除过时的缓存项,就像它应该做的那样。这就解释了为什么缓存可以增长到500 g,但稍晚一点就会减少到344 G。
发布于 2022-01-27 02:28:59
根据文档,max_size
参数是可选的。
如果不指定值,缓存就可以使用所有可用的磁盘空间。
删除max-size
可能会让缓存使用所有可用空间。尝试编辑proxy_cache_path
指令并删除它,在当前的conf中修改为:
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=mytool:10m inactive=180d;
https://stackoverflow.com/questions/70741577
复制相似问题