现在安装的 Nginx MP3 文件不会被浏览器缓存

发布于 2024-12-17 10:30:21 字数 3811 浏览 0 评论 0原文

直到最近我一直在使用普通的 apache 安装,没有出现重大问题。

今天我安装了 Nginx Admin(cpanel nginx 插件),它充当 Apache 的反向代理来传递静态文件。

这一切都运行得很好,并且性能得到显着提升,我对此感到满意。

然而,我的网站托管了大量 MP3 文件,在使用 Nginx 之前,这些文件都会被浏览器缓存,这意味着用户只需下载一次,后续的收听都是即时的。

现在,安装了 Nginx Admin 后,浏览器每次都会从服务器请求文件。

我尝试将以下内容添加到我的 nginx 配置文件中:-

location ~* \.(mp3)$ {
          expires max;
 }

但即使这样也没有效果。可能是什么原因造成的?我还可以尝试什么来纠正这个问题?

奇怪的是,在 apache 下,我的标头看起来像这样:-

  Server    Apache
Connection  Keep-Alive
Keep-Alive  timeout=5, max=98
Vary    Accept-Encoding,User-Agent

缓存后返回 304 Not Modified 响应

,安装了 nginx admin 后,标头如下:-

Server  nginx admin
Date    Thu, 24 Nov 2011 12:46:27 GMT
Content-Type    audio/mpeg
Content-Length  5263187
Last-Modified   Mon, 26 Sep 2011 18:29:39 GMT
Connection  keep-alive
Expires Thu, 01 Dec 2011 12:46:27 GMT
Cache-Control   max-age=604800
X-Cache HIT from Backend
Accept-Ranges   bytes

无论什么情况,总是返回 200 OK。

我的 nginx 配置文件如下:-

user  nobody;
# no need for more workers in the proxy mode
worker_processes  4;
error_log  /var/log/nginx/error.log info;
worker_rlimit_nofile 20480;
events {
 worker_connections 5120; # increase for busier servers
 use epoll; # you should use epoll here for Linux kernels 2.6.x
}
http {
 server_name_in_redirect off;
 server_names_hash_max_size 10240;
 server_names_hash_bucket_size 1024;
 include    mime.types;
 default_type  application/octet-stream;
 server_tokens off;
 sendfile on;
 tcp_nopush on;
 tcp_nodelay on;
 keepalive_timeout  5;
 gzip on;
 gzip_vary on;
 gzip_disable "MSIE [1-6]\.";
 gzip_proxied any;
 gzip_http_version 1.1;
 gzip_min_length  1000;
 gzip_comp_level  6;
 gzip_buffers  16 8k;
# You can remove image/png image/x-icon image/gif image/jpeg if you have slow CPU
 gzip_types    text/plain text/xml text/css application/x-javascript application/xml image/png image/x-icon image/gif image/jpeg application/xml+rss text/javascript application/atom+xml;
 ignore_invalid_headers on;
 client_header_timeout  3m;
 client_body_timeout 3m;
 send_timeout     3m;
 reset_timedout_connection on;
 connection_pool_size  256;
 client_header_buffer_size 256k;
 large_client_header_buffers 4 256k;
 client_max_body_size 200M; 
 client_body_buffer_size 128k;
 request_pool_size  32k;
 output_buffers   4 32k;
 postpone_output  1460;
 proxy_temp_path  /tmp/nginx_proxy/;
 client_body_in_file_only on;
 log_format bytes_log "$msec $bytes_sent .";
 include "/etc/nginx/vhosts/*";
}

包含此域特定文件:-

