Aktivieren Sie das Caching auf nginx

Aktivieren Sie das Caching auf nginx

Ich habe einen eigenen VPS mit CentOS 6 und nginx und möchte Caching aktivieren. Um zu testen, ob es erfolgreich aktiviert ist, verwende ich Google PageSpeed ​​Insight. Mein Problem ist, dass ich nicht viel Erfahrung damit habe, Caching zu aktivieren und einzustellen, wie lange z. B. ein Bild im Cache gespeichert wird usw. Folgendes habe ich im Internet gefunden und bisher ausprobiert:

  1. Verzeichnisse erstellen: /etc/nginx/sites-availableund /etc/nginx/sites-enabledweil sie irgendwie nicht existierten.
  2. Verknüpfen Sie die erstellten Verzeichnisse hier: /etc/nginx/nginx.confmit Hinzufügen include /etc/nginx/sites-enabled/*;am Ende der Datei, aber vor dem letzten}
  3. Erstellen der Datei /etc/nginx/sites-available/my-site.com.conf:

    server {
    listen       80;
    server_name  localhost;
    
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    
    location ~*  \.(jpg|jpeg|png|gif|ico|css|js)$ {
        expires 15d;
    }
    
    location ~*  \.(pdf)$ {
        expires 30d;
    }
    

    }

  4. Verknüpfen der Conf-Datei:ln -s /etc/nginx/sites-available/my-site.com.conf /etc/nginx/sites-enabled/my-site.com.conf

  5. Tunservice nginx restart

Ich verwende für meine Website WordPress.

Wenn ich meine Seite also mit PageSpeed ​​Insight oder anderen PageSpeed-Tools teste, wird angezeigt, dass ich kein Caching für meine header.png, Javascripts usw. verwende. Aber ich erhalte keine Fehler, selbst wenn ich die Konfigurationsdateien überprüfe, die nginx -tFolgendes anzeigen:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Habe ich etwas vergessen?

Dies ist meine vollständige Nginx-Konfiguration:http://pastebin.com/wxnzzePT

Das default.confaus dem conf.dOrdner:http://pastebin.com/KUH2tSrD

Antwort1

Sie müssen die Caching-Direktiven zu Ihrer default.confDatei hinzufügen und die neu erstellte Datei entfernen.

Ihre neue Datei wird nur verwendet, wenn Benutzer die Site mithilfe von besuchen http://localhost. Darüber hinaus verwendet Ihre neue Dateikonfiguration andere Pfade als Ihre default.confDatei.

Außerdem ist die rootDirektive innerhalb eines locationBlocks eine schlechte Praxis.

Ihr sollte also default.confso aussehen:

#
# The default server
#
server {
    listen       80 default_server;
    server_name  213.165.xx.xx;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    root   /var/www/wordpress;

    location / {
        index  index.html index.htm index.php;

        try_files $uri $uri/ /index.php?q=$request_uri;

    }

    location ~*  \.(jpg|jpeg|png|gif|ico|css|js)$ {
        expires 15d;
    }

    location ~*  \.(pdf)$ {
        expires 30d;
    }

    location /admin {
        auth_basic "Administrator Login";
        auth_basic_user_file /var/www/admin/.htpasswd;
    }

    #!!! IMPORTANT !!! We need to hide the password file from prying eyes
    # This will deny access to any hidden file (beginning with a .period)
    location ~ /\. { deny  all; }

    error_page  404              /404.html;
    location = /404.html {
        root   /usr/share/nginx/html;
    }

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }


    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root           /var/www/wordpress;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

verwandte Informationen