我今天遇到浏览器缓存静态文件的问题。
我在运行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设置为将expires-header添加到静态文件中:
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的静态文件:
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规则重写:
url示例: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?
提前致谢。
编辑
服务器管理由Plesk 12.5处理。
nGinx用于处理静态文件。我对它的配置知之甚少。
在Plesk domain.tld - >在Nginx配置中,我检查了“智能静态文件处理”和“由Nginx直接提供静态文件”。在“其他Nginx指令”中,我添加了expires-header代码。这就是我对此所做的所有修改。它是默认的plesk配置。
重新编辑
nginx vhost配置文件:
#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";
}
答案 0 :(得分:1)
我想出了如何解决这个问题。不是一个大谜......
我确实将重写规则从apache转换为nginx并忽略了.htaccess。
这就是诀窍。
新的"附加nginx指令" :
# Deny all for hidden files/directory (.htaccess, .htpasswd, etc...)
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
rewrite ^/api/?(.*)$ /webservice/dispatcher.php?url=$1 last;
rewrite ^/([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$1$2$3.jpg last;
rewrite ^/([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$1$2$3$4.jpg last;
rewrite ^/([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$1$2$3$4$5.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$1$2$3$4$5$6.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$1$2$3$4$5$6$7.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$1$2$3$4$5$6$7$8.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$7/$1$2$3$4$5$6$7$8$9.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$7/$8/$1$2$3$4$5$6$7$8$9$10.jpg last;
rewrite ^/c/([0-9]+)(\-[\.*_a-zA-Z0-9-]*)(-[0-9]+)?/.+\.jpg$ /img/c/$1$2$3.jpg last;
rewrite ^/c/([a-zA-Z_-]+)(-[0-9]+)?/.+\.jpg$ /img/c/$1$2.jpg last;
rewrite ^/images_ie/?([^/]+)\.(jpe?g|png|gif)$ /js/jquery/plugins/fancybox/images/$1.$2 last;
try_files $uri $uri/ /index.php$is_args$args;
error_page 404 /index.php?controller=404;
location ~* \.(gif)$ {
expires 2592000s;
}
location ~* \.(jpeg|jpg)$ {
expires 2592000s;
}
location ~* \.(png)$ {
expires 2592000s;
}
location ~* \.(css)$ {
expires 604800s;
}
location ~* \.(js|jsonp)$ {
expires 604800s;
}
location ~* \.(js)$ {
expires 604800s;
}
location ~* \.(ico)$ {
expires 31536000s;
}
location ~* \.(woff)$ {
expires 2592000s;
}