server {
          error_log /var/log/nginx/vhost-error_log warn;
          listen x.x.x.x:x;
          server_name songbanc.com www.songbanc.com;
          access_log /usr/local/apache/domlogs/mydomain.com-bytes_log bytes_log;
          access_log /usr/local/apache/domlogs/mydomain.com combined;
          root /home/mydomain/public_html;
          location / {
          location ~.*\.(3gp|gif|jpg|jpeg|png|ico|wmv|avi|asf|asx|mpg|mpeg|mp4|pls|mp3|mid|wav|swf|flv|html|htm|txt|js|css|exe|zip|tar|rar|gz|tgz|bz2|uha|7z|doc|docx|xls|xlsx|pdf|iso)$ {
          expires 7d;
          try_files $uri @backend;
          }
          error_page 405 = @backend;
          add_header X-Cache "HIT from Backend";
          proxy_pass http://x.x.x.x:8081;
          include proxy.inc;
          }
          location @backend {
          internal;
          proxy_pass http://x.x.x.x:8081;
          include proxy.inc;
          }
          location ~ .*\.(php|jsp|cgi|pl|py)?$ {
          proxy_pass http://x.x.x.x:8081;
          include proxy.inc;
          }
          location ~ /\.ht {
          deny all;
          }

          location ~* \.(mp3)$ {
          expires max;
          }             
        }

I've been using a vanilla apache install until recently with no major problems.

Today I installed Nginx Admin (the cpanel nginx plugin) which acts as a reverse proxy for Apache to deliver static files.

This all works very well and there is a noticable performance boost which I am pleased with.

However my site hosts a large amount of MP3 files which prior to using Nginx would all get cached by the browser meaning that a user only had to download them once and subsequent listens were instananeous.

Now, with Nginx Admin installed the browser requests the file from the server every time.

I have tried adding the following to my nginx config file:-

location ~* \.(mp3)$ {
          expires max;
 }

But even this had no effect. What might be causing this and what else can I try to rectify this issue?

Strangely under apache my header looks like this:-

  Server    Apache
Connection  Keep-Alive
Keep-Alive  timeout=5, max=98
Vary    Accept-Encoding,User-Agent

This returns a 304 Not Modified response after being cached

and with nginx admin installed the headers are as follows:-

Server  nginx admin
Date    Thu, 24 Nov 2011 12:46:27 GMT
Content-Type    audio/mpeg
Content-Length  5263187
Last-Modified   Mon, 26 Sep 2011 18:29:39 GMT
Connection  keep-alive
Expires Thu, 01 Dec 2011 12:46:27 GMT
Cache-Control   max-age=604800
X-Cache HIT from Backend
Accept-Ranges   bytes

Which no matter what always returns a 200 OK.

My nginx config file is as follows:-

user  nobody;
# no need for more workers in the proxy mode
worker_processes  4;
error_log  /var/log/nginx/error.log info;
worker_rlimit_nofile 20480;
events {
 worker_connections 5120; # increase for busier servers
 use epoll; # you should use epoll here for Linux kernels 2.6.x
}
http {
 server_name_in_redirect off;
 server_names_hash_max_size 10240;
 server_names_hash_bucket_size 1024;
 include    mime.types;
 default_type  application/octet-stream;
 server_tokens off;
 sendfile on;
 tcp_nopush on;
 tcp_nodelay on;
 keepalive_timeout  5;
 gzip on;
 gzip_vary on;
 gzip_disable "MSIE [1-6]\.";
 gzip_proxied any;
 gzip_http_version 1.1;
 gzip_min_length  1000;
 gzip_comp_level  6;
 gzip_buffers  16 8k;
# You can remove image/png image/x-icon image/gif image/jpeg if you have slow CPU
 gzip_types    text/plain text/xml text/css application/x-javascript application/xml image/png image/x-icon image/gif image/jpeg application/xml+rss text/javascript application/atom+xml;
 ignore_invalid_headers on;
 client_header_timeout  3m;
 client_body_timeout 3m;
 send_timeout     3m;
 reset_timedout_connection on;
 connection_pool_size  256;
 client_header_buffer_size 256k;
 large_client_header_buffers 4 256k;
 client_max_body_size 200M; 
 client_body_buffer_size 128k;
 request_pool_size  32k;
 output_buffers   4 32k;
 postpone_output  1460;
 proxy_temp_path  /tmp/nginx_proxy/;
 client_body_in_file_only on;
 log_format bytes_log "$msec $bytes_sent .";
 include "/etc/nginx/vhosts/*";
}

With this domain specific file being included:-

