Habilitar el almacenamiento en caché en nginx

Habilitar el almacenamiento en caché en nginx

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:

  1. creando directorios: /etc/nginx/sites-availabley /etc/nginx/sites-enabledporque de alguna manera no existían.
  2. Vinculando los directorios creados aquí: /etc/nginx/nginx.confagregando include /etc/nginx/sites-enabled/*;al final del archivo pero antes del último}
  3. 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;
    }
    

    }

  4. Vinculando el archivo conf:ln -s /etc/nginx/sites-available/my-site.com.conf /etc/nginx/sites-enabled/my-site.com.conf

  5. hacerservice 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 -tque 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.confde la conf.dcarpeta:http://pastebin.com/KUH2tSrD

Respuesta1

Debe agregar las directivas de almacenamiento en caché a su default.confarchivo 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.confarchivo.

Además, la rootdirectiva dentro de un locationbloque es una mala práctica.

Entonces, default.confdeberí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;
    #}
}

información relacionada