Nginx 缓存控制 / 过期标头将不起作用

Nginx 缓存控制 / 过期标头将不起作用

我到处寻找这个问题的有效答案,但似乎没有一个答案有效。我很抱歉发了这么长的帖子,但我只是想就此寻求帮助。我尽量写得尽可能详细。

我在 CentOS 7 上安装了 ngx_pagespeed 的 nginx 网络服务器,我从源代码构建,但无论如何,我都无法让cache-controlexpire标头工作。我搜索并尝试了这里、StackOverflow、nginx wiki 上建议的每一个“解决方案”,并且看到了大约 50 个不同的页面,其中介绍了人们让他们的“工作”的不同方法。

现在我知道 ngx_pagespeed 有自己的缓存,但我的网站似乎没有从中受益。所以我尝试设置自己的媒体 (jpg、gif 等)cache-control标题expire。但是,在过去的两天里,我得到的只是:

curl -I 的输出

HTTP/2 200
server: nginx
content-type: text/html; charset=UTF-8
vary: Accept-Encoding
x-frame-options: SAMEORIGIN
x-xss-protection: 1; mode=block
strict-transport-security: max-age=31536000
x-content-type-options: nosniff
date: Tue, 26 Jun 2018 02:02:46 GMT
x-page-speed: Powered By ngx_pagespeed
cache-control: max-age=0, no-cache

我对此感到非常沮丧。无论我怎么努力,我都无法得到任何东西,除了

cache-control: max-age=0, no-cache从卷曲。

以下是我的配置文件。我的站点的配置在,/etc/nginx/sites-available/conf而 nginx 的配置文件在/etc/nginx/nginx.conf

nginx.conf:

user  centos;
worker_processes  1;


#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    include /etc/nginx/sites-enabled/*;
    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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

     server {
        listen       80;
        server_name  localhost;
    }

我认为其余部分nginx.conf无关紧要。默认情况下,它们都被注释掉了。此文件可能有各种错误。我尝试在这里添加我发现的可以为其他人“修复”的问题的内容。

站点配置文件

# don't send the nginx version number in error pages and Server header
server_tokens off;

server {
    listen 80;
    server_name my_site;
    return 301 https://www.my_site.org;

    root /home/centos/site_folder/public;

    location / {
    add_header Cache-Control "public";
    expires 1d;
    proxy_pass http://site_ip:8080;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;

    #proxy_buffering on;
    #proxy_ignore_headers "Set-Cookie";
    #proxy_hide_header "Set-Cookie";

    proxy_buffering on;
    #proxy_ignore_headers Expires;
    #proxy_ignore_headers X-Accel-Expires;
    #proxy_ignore_headers Cache-Control;
    #proxy_ignore_headers Set-Cookie;

    #proxy_hide_header X-Accel-Expires;
    #proxy_hide_header Expires;
    #proxy_hide_header Cache-Control;
    #proxy_hide_header Pragma;

    }

    location ~*  \.(jpg|jpeg|png|gif|ico|css|js)$ {
    expires 15d;
    }


    location ~* \.(jpg|jpeg|gif|png)$ {
    expires 365d;
    }

    location ~* \.(pdf|css|html|js|swf)$ {
    expires 30d;
    }

}


server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
ssl_certificate /my/certs/are/here.crt;
ssl_certificate_key /my/certs/are/here.key;

# intermediate configuration
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;

