【问题标题】:Nginx cache directory is emptyNginx 缓存目录为空
【发布时间】:2019-12-09 11:32:19
【问题描述】:

我在 Ubuntu 16.04 (xenial) 上使用 Nginx v1.17.6 作为 缓存反向代理,它侦听 443 并代理对上游 backend 的请求。其他 example_ip 使用与此服务器相同的配置。 backend 服务器在端口 888 上相互通信,该端口用于通过 负载平衡器 (LB) 在它们之间进行缓存交换。 (另外,LB 也适用于对文件服务器的代理请求)。 这是我的 nginx.conf :

user www-data www-data;
worker_processes 8;
pid /run/nginx.pid;
worker_rlimit_nofile 3000;
thread_pool one threads=16 max_queue=1000;

http {

        ##
        # Basic Settings
        ##
        map_hash_bucket_size 128;
        sendfile on;
        tcp_nopush off;
        tcp_nodelay on;
        keepalive_timeout 5;
        server_tokens off;

        add_header X-Content-Type-Options nosniff;
        client_body_buffer_size 0;
        client_max_body_size 1;
        client_header_buffer_size 1k;
        large_client_header_buffers 2 1k;

        include /etc/nginx/mime.types;

        proxy_cache_path /mnt/sdc keys_zone=hard_cache:2048m levels=1:2 inactive=12h
                                        max_size=100G use_temp_path=off;
        proxy_cache_path /mnt/sdb keys_zone=ssd_cache:2048m levels=1:2 inactive=12h
                                        max_size=150G use_temp_path=off;
        map $request_uri $cache {
                ~^\/v4\/4a35e4a4o\/avatar\/.*$ ssd_cache;
                ~^\/v4\/82ca5e4ai\/succh\/.*$ ssd_cache;
                ~^\/v4\/115a5e4ac\/sucrt1\/.*$ ssd_cache;
                ~^\/v4\/8766e82ca5e4v\/mc\/.*$ ssd_cache;
                ~^\/v4\/66e82ca5e4n\/luctw\/.*$ ssd_cache;
                ~^\/v4\/23e82ca5e4d\/luc1tq\/.*$ hard_cache;
                ~^\/v4\/9266e82ca5e4s\/lcuq\/.*$ hard_cache;
                ~^\/v4\/32e82ca5e4l\/euid\/.*$ hard_cache;
                ~^\/v4\/4466e82ca5e4b\/edjp\/.*$ hard_cache;
        }

        upstream backend {
           hash $request_uri consistent;
           server example_ip1:888 max_fails=50;
           server example_ip2:888 max_fails=50;
           server example_ip3:888 max_fails=50;
           server example_ip4:888 max_fails=50;
        }

        server {
                listen     *:443 ssl backlog=2048 reuseport ;
                ssl_trusted_certificate ....;
                ssl_certificate    .....;
                ssl_certificate_key ....;

                location / {
                        proxy_pass http://backend;
                        proxy_next_upstream      error  timeout  invalid_header  http_500;
                        proxy_connect_timeout  3;
                        proxy_buffering on;
                }
       }

       server {
                listen   *:888 backlog=4096 reuseport;

                location / {
                        proxy_set_header Host $host;
                        proxy_set_header User-Agent $http_user_agent;
                        proxy_set_header X-Forwarded-Proto https;
                        proxy_set_header X-Forwarded-Port 443;
                        proxy_buffering on;
                        proxy_buffer_size 1024k;
                        proxy_buffers   4 8m;
                        proxy_cache $cache;
                        aio threads=one;
                        directio 2m;
                        aio_write on;
                        proxy_pass http://LB-ip-address;
                        proxy_max_temp_file_size 0;
                        proxy_cache_valid 200 10d;
                        proxy_cache_valid 500 501 502 503 504 505 1s;
                        proxy_cache_valid 401 402 403 404 1s;
                        proxy_connect_timeout 15;
                        proxy_cache_min_uses 1;
                }
        }

        log_format default_format'$remote_addr - $remote_user [$time_local] '
                             '"$request" $status $body_bytes_sent '
                             '"$http_referer" "$http_user_agent" '
                             'rt=$request_time ucs="$upstream_cache_status" '
        access_log /var/log/nginx/access.log default_format;
        error_log /var/log/nginx/error.log;
}

我已经在/mnt/sdb/mnt/sdc 上安装了一个磁盘,proxy_cache_path 指令使用它来缓存存储。

查看 access.log 文件时,upstream_cache_status"-""MISS" 但没有得到 "HIT"状态。

proxy_buffering 指令也是基于this 答案的on

此外,我已经将chown -R www-data:www-datachmod -R 776 设置为/mnt/sdb/mnt/sdc,但是这些目录中什么都没有出现(以/sdb 的内容为例):

root@cdn:/etc/nginx# ls -lart /mnt/sdb
total 24
drwxr-xr-x 9 root     root      4096 Dec  5 02:52 ..
drwxrw-rw- 2 www-data www-data 16384 Dec  7 05:35 lost+found
drwxrw-rw- 3 www-data www-data  4096 Dec  7 05:35 .

此配置适用于 v1.14.0 并进行缓存! This 是两个相同配置的版本差异。

总结: ssl 卸载正在正确完成,来自后端服务器(文件服务器)的响应正在正确返回给用户,但没有发生缓存!

谢谢! :)

【问题讨论】:

    标签: nginx caching reverse-proxy nginx-reverse-proxy


    【解决方案1】:

    问题是因为Set-Cookie 标头。添加这些指令以忽略 Set-Cookie 标头后,缓存成功:

    proxy_ignore_headers "Set-Cookie";
    proxy_hide_header "Set-Cookie";
    

    在 nginx v.1.14.0 中,没有 Set-Cookie 头,但它可以工作,这很奇怪!

    【讨论】:

      猜你喜欢
      • 2015-10-21
      • 2020-05-05
      • 2020-09-05
      • 2020-10-09
      • 2021-04-29
      • 1970-01-01
      • 2011-05-25
      • 2012-10-25
      • 2020-02-09
      相关资源
      最近更新 更多