Nginx 服务器“缓存”PHP。更新的页面不会重新加载

Lov*_*ess 5 nginx cache

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 安装:

  1. 根据此站点编辑 nginx.conf以更改sendfile on为。然后重新加载nginx配置(见下面的文件)sendfile offnginx -s reloadnginx.conf
  2. 创建新的服务器块test.mypersonalsite.com(见下面的服务器块文件)
  3. 将服务器块链接sites-enabled目录

sudo ln -s /etc/nginx/sites-available/test.mypersonalsite.com /etc/nginx/sites-enabled/

  1. 检查并重新启动nginxnginx -t然后sudo systemctl restart nginx

  2. 创建 index.htmlindex.php部署到服务器(完美运行)

直到这里一切正常,在这里之后它会出错

  1. index.htmlindex.php从 v1.0.7更新到 v1.0.8

更改后index.php不会重新加载。虽然index.html确实如此。

这意味着,如果我通过浏览器请求该站点,我会得到旧的 php 文件,而不是服务器上的新文件。

如果我 ssh 进入服务器和sudo nanophp 文件,它仍然不会更新。好像php缓存在服务器上。

我尝试从不同的机器访问该站点。同样的问题。

我在网上搜索了很多。大多数解决方案都围绕将 nginx 配置设置为sendfile off,我这样做了,但问题仍然存在。

如何让 Nginx 重新加载我更新的 php 文件?

最重要的文件:

配置文件

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;
#   }
#}
Run Code Online (Sandbox Code Playgroud)

服务器块: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;
    }
}
Run Code Online (Sandbox Code Playgroud)

初始 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>
Run Code Online (Sandbox Code Playgroud)

初始 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>
Run Code Online (Sandbox Code Playgroud)

将 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>
Run Code Online (Sandbox Code Playgroud)

将 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>
Run Code Online (Sandbox Code Playgroud)

2ps*_*2ps 6

您正在重新启动错误的服务。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
Run Code Online (Sandbox Code Playgroud)