【发布时间】:2016-02-29 03:17:40
【问题描述】:
我有一个运行外部进程的 PHP 脚本。
外部进程输出一个 JSON 字符串,其中包含系统中不时变化的数据。
当我运行外部进程时,我可以清楚地看到 JSON 字符串是 每次运行都不一样。
但是,当我从网络浏览器使用 AJAX 调用我的 PHP 脚本时,在第一次调用 PHP 脚本后 JSON 字符串不会更新。
只有在我在网站内刷新页面或手动运行外部进程时才会更新 JSON 字符串。
为什么会这样?
LIGHTTPD 是否缓存输出?
编辑:
这是我的 LIGHTTPD 配置文件内容:
server.modules = (
"mod_access",
"mod_cgi",
"mod_alias",
"mod_compress",
"mod_redirect",
# "mod_rewrite",
)
server.document-root = "/var/www"
server.upload-dirs = ( "/var/cache/lighttpd/uploads" )
server.errorlog = "/var/log/lighttpd/error.log"
server.pid-file = "/var/run/lighttpd.pid"
server.username = "www-data"
server.groupname = "www-data"
server.port = 80
index-file.names = ( "index.php", "index.html", "index.lighttpd.html" )
url.access-deny = ( "~", ".inc" )
static-file.exclude-extensions = ( ".php", ".pl", ".fcgi" )
compress.cache-dir = "/var/cache/lighttpd/compress/"
compress.filetype = ( "application/javascript", "text/css", "text/html", "text/plain" )
# default listening port for IPv6 falls back to the IPv4 port
include_shell "/usr/share/lighttpd/use-ipv6.pl " + server.port
include_shell "/usr/share/lighttpd/create-mime.assign.pl"
include_shell "/usr/share/lighttpd/include-conf-enabled.pl"
$HTTP["url"] =~ "/cgi-bin/" {
cgi.assign = ( "" => "" )
}
cgi.assign = (
".cgi" => ""
)
编辑
According to the following Google Product thread 一个叫johnjbarton 的人提到以下内容:
好的,我与 Chrome 缓存专家进行了交谈。我的问题是非常 可能在此错误中进行了描述:错误 30862 - 动态插入 即使包含的文档被重新验证,子资源也不会被重新验证 重新加载https://bugs.webkit.org/show_bug.cgi?id=30862
强制刷新仅强制加载外部页面中的资源。它 不会强制刷新 XHR 加载的资源。这几乎是 dojo 应用程序中的所有资源。
当我查看 Network 部分下的 chrome 控制台时,它说
我的网站发出的所有请求都是 XHR 类型的。
这有什么关系吗?
编辑
我的请求的标题是
Accept */*
Content-Type application/x-www-form-urlencoded
非常重要的编辑 我编辑了我的 PHP 脚本,以便它将执行过程的结果附加到日志文件中。
原来进程的输出总是一样的。
但是,当我手动运行该过程时,它会按预期不断变化。
在我看来,LIGHTTPD 似乎在缓存它执行的进程的输出。
编辑
这是我的 PHP 脚本的样子:
<?php
session_start();
if(!isset($_SESSION['user'])){
header("Location: /");
} else {
header('Content-type: application/json');
$output = shell_exec('/home/debian/myProcess/myProcess');
file_put_contents("/var/log/lighttpd/access.log", $output, FILE_APPEND | LOCK_EX);
echo $output;
}
?>
【问题讨论】:
-
你能发布 lighttpd 配置的相关部分吗?
-
在您的浏览器中进行 AJAX 调用时检查网络流量,结果很可能缓存在您的浏览器中。检查您用于 AJAX 的任何 JS 框架,了解如何避免缓存 AJAX 响应,如果不使用框架,您将不得不例如为每个查询 URL 添加时间戳。
-
@Eborbob 哦,好的。那么如何添加时间戳以及在哪里添加此时间戳?我是在客户端还是服务器端添加它?您对我可以在哪里阅读更多相关信息有什么建议吗?最后:为什么时间戳会改变浏览器缓存的行为?
-
@Eborbob 你的意思是像这个 SO 上的答案 #2 吗? stackoverflow.com/questions/367786/prevent-caching-of-ajax-call
-
假设您对
http://example.com/ajax.php?action=ping进行了AJAX 调用,以防止浏览器缓存响应,您可以改为调用http://example.com/ajax.php?action=ping&nocache=1456765882。由于此 URL 不同,浏览器会将请求发送到服务器,而不是从其缓存中获取内容。
标签: php json ajax lighttpd shell-exec