server {
          error_log /var/log/nginx/vhost-error_log warn;
          listen x.x.x.x:x;
          server_name songbanc.com www.songbanc.com;
          access_log /usr/local/apache/domlogs/mydomain.com-bytes_log bytes_log;
          access_log /usr/local/apache/domlogs/mydomain.com combined;
          root /home/mydomain/public_html;
          location / {
          location ~.*\.(3gp|gif|jpg|jpeg|png|ico|wmv|avi|asf|asx|mpg|mpeg|mp4|pls|mp3|mid|wav|swf|flv|html|htm|txt|js|css|exe|zip|tar|rar|gz|tgz|bz2|uha|7z|doc|docx|xls|xlsx|pdf|iso)$ {
          expires 7d;
          try_files $uri @backend;
          }
          error_page 405 = @backend;
          add_header X-Cache "HIT from Backend";
          proxy_pass http://x.x.x.x:8081;
          include proxy.inc;
          }
          location @backend {
          internal;
          proxy_pass http://x.x.x.x:8081;
          include proxy.inc;
          }
          location ~ .*\.(php|jsp|cgi|pl|py)?$ {
          proxy_pass http://x.x.x.x:8081;
          include proxy.inc;
          }
          location ~ /\.ht {
          deny all;
          }

          location ~* \.(mp3)$ {
          expires max;
          }             
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

隔纱相望 2024-12-24 10:30:21

我相信原因可能是因为您的 conf 文件过于复杂,并且错误开始蔓延到此类配置中。

位置 ~ .*\.(php|jsp|cgi|pl|py)?$ 应该是 位置 ~ .*\.(php|jsp|cgi|pl|py)$< /代码>。

同样不需要 location / 下的长嵌套 if 块。

add_header X-Cache 指令应位于 proxy.inc 中。

诸如“@backend”之类的命名位置不需要“内部”指令。

不管怎样,在精简之后,我们剩下:

server {
    error_log /var/log/nginx/vhost-error_log warn;
    listen x.x.x.x:x;
    server_name songbanc.com www.songbanc.com;
    access_log /usr/local/apache/domlogs/mydomain.com-bytes_log bytes_log;
    access_log /usr/local/apache/domlogs/mydomain.com combined;
    root /home/mydomain/public_html;
    index index.html index.php;
    error_page 418 = @backend;

    location / {
        expires 7d;
        add_header Cache-Control "public";
        try_files $uri $uri/ @backend;
    }
    location ~* \.mp3$ {
        # Add rewrite rules to handle these requests here
        add_header Cache-Control "public";
        expires max;
    }
    location ~ .*\.(php|jsp|cgi|pl|py)$ {
        return 418;
    }
    location @backend {
        proxy_pass http://x.x.x.x:8081;
        include proxy.inc;
    }
    location ~ /\.ht {
        deny all;
    }
}

这应该缓存静态文件,不缓存动态文件。需要从后端翻译 mp3 块的重写规则。

或者,您可以将 mp3 请求传递到后端。

server {
    error_log /var/log/nginx/vhost-error_log warn;
    listen x.x.x.x:x;
    server_name songbanc.com www.songbanc.com;
    access_log /usr/local/apache/domlogs/mydomain.com-bytes_log bytes_log;
    access_log /usr/local/apache/domlogs/mydomain.com combined;
    root /home/mydomain/public_html;
    index index.html index.php;
    error_page 418 = @backend;

    location / {
        expires 7d;
        add_header Cache-Control "public";
        try_files $uri $uri/ @backend;
    }
    location ~* \.mp3$ {
        add_header Cache-Control "public";
        expires max;
        return 418;
    }
    location ~ .*\.(php|jsp|cgi|pl|py)$ {
        return 418;
    }
    location @backend {
        proxy_pass http://x.x.x.x:8081;
        include proxy.inc;
    }
    location ~ /\.ht {
        deny all;
    }
}

我不能 100% 确定触发重定向时是否未清除“expires”和“add_header”等指令。如果您发现使用上面的配置没有获得您的标头,那么这个稍长的标头应该可以做到。

server {
    error_log /var/log/nginx/vhost-error_log warn;
    listen x.x.x.x:x;
    server_name songbanc.com www.songbanc.com;
    access_log /usr/local/apache/domlogs/mydomain.com-bytes_log bytes_log;
    access_log /usr/local/apache/domlogs/mydomain.com combined;
    root /home/mydomain/public_html;
    index index.html index.php;
    error_page 418 = @backend;

    location / {
        expires 7d;
        add_header Cache-Control "public";
        try_files $uri $uri/ @backend;
    }
    location ~* \.mp3$ {
        error_page 418 = @backend_alt;
        return 418;
    }
    location ~ .*\.(php|jsp|cgi|pl|py)$ {
        return 418;
    }
    location @backend {
        proxy_pass http://x.x.x.x:8081;
        include proxy.inc;
    }
    location @backend_alt {
        proxy_pass http://x.x.x.x:8081;
        include proxy.inc;
        add_header Cache-Control "public";
        expires max;
    }
    location ~ /\.ht {
        deny all;
    }
}

I believe the reason may be because your conf file was unnecessarily complex and errors start to creep into such configs.

location ~ .*\.(php|jsp|cgi|pl|py)?$ should be location ~ .*\.(php|jsp|cgi|pl|py)$.

The long nested if block under location / is similarly not required.

The add_header X-Cache directive should be in proxy.inc.

Named locations such as "@backend" do not need "internal" directives.

Anyway, after striping things down, we are left with:

server {
    error_log /var/log/nginx/vhost-error_log warn;
    listen x.x.x.x:x;
    server_name songbanc.com www.songbanc.com;
    access_log /usr/local/apache/domlogs/mydomain.com-bytes_log bytes_log;
    access_log /usr/local/apache/domlogs/mydomain.com combined;
    root /home/mydomain/public_html;
    index index.html index.php;
    error_page 418 = @backend;

    location / {
        expires 7d;
        add_header Cache-Control "public";
        try_files $uri $uri/ @backend;
    }
    location ~* \.mp3$ {
        # Add rewrite rules to handle these requests here
        add_header Cache-Control "public";
        expires max;
    }
    location ~ .*\.(php|jsp|cgi|pl|py)$ {
        return 418;
    }
    location @backend {
        proxy_pass http://x.x.x.x:8081;
        include proxy.inc;
    }
    location ~ /\.ht {
        deny all;
    }
}

This should have the static files cached and dynamic ones not cached. requires rewrite rules for the mp3 block to be translated from the backend.

Alternatively, you can just pass the mp3 requests to the backend.

server {
    error_log /var/log/nginx/vhost-error_log warn;
    listen x.x.x.x:x;
    server_name songbanc.com www.songbanc.com;
    access_log /usr/local/apache/domlogs/mydomain.com-bytes_log bytes_log;
    access_log /usr/local/apache/domlogs/mydomain.com combined;
    root /home/mydomain/public_html;
    index index.html index.php;
    error_page 418 = @backend;

    location / {
        expires 7d;
        add_header Cache-Control "public";
        try_files $uri $uri/ @backend;
    }
    location ~* \.mp3$ {
        add_header Cache-Control "public";
        expires max;
        return 418;
    }
    location ~ .*\.(php|jsp|cgi|pl|py)$ {
        return 418;
    }
    location @backend {
        proxy_pass http://x.x.x.x:8081;
        include proxy.inc;
    }
    location ~ /\.ht {
        deny all;
    }
}

I am not 100% certain if directives such as "expires" and "add_header" are not cleared when a redirect is triggered. If you find that you are not getting the you headers with the config above, this slightly longer one should do it.

server {
    error_log /var/log/nginx/vhost-error_log warn;
    listen x.x.x.x:x;
    server_name songbanc.com www.songbanc.com;
    access_log /usr/local/apache/domlogs/mydomain.com-bytes_log bytes_log;
    access_log /usr/local/apache/domlogs/mydomain.com combined;
    root /home/mydomain/public_html;
    index index.html index.php;
    error_page 418 = @backend;

    location / {
        expires 7d;
        add_header Cache-Control "public";
        try_files $uri $uri/ @backend;
    }
    location ~* \.mp3$ {
        error_page 418 = @backend_alt;
        return 418;
    }
    location ~ .*\.(php|jsp|cgi|pl|py)$ {
        return 418;
    }
    location @backend {
        proxy_pass http://x.x.x.x:8081;
        include proxy.inc;
    }
    location @backend_alt {
        proxy_pass http://x.x.x.x:8081;
        include proxy.inc;
        add_header Cache-Control "public";
        expires max;
    }
    location ~ /\.ht {
        deny all;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文