遇到这个问题.. nginx 似乎会保留不再在磁盘上的文件,这不是浏览器缓存问题,因为我可以用 curl 进行测试。
例如.. curlhttps://staging.xxxx.co/
<!DOCTYPE html><ht ... src="/app/main.92974ab5ce4e059cd6a0.js ..
该文件不再存在于磁盘上..
重新启动可以解决问题 - 例如 curlhttps://staging.xxxx.co/
<!DOCTYPE html><ht ... src="/app/main.5920b750cb8af60be300.js ..
我的配置文件
# HTTPS server
server {
ssl on;
ssl_certificate /etc/letsencrypt/live/db.xxxx.co/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/db.xxxx.co/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
#ssl_dhparam /etc/ssl/dhparams.pem;
#ssl_session_timeout 5m;
#ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH";
#ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
#ssl_prefer_server_ciphers on;
# Remove the # from the following line once you are sure everything works
# add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;
add_header X-Clacks-Overhead "GNU Terry Pratchett";
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.4.4 8.8.8.8 valid=300s;
resolver_timeout 5s;
listen 443 ssl;
listen [::]:443 ssl;
server_name staging.xxxx.co;
root /var/www/staging.xxxx/current/public;
index index.html index.htm index.php;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
# try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php?$query_string;
expires 1;
# Do not cache index.html, SPA
location = /app/index.html {
add_header Cache-Control 'no-store, no-cache, must-revalidate, proxy-revalidate, max
-age=0';
proxy_no_cache 1;
expires 1;
try_files /app/index.html =404;
}
# Do not cache sw.js, required for offline-first updates.
location /sw.js {
add_header Cache-Control 'no-store, no-cache, must-revalidate, proxy-revalidate, max
-age=0';
proxy_cache_bypass $http_pragma;
proxy_cache_revalidate on;
expires 1;
access_log off;
}
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# include snippets/fastcgi-php.conf;
#
# # With php7.0-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php7.0-fpm:
# fastcgi_pass unix:/run/php/php7.0-fpm.sock;
#}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
expires 1;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
location ~ /\.ht {
deny all;
}
}
有什么建议吗?我已经检查过了Nginx 似乎正在发送页面的缓存版本
Linux 服务器是 ubuntu 16.04 - 最新版、nginx/1.10.3 (Ubuntu)、php 7.2
答案1
我通过在 /etc/php/7.2/fpm/php.ini 中禁用 opcache 解决了这个问题:
opcache.enable=0
更新:
正如 RobIII 在评论中正确指出的那样,opcache.enable=0
这将禁用 opcache,而您应该在生产服务器上启用它。对于生产,您应该使用 systemctl 或服务命令重新加载 php-fpm:
systemctl reload php-fpm
或者使用应用程序目录中的 php 脚本(/var/www/html
默认情况下或在 OP 的情况下/var/www/staging.xxxx/current/public
),然后您可以在需要时从浏览器调用它:
<?php
opcache_reset();
或者,更优雅的是,使用命令行中的自定义 bash 命令(借用自php.net 评论):
#!/bin/bash
WEBDIR=/var/www/html/
RANDOM_NAME=$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 13)
echo "<?php opcache_reset(); ?>" > ${WEBDIR}${RANDOM_NAME}.php
curl http://localhost/${RANDOM_NAME}.php
rm ${WEBDIR}${RANDOM_NAME}.php
将其放入 /usr/local/bin/opcache-clear 并使其可执行。当我想清除缓存时,我只需在终端内运行“opcache-clear”。