3

I have my own VPS with CentOS 6 and nginx and I want to enable caching. To test it, if it is enabled successfully I use the Google PageSpeed Insight. My problem is that I don't have much experience where I have to enable caching and where I can set how long an image for example is cached and so on. Thats what I found on the internet and tried so far:

  1. creating directories: /etc/nginx/sites-available and /etc/nginx/sites-enabled because they didn't exist somehow.
  2. Linking the created directories here: /etc/nginx/nginx.conf with adding include /etc/nginx/sites-enabled/*; at the end of the file but before the last }
  3. Creating the file /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. Linking the conf file: ln -s /etc/nginx/sites-available/my-site.com.conf /etc/nginx/sites-enabled/my-site.com.conf

  5. do service nginx restart

I use my website for WordPress.

So whenever I test my page with PageSpeed Insight or other pagespeed tools it says that I don't use caching for my header.png, javascripts and so on. But I dont get some errors, even if I check the configuration files with nginx -t which shows this:

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

Do I have forgotten something?

This is my complete nginx config: http://pastebin.com/wxnzzePT

The default.conf from the conf.d folder: http://pastebin.com/KUH2tSrD

  • Please show your complete nginx configuration, there are quite certainly other parts of the configuration that affect things. And also, you should have used your Linux distribution's native way of adding to nginx configuration. The sites-enabled scheme is used in Debian / Ubuntu systems, and is there automatically when you have installed the nginx packets. – Tero Kilkanen Mar 3 '15 at 23:54
  • Thanks for your reply, I added my nginx config in my original post. How can I add this to the nxing config with CentOS? – Peter Mar 4 '15 at 0:01
  • I am not familiar with CentOS way of doing nginx configuration. – Tero Kilkanen Mar 4 '15 at 0:04
  • Most likely the configuration is done in the conf.d directory. Please also show the contents of default.conf in conf.d. – Tero Kilkanen Mar 4 '15 at 0:05
  • They say here that I can just create those directories 'by hand' but of course I don't know if this is right. I added the default.conf file. I hope this helps. – Peter Mar 4 '15 at 0:11
3

You need to add the caching directives to your default.conf file, and remove this new file you created.

Your new file is only used when users visit the site using http://localhost. In addition, your new file configuration is using different paths compared to your default.conf file.

Also, the root directive inside a location block is bad practice.

So, your default.conf should look like this:

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

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.