今天我遇到了瀏覽器緩存靜態文件的問題。用Apache/nginx重寫規則和Expires-Header
我在運行Apache和nGinx的Plesk服務器上有一個Prestashop網站。
的Prestashop生成與Apache重寫URL規則.htaccess文件:
RewriteRule ^([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$1$2$3.jpg [L]
RewriteRule ^([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$1$2$3$4.jpg [L]
RewriteRule ^([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$1$2$3$4$5.jpg [L]
RewriteRule ^([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$4/$1$2$3$4$5$6.jpg [L]
RewriteRule ^([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$4/$5/$1$2$3$4$5$6$7.jpg [L]
RewriteRule ^([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$4/$5/$6/$1$2$3$4$5$6$7$8.jpg [L]
RewriteRule ^([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$4/$5/$6/$7/$1$2$3$4$5$6$7$8$9.jpg [L]
RewriteRule ^([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$4/$5/$6/$7/$8/$1$2$3$4$5$6$7$8$9$10.jpg [L]
而且NGINX成立於到期頭添加到靜態文件:
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires 30d;
add_header Pragma public;
add_header Cache-Control "public";
try_files $uri @fallback;
}
這將適用於靜態文件他們的網址並未被Apache重寫:
url示例:http://www.domain.tld/modules/homeslider/images/image_name.jpg
Server: nginx Date: Sun, 20 Nov 2016 19:32:33 GMT Content-Type: image/jpeg Content-Length: 50616 Last-Modified: Thu, 17 Nov 2016 14:47:22 GMT Connection: keep-alive ETag: "582dc2fa-c5b8" Expires: Tue, 20 Dec 2016 19:32:33 GMT Cache-Control: max-age=2592000 Pragma: public Cache-Control: 「public」 Accept-Ranges: bytes
而不是產品的圖片,他們的網址是重寫了由Apache規則:
網址例如:http://www.domain.tld/156-home_default/image_name.jpg
Server: nginx Date: Sun, 20 Nov 2016 19:34:11 GMT Content-Type: image/jpeg Content-Length: 7587 Connection: keep-alive Last-Modified: Thu, 17 Nov 2016 21:14:27 GMT Accept-Ranges: bytes X-Powered-By: PleskLin
的問題是:如何使nginx expires-header應用於通過Apache Rewrite規則重寫的文件url?
在此先感謝。
編輯
服務器管理是12.5的Plesk處理。
nGinx用於處理靜態文件。我不太瞭解它的配置。
在Plesk domain.tld - > nGinx配置中,我選中了「智能靜態文件處理」和「由nginx直接提供靜態文件」。在「其他nginx指令」中,我添加了過期標題代碼。這就是我所做的所有修改。這是默認的plesk配置。
重新編輯
nginx的虛擬主機配置文件:
#ATTENTION!
#
#DO NOT MODIFY THIS FILE BECAUSE IT WAS GENERATED AUTOMATICALLY,
#SO ALL YOUR CHANGES WILL BE LOST THE NEXT TIME THE FILE IS GENERATED.
server {
listen xxx.xxx.xxx.xxx:443 ssl;
server_name domain.tld;
server_name www.domain.tld;
server_name ipv4.domain.tld;
ssl_certificate /opt/psa/var/certificates/certP286MBP;
ssl_certificate_key /opt/psa/var/certificates/certP286MBP;
client_max_body_size 128m;
root "/var/www/vhosts/domain.tld/httpdocs";
access_log "/var/www/vhosts/system/domain.tld/logs/proxy_access_ssl_log";
error_log "/var/www/vhosts/system/domain.tld/logs/proxy_error_log";
location/{
proxy_pass https://xxx.xxx.xxx.xxx:7081;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
access_log off;
}
location @fallback {
proxy_pass https://xxx.xxx.xxx.xxx:7081;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
access_log off;
}
location ~ ^/plesk-stat/ {
proxy_pass https://xxx.xxx.xxx.xxx:7081;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
access_log off;
}
location ~ ^/(.*\.(ac3|avi|bmp|bz2|cue|dat|doc|docx|dts|eot|exe|flv|gz|htm|html|img|iso|mkv|mp3|mp4|mpeg|mpg|ogg|pdf|ppt|pptx|qt|rar|rm|svg|swf|tar|tgz|ttf|txt|wav|woff|woff2|xls|xlsx|zip))$ {
try_files $uri @fallback;
}
include "/var/www/vhosts/system/domain.tld/conf/vhost_nginx.conf";
}
server {
listen xxx.xxx.xxx.xxx:80;
server_name domain.tld;
server_name www.domain.tld;
server_name ipv4.domain.tld;
client_max_body_size 128m;
root "/var/www/vhosts/domain.tld/httpdocs";
access_log "/var/www/vhosts/system/domain.tld/logs/proxy_access_log";
error_log "/var/www/vhosts/system/domain.tld/logs/proxy_error_log";
location/{
proxy_pass http://xxx.xxx.xxx.xxx:7080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
access_log off;
}
location @fallback {
proxy_pass http://xxx.xxx.xxx.xxx:7080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
access_log off;
}
location ~ ^/plesk-stat/ {
proxy_pass http://xxx.xxx.xxx.xxx:7080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
access_log off;
}
location ~ ^/(.*\.(ac3|avi|bmp|bz2|cue|dat|doc|docx|dts|eot|exe|flv|gz|htm|html|img|iso|mkv|mp3|mp4|mpeg|mpg|ogg|pdf|ppt|pptx|qt|rar|rm|svg|swf|tar|tgz|ttf|txt|wav|woff|woff2|xls|xlsx|zip))$ {
try_files $uri @fallback;
}
include "/var/www/vhosts/system/domain.tld/conf/vhost_nginx.conf";
}
你能提供一些更多的背景下,像全樣本URL和更nginx的配置?另外,我猜想nginx是使用Apache作爲上游路由請求的嗎? –
@JanPapenbrock,感謝您的回覆。我編輯了答案。 – JazZ
上面的兩個例子都有「Server:nginx」的回覆,所以這些都是由nginx提供的。你需要的是放置你的位置〜*。(js | css | png | jpg | jpeg | gif | ico)$ {早於你的nginx配置文件,因爲順序很重要。 – peixotorms