# enable session resumption to improve https performance
    ssl_session_cache shared:SSL:50m;
    ssl_session_timeout 1d;
    ssl_session_tickets off;

    ssl_stapling on;
    ssl_stapling_verify on;
    ssl_trusted_certificate         
    /my/certs/are/here.crt;

    gzip on;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";
    gzip_proxied any;
    gzip_types text/plain text/css application/json         
    application/javascript application/x-javascript text/xml 
    application/xml application/xml+rss text/javascript image/x- 
    icon image/bmp image/svg+xml;
    gzip_vary on;
    gzip_comp_level 5;
    gzip_min_length 256;
    gzip_buffers 16 8k;

    include /etc/nginx/snippets/security-headers.conf;

    server_name my_site.org;
    access_log /var/log/nginx/nginx.vhost.access.log;
    error_log /var/log/nginx/nginx.vhost.error.log;
    location / {
    #root /home/centos/my_site.org/public;
    try_files $uri $uri/ /index.html?$args;
    index index.html;

    }

    location ~*\.(js|css|png|jpg|jpeg|gif|ico)$ {
    expires 30d;
    add_header Cache-Control "public, no-transform";
    }

    location /page1 {
    default_type "text/html";
    alias /home/centos/my_site.org/public/page1.html;
    }

    location /page2 {
    default_type "text/html";
    alias /home/centos/my_site.org/public/page2.html;
    }

    location ~* \. 
    (jpeg|jpg|JPG|gif|png|ico|cur|gz|svg|svgz|webp)$ {
    #root /home/centos/my_site.org/public;
    #include /etc/nginx/snippets/security-headers.conf;
    try_files $uri $uri/ /$1/$2 =404;
    #access_log off;
    #log_not_found off;
    }

    #location ~* \.(mp3|mpeg|mpg|mp4|ogg|ogv|webm|webp|htc)$ {        
    #root /home/centos/my_site.org/public;
    #include /etc/nginx/snippets/security-headers.conf;
    #try_files $uri $uri/ /$1/$2 =404;
    #proxy_cache_valid any 30d;
    #access_log off;
    #add_header Cache-Control "public";
    #}

    #location ~* \.(|css|js)$ {
    #expires 7d;
    #root /home/centos/my_site.org/public;
    #include /etc/nginx/snippets/security-headers.conf;
    #try_files $uri $uri/ /$1/$2 =404;
    #access_log off;
    #add_header Cache-Control "public";
    #}  

    ##
    # Pagespeed main settings

    pagespeed on;
    pagespeed FileCachePath /var/ngx_pagespeed_cache;   
    pagespeed FileCacheSizeKb            102400;
    pagespeed FileCacheCleanIntervalMs   3600000;
    pagespeed FileCacheInodeLimit        500000;
    pagespeed Domain https://www.my_site.org;
    pagespeed XHeaderValue "Powered By ngx_pagespeed";
    pagespeed HonorCsp on;
    pagespeed LowercaseHtmlNames on;
    #pagespeed EnableFilters extend_cache;
    #pagespeed ModifyCachingHeaders off;

    # Image related
    pagespeed EnableFilters rewrite_images;
    pagespeed EnableFilters convert_gif_to_png;
    pagespeed EnableFilters convert_jpeg_to_progressive;
    pagespeed EnableFilters strip_image_color_profile;
    pagespeed EnableFilters strip_image_meta_data;
    pagespeed EnableFilters jpeg_subsampling;
    pagespeed EnableFilters lazyload_images;
    pagespeed EnableFilters responsive_images,resize_images;
    pagespeed EnableFilters resize_mobile_images;
    pagespeed EnableFilters recompress_images;

    # code related
    pagespeed EnableFilters collapse_whitespace;

    # JS related
    pagespeed EnableFilters rewrite_javascript;
    pagespeed EnableFilters combine_javascript;

    # CSS related
    pagespeed EnableFilters rewrite_css;
    pagespeed EnableFilters prioritize_critical_css;
    pagespeed EnableFilters combine_css;
    pagespeed EnableFilters outline_css;
    pagespeed EnableFilters flatten_css_imports;

    # Ensure requests for pagespeed optimized resources go to the         
    pagespeed
    # handler and no extraneous headers get set.

    location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" 
    { add_header "" ""; }

    #location ~* \.(jpg|jpeg|gif|png|js|css)$ {
    #add_header Cache-Control "public, max-age=600";
    #}      

    location ~ "^/ngx_pagespeed_static/" { }
    location ~ "^/ngx_pagespeed_beacon" { }

    }

这些配置有效并且通过了,nginx -t但缓存就是不起作用。我保留了所有注释掉的代码,以便您可以看到我所做/尝试过的事情。任何帮助都将不胜感激。如果我遗漏了任何需要的详细信息,请告诉我。谢谢!

答案1

在一些指导下泰罗·基尔卡宁,我能够从托管媒体的 S3 存储桶中设置cache-control和标头,而不是从我的 nginx 网络服务器中设置。expires

谢谢你!

相关内容