Nginx 似乎缓存了我的 PHP 文件,因为即使我在服务器上更改了文件,它仍然提供相同的 PHP 页面。
我在用:
- Digitalocean 的 Ubuntu 16.04 Droplet
- nginx/1.10.0(正常 LEMP一键安装)
- PHP 7.0.13-0ubuntu0.16.04.1
我尝试了不同的机器,问题仍然存在。
我在 Apache2 上进行了测试,php 文件按预期更新。因此我得出结论,问题出在 Nginx 上。
调试过程:新服务器和全新 LEMP 安装:
- 编辑 nginx.conf改为
sendfile on
,sendfile off
根据本网站.然后使用以下命令重新加载 nginx 配置nginx -s reload
(参见nginx.conf
下面的文件) - 创建新的服务器块对于
test.mypersonalsite.com
(请参阅下面的服务器块文件) - 链接服务器块到
sites-enabled
目录
sudo ln -s /etc/nginx/sites-available/test.mypersonalsite.com /etc/nginx/sites-enabled/
检查并重启 nginx然后
nginx -t
sudo systemctl restart nginx
创建index.html和索引.php部署到服务器(完美运行)
到这里之前一切都很好,过了这里就出问题了
- 更新索引.html和索引.php从 v1.0.7 到 v1.0.8
更改后index.php
不会重新加载。虽然可以index.html
。
这意味着,如果我通过浏览器请求该网站,我会得到旧的 php 文件,而不是服务器上的新文件。
如果我通过 ssh 进入服务器和sudo nano
php 文件,它仍然没有更新。它似乎php
被缓存在服务器上。
我尝试从不同的机器访问该网站。同样的问题。
问题
我在网上搜索了很多。大多数解决方案都围绕着将 nginx 配置设置为sendfile off
,我这样做了,但问题仍然存在。
如何让 Nginx 重新加载我更新的 php 文件?
最重要的文件:
nginx.conf
user www-data;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile off;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}
服务器块:test.mypersonalsite.com
# Default server configuration
#
server {
listen 80;
listen [::]:80;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name test.mypersonalwebsite.com;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# 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;
}
}
初始 index.html v1.0.7
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Nginx Test v1.0.7</title>
</head>
<body>
<h1>Nginx Test v1.0.7</h1>
<a href="/public" title="">Go to PHP file</a>
</body>
</html>
初始 index.php v1.0.7
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>v1.0.7 - <?php echo "That's an nginx Test." ?></title>
<link rel="stylesheet" href="">
</head>
<body>
<h1><?= "v1.0.7 - Just Testing PHP on Nginx." ?></h1>
<a href="../" title="">Return to Index</a>
<h4>Random PHP Generated Number:
<?=
rand(1, 100);
?>
</h4>
</body>
</html>
更新 index.html 至 v1.0.8(重新加载)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Nginx Test v1.0.8</title>
</head>
<body>
<h1>Nginx Test v1.0.8</h1>
<a href="/public" title="">Go to PHP file</a>
</body>
</html>
已将 index.php 更新至 v1.0.8(无需重新加载)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>v1.0.8 - <?php echo "That's an nginx Test." ?></title>
<link rel="stylesheet" href="">
</head>
<body>
<h1><?= "v1.0.8 - Just Testing PHP on Nginx." ?></h1>
<a href="../" title="">Return to Index</a>
<h4>Random PHP Generated Number:
<?=
rand(1, 100);
?>
</h4>
</body>
</html>
答案1
您正在重启错误的服务。PHP 7 现在在 fpm 服务中缓存 php 文件(nginx 本身不处理 PHP 文件)。根据您的操作系统和 php 发行版,以下方法之一应该可以解决此问题:
service php-fpm restart # most centos
service php7-php-fpm restart # centos and remi php7
service php7.0-fpm restart # ubuntu