Nginx Server "caches" PHP. Updated Page does not Reload

Nginx seems to cache my PHP files, because it serves the same PHP page even though I changed the file on the server.

I am using:

  • Ubuntu 16.04 Droplet by Digitalocean
  • nginx/1.10.0 (Normal LEMP One-Click installation)
  • PHP 7.0.13-0ubuntu0.16.04.1

I tried different machines, problem persists.

I tested on Apache2 and the php file was updating as expected. Therefore I conclude that the problem lies with Nginx.

Debugging Process: New Server & Fresh LEMP Installation:

  1. Edit the nginx.conf to change sendfile on to sendfile off, according to this site. Then reload nginx configuration with nginx -s reload (see nginx.conf file below)
  2. Create new server block for test.mypersonalsite.com (see server block file below)
  3. Link server block to sites-enabled directory

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

  1. Check and Restart nginx with nginx -t and then sudo systemctl restart nginx

  2. Create index.html and index.php deploy to server (Works perfect)

Until here everything works fine, after here it bugs

  1. Update index.html and index.php from v1.0.7 to v1.0.8

After the change the index.php doesn't reload. Though the index.html does.

This means, if I request the site over my browser, I get the old php file, instead of the new one that is on the server.

If I ssh into the server and sudo nano the php file, it still does not update. It seems like php is cached on the server.

I tried accessing the site from different machines. Same problem.

Question

I searched the web a lot. Most solutions revolve around setting the nginx configuration to sendfile off, which I did and the problem still persists.

How do I make Nginx reload my updated php files?

The most important files:

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;
#   }
#}

Server Block: 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;
    }
}

Initial 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>

Initial 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>

Updated index.html to v1.0.8 (reloading)

<!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>

Updated index.php to v1.0.8 (NOT reloading)

<!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>

Solution 1:

You're restarting the wrong service. PHP 7 now caches php files in the fpm service (nginx does not itself process PHP files). Depending on your OS and distribution of php one of the following should fix the issue:

service php-fpm restart # most centos
service php7-php-fpm restart # centos and remi php7
service php7.0-fpm restart # ubuntu