Blog

ISPManager, nginx and expires

Learn how to configure Expires headers in nginx when using ISPManager to enhance your site's caching efficiency.

Dmytro
Expires headers caching nginx configuration web performance ISPManager setup static content

Is your server using the popular software stack of ISPManager control panel, Apache web server, and nginx as a www accelerator? Note that nginx does not return Expires headers by default. While it might not always be critical for documents generated by the PHP interpreter, serving static data with such a header is desirable, allowing visitors’ browsers to cache content more effectively.

There are two methods to specify the Expires header:

  1. Per Site Configuration: If you have a few sites, open nginx.conf in a text editor (for Linux systems, it is usually located in /etc/nginx/; for FreeBSD, it is in /usr/local/etc/nginx/). Locate the server {} block for your virtual host. In this block, find the location section for processing static documents and add expires Xd, replacing X with the desired number of days. For instance, expires 7d instructs the browser to cache images and CSS/JS files for one week.

    Your location block should look like this:

    location ~* ^.+\.(jpg|jpeg|gif|png|svg|js|css|mp3|ogg|mpe?g|avi|zip|gz|bz2?|rar|swf)$ {
        root /var/www/username/data/www/usernamesite.ru;
        access_log /var/www/nginx-logs/usernamesite isp;
        access_log /var/www/httpd-logs/usernamesite.ru.access.log;
        error_page 404 = @fallback;
        expires 7d;
    }
    
  2. Global Configuration for All Virtual Servers: To have the Expires parameter set for static files across all virtual servers, create the file /usr/local/ispmgr/etc/server.templ with the following content:

    location ~* ^.+\.(jpg|jpeg|gif|png|svg|js|css|mp3|ogg|mpe?g|avi|zip|gz|bz2?|rar|swf)$ {
        expires 7d;
    }
    

    After making this change, reload the panel using killall ispmgr, create a new website via ISPManager, and check that the Expires parameter is correctly configured in the newly created site’s nginx.conf.

Documentation Links:

Need Help?

Our support team is available 24/7 to assist you with any questions or issues.

Contact Support