
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:
- Verzeichnisse erstellen:
/etc/nginx/sites-available
und/etc/nginx/sites-enabled
weil sie irgendwie nicht existierten. - Verknüpfen Sie die erstellten Verzeichnisse hier:
/etc/nginx/nginx.conf
mit Hinzufügeninclude /etc/nginx/sites-enabled/*;
am Ende der Datei, aber vor dem letzten}
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; }
}
Verknüpfen der Conf-Datei:
ln -s /etc/nginx/sites-available/my-site.com.conf /etc/nginx/sites-enabled/my-site.com.conf
- Tun
service 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 -t
Folgendes 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.conf
aus dem conf.d
Ordner:http://pastebin.com/KUH2tSrD
Antwort1
Sie müssen die Caching-Direktiven zu Ihrer default.conf
Datei 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.conf
Datei.
Außerdem ist die root
Direktive innerhalb eines location
Blocks eine schlechte Praxis.
Ihr sollte also default.conf
so 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;
#}
}