
Tengo mi propio VPS con CentOS 6 y nginx y quiero habilitar el almacenamiento en caché. Para probarlo, si se habilita correctamente, utilizo Google PageSpeed Insight. Mi problema es que no tengo mucha experiencia en dónde tengo que habilitar el almacenamiento en caché y dónde puedo configurar durante cuánto tiempo se almacena en caché una imagen, por ejemplo, etc. Eso es lo que encontré en Internet y probé hasta ahora:
- creando directorios:
/etc/nginx/sites-available
y/etc/nginx/sites-enabled
porque de alguna manera no existían. - Vinculando los directorios creados aquí:
/etc/nginx/nginx.conf
agregandoinclude /etc/nginx/sites-enabled/*;
al final del archivo pero antes del último}
Creando el archivo
/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; }
}
Vinculando el archivo conf:
ln -s /etc/nginx/sites-available/my-site.com.conf /etc/nginx/sites-enabled/my-site.com.conf
- hacer
service nginx restart
Utilizo mi sitio web para WordPress.
Entonces, cada vez que pruebo mi página con PageSpeed Insight u otras herramientas de velocidad de página, dice que no uso el almacenamiento en caché para mi header.png, javascripts, etc. Pero no recibo algunos errores, incluso si reviso los archivos de configuración nginx -t
que muestran esto:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
¿Me he olvidado algo?
Esta es mi configuración completa de nginx:http://pastebin.com/wxnzzePT
El default.conf
de la conf.d
carpeta:http://pastebin.com/KUH2tSrD
Respuesta1
Debe agregar las directivas de almacenamiento en caché a su default.conf
archivo y eliminar este nuevo archivo que creó.
Su nuevo archivo solo se usa cuando los usuarios visitan el sitio usando http://localhost
. Además, la configuración de su nuevo archivo utiliza rutas diferentes en comparación con su default.conf
archivo.
Además, la root
directiva dentro de un location
bloque es una mala práctica.
Entonces, default.conf
deberías lucir así:
#
# 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;
#}
}