Nginx 記憶體控制/過期標頭將不起作用
我到處尋找這個問題的有效答案,但似乎沒有一個答案有效。我也很抱歉這篇超長的文章,但我只需要在這方面尋求幫助。我試圖盡可能詳細。
我在從原始碼建構的 CentOS 7 上安裝了 ngx_pagespeed 的 nginx 網路伺服器,在我的一生中,我無法獲取
cache-control
或expire
標頭工作。我在這裡、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
,但記憶體不起作用。我留下了所有註釋掉的程式碼,所以你可以看到我做了什麼/嘗試了什麼。在這裡的任何幫助將不勝感激。如果我錯過了任何需要的細節,請告訴我。謝謝!
在Tero Kilkanen的一些指導下,我能夠從託管媒體的 S3 儲存桶中設置
cache-control
和expires
標頭,而不是從我的 nginx 網路伺服器。謝謝!