ociruby

Configure and use Varnish(TM) with Apache

NOTE: We are in the process of modifying the file structure and configuration for many Bitnami stacks. On account of these changes, the file paths stated in this guide may change depending on whether your Bitnami stack uses native Linux system packages (Approach A), or if it is a self-contained installation (Approach B). To identify your Bitnami installation type and what approach to follow, run the command below:

 $ test ! -f "/opt/bitnami/common/bin/openssl" && echo "Approach A: Using system packages." || echo "Approach B: Self-contained installation."

The output of the command indicates which approach (A or B) is used by the installation, and will allow you to identify the paths, configuration and commands to use in this guide. Refer to the FAQ for more information on these changes.

Varnish™ is a web application accelerator (also known as a caching HTTP reverse proxy) that is installed and configured in front of any HTTP server and takes care of caching its contents. Varnish™ is fast, typically speeding up delivery with a factor of 300-1000x depending on the architecture.

Varnish™ requires a working compiler (such as gcc) to compile its configuration file, which is then dynamically linked into the server process.

Enable Varnish™

Varnish™ is disabled by default, so it cannot be started using the control script. To enable it, follow the steps below:

  • Enable the Varnish™ service. Depending on your system type, run the following commands:

    • Approach A (Bitnami installations using system packages):

      $ sudo mv /etc/monit/conf.d/varnish.conf.disabled /etc/monit/conf.d/varnish.conf
      $ sudo gonit reload
      
    • Approach B (Self-contained Bitnami installations):

      $ sudo mv /opt/bitnami/varnish/scripts/ctl.sh.disabled /opt/bitnami/varnish/scripts/ctl.sh
      $ sudo /opt/bitnami/ctlscript.sh start varnish
      
  • Since Varnish™ with PageSpeed is not currently supported by Bitnami, ensure PageSpeed is disabled. Follow this guide for more information on this.

Start Varnish™

By default, Varnish™ is configured to use TCP port 81. Follow these steps to start the Varnish™ service:

  • Execute these commands at the server console:

    $ sudo /opt/bitnami/ctlscript.sh start varnish
    
  • Ensure Apache is running:

    $ sudo /opt/bitnami/ctlscript.sh start apache
    

Now you should be able to access the index page on both ports 80 and 81, for Apache and Varnish™, respectively.

  • Accessing the server through the 80 port will behave as if Varnish™ was not enabled, retrieving all the data from the server.

  • Accessing the server through the 81 port will use Varnish™ as a reverse proxy, serving cached contents and requesting Apache for non-cached content.

Check Varnish™ status

Check what Varnish™ is doing under the hood with the varnishlog command. To indicate which instance of Varnish™ you are interested in, specify the Varnish™ working directory which is located by default at /opt/bitnami/varnish/var/varnish/.

0 CLI          - Rd ping
0 CLI          - Wr 200 19 PONG 1340840690 1.0
0 CLI          - Rd ping
0 CLI          - Wr 200 19 PONG 1340840693 1.0
0 CLI          - Rd ping
0 CLI          - Wr 200 19 PONG 1340840696 1.0

If you visit your server URL though the configured Varnish™ port (81 in our example), you will see a more interesting output message:

15 Hash         c /favicon.ico
15 Hash         c 75.101.208.108
15 VCL_return   c hash
15 VCL_call     c pass pass
15 Backend      c 14 default default
15 TTL          c 1976586397 RFC 120 -1 -1 1340840847 0 1340840847 0 0
15 VCL_call     c fetch
15 TTL          c 1976586397 VCL 120 -1 -1 1340840847 -0
....
15 TxResponse   c OK
15 TxHeader     c Server: Apache
15 TxHeader     c X-Powered-By: PHP/5.3.13
15 TxHeader     c Content-Type: image/vnd.microsoft.icon
15 TxHeader     c Content-Length: 0
15 TxHeader     c Accept-Ranges: bytes
15 TxHeader     c Date: Wed, 27 Jun 2012 23:47:27 GMT
15 TxHeader     c X-Varnish: 1976586397

To get a clearer idea of what is happening, use the varnishstat command instead:

NAME CURRENT CHANGE AVERAGE AVG_10 AVG_100 AVG_1000
MGT.uptime 0+00:07:02
MAIN.uptime 0+00:07:03
MAIN.sess_conn 8 0.00 0.02 0.03 0.01 0.01
MAIN.client_req 197 0.00 0.47 0.03 0.01 0.01
MAIN.cache_miss 7 0.00 0.02 0.03 0.01 0.01

The command shows much more information but a clear indication of whether it is working can be obtained by checking the MAIN.backend_reuse (how often Varnish™ finds the contents in its cache) and the MAIN.cache_miss (how many times it failed and had to contact the web server).

After browsing the site for a while, you may find something like the below:

NAME CURRENT CHANGE AVERAGE AVG_10 AVG_100 AVG_1000
MGT.uptime 0+00:19:34
MAIN.uptime 0+00:19:35
MAIN.sess_conn 26 0.00 0.03 0.00 0.08 0.03
MAIN.client_req 593 0.00 0.63 0.04 1.77 0.65
MAIN.cache_hit 6 0.00 0.01 0.00 0.03 0.03
MAIN.cache_hit_grace 3 0.00 0.00 0.00 0.00 0.00
MAIN.cache_miss 12 0.00 0.01 0.00 0.02 0.01
MAIN.backend_conn 21 0.00 0.02 0.00 0.05 0.02
MAIN.backend_reuse 569 0.00 0.60 0.04 1.70 0.62

Disable Varnish™

IMPORTANT: Before disabling Varnish™ make sure that all services are stopped and that Apache is running on port 80 as described in the Varnish™ guide.

In some cases it is necessary to disable Varnish™. One example would be when trying to force HTTPS redirection with Apache (although you can also configure Varnish™ with SSL). To disable Varnish™, follow these steps:

  • Stop the Varnish™ service:

    $ sudo /opt/bitnami/ctlscript.sh stop varnish
    
  • Disable the Varnish™ service. Depending on your system type, run the following commands:

    • Approach A (Bitnami installations using system packages):

          $ sudo mv /etc/monit/conf.d/varnish.conf /etc/monit/conf.d/varnish.conf.disabled
          $ sudo gonit reload
      
    • Approach B (Self-contained Bitnami installations):

          $ sudo mv /opt/bitnami/varnish/scripts/ctl.sh /opt/bitnami/varnish/scripts/ctl.sh.disabled
          $ sudo /opt/bitnami/ctlscript.sh start varnish
      
  • Restart Apache:

    $ sudo /opt/bitnami/ctlscript.sh start apache
    

Use Varnish™ with SSL

Varnish™ is not compatible with HTTPS and needs an SSL terminator in front of it. However, it is possible to configure Apache to proxy all HTTPS requests to Varnish™. Follow the steps below:

  • Enable and start Varnish™.

  • Open the Apache HTTPS virtual host configuration file in a text editor. Depending on your installation type, edit the following files:

    • Approach A (Bitnami installations using system packages): /opt/bitnami/apache2/conf/bitnami/bitnami.conf

    • Approach B (Self-contained Bitnami installations): /opt/bitnami/apache2/conf/bitnami/bitnami-ssl.conf

  • Modify the Apache HTTPS virtual host (which uses port 443 by default) to proxy all its requests to Varnish™.

    <VirtualHost _default_:443>
        ...
        ProxyPreserveHost On
        RequestHeader set "X-Forwarded-Proto" expr=%{REQUEST_SCHEME}
        ProxyPass "/"  "http://127.0.0.1:81/"
        ProxyPassReverse "/"  "http://127.0.0.1:81/"
    
        ...
    
        # Bitnami applications installed with a prefix URL (default)
        # Include "/opt/bitnami/apache2/conf/bitnami/bitnami-apps-prefix.conf"
    </VirtualHost>
    
  • Configure the application to use the correct domain name. The steps to do this will vary for each application. As an example, if you are using WordPress, you would need to follow these steps.

  • If your Bitnami installation follows Approach B: Self-contained Bitnami installations, modify the /opt/bitnami/apps/APPNAME/conf/httpd-prefix.conf file and add the following line at the top of the file.

    SetEnvIf x-forwarded-proto https HTTPS=on
    

    In some cases, it is also necessary to modify the Bitnami application configuration to automatically redirects all HTTP traffic to the HTTPS port. The procedure to do this varies per application: some applications may require you to manually edit a configuration file, others may provide an administration interface, and still others may not require any specific changes.

    As an example, if you are using WordPress, you would need to edit the /opt/bitnami/apps/wordpress/htdocs/wp-config.php file and add the lines below before the WP_HOME and WP_SITEURL definitions:

    if (strpos($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') !== false)
        $_SERVER['HTTPS']='on';
    
  • Restart Apache:

    $ sudo /opt/bitnami/ctlscript.sh restart apache
    

Customize Varnish™

Varnish™ is installed with a default configuration file, agnostic to the Web application being cached. Using this configuration file, although achieving high performance, could lead to some content not being properly refreshed in the Varnish™ cache. As a result, users would see an outdated version of the site.

The solution is to use a custom VCL configuration file. There are multiple sources on the Internet that provide customized configuration files for different applications. A good source is the Varnish™ example page.

This section discusses how to change the default configuration file to a WordPress-specific one. Follow the steps below:

  • Obtain the source file here.

  • The file requires some modification to register the port on which your Apache server will be running. This port can be read either from the Apache configuration file at /opt/bitnami/apache2/conf/httpd.conf in the Listen directive:

    ...
    # Change this to Listen on specific IP addresses as shown below to
    # prevent Apache from glomming onto all bound IP addresses.
    #
    #Listen 12.34.56.78:80
    Listen 80
    ...
    

    Or by executing this command in a console:

    $ egrep '^Listen ' /opt/bitnami/apache2/conf/httpd.conf
    Listen 80
    

    With this value (80), edit the downloaded file and update the section below with the port number:

    backend default {
        .host = "127.0.0.1";
        .port = "81";
    }
    

    NOTE: For Bitnami stacks, Varnish™ is installed on the same server as Apache so the host can be configured as 127.0.0.1. You can also use Varnish™ to cache a remote server, by providing the host’s IP address.

  • Copy the file to the Varnish™ directory:

    $ cp /path/to/the/wordpress.vcl  /opt/bitnami/varnish/etc/varnish/
    
  • Stop Varnish™:

    $ sudo /opt/bitnami/ctlscript.sh stop varnish
    
  • Configure Varnish™ to load the appropriate file. Depending on your system type, follow the steps below:

    • Approach A (Bitnami installations using system packages):

      • Edit the file at /opt/bitnami/scripts/varnish-env.sh and add the following lines to the bottom of the file:

        export VARNISH_CONF_FILE="/opt/bitnami/varnish/etc/varnish/wordpress.vcl"
        
    • Approach B (Self-contained Bitnami installations):

      • Edit the file at /opt/bitnami/varnish/scripts/ctl.sh and change the VARNISH_CONFIG_FILE variable to point to the new configuration file:

        #! /bin/sh
        ...
        VARNISH_CONFIG_FILE=/opt/bitnami/varnish/etc/varnish/wordpress.vcl
        ...
        
  • Restart Varnish™ (and Apache if needed):

    $ sudo /opt/bitnami/ctlscript.sh start varnish
    $ sudo /opt/bitnami/ctlscript.sh start apache
    

IMPORTANT: Varnish™ will not cache content if Apache’s PageSpeed module is enabled. Find out how to disable this module.

Modify the default Varnish™ and Apache ports

After checking all is working properly, you may want to change the Varnish™ port to a standard one, usually port 80. If it was free at installation time, it should already be in use by Apache.

Follow these steps:

  • Stop Apache and Varnish™:

    $ sudo /opt/bitnami/ctlscript.sh stop apache
    $ sudo /opt/bitnami/ctlscript.sh stop varnish
    
  • The configuration of the ports will involve first changing the Apache port and then the Varnish™ port. Move Apache to a different port, by editing the Listen directive in the Apache configuration file at /opt/bitnami/apache2/conf/httpd.conf. Find the lines below:

    ...
    # Change this to Listen on specific IP addresses as shown below to
    # prevent Apache from glomming onto all bound IP addresses.
    #
    #Listen 12.34.56.78:80
    Listen 80
    ...
    

    and change them so that Apache listens on a different port:

    ...
    # Change this to Listen on specific IP addresses as shown below to
    # prevent Apache from glomming onto all bound IP addresses.
    #
    #Listen 12.34.56.78:80
    Listen 81
    ...
    
  • Update your application configuration for Apache. For example, if your applications are configured for virtual hosts, change the port in the Apache /opt/bitnami/apache2/conf/httpd.conf configuration file, the /opt/bitnami/apache2/conf/bitnami/bitnami.conf file as well as any application-specific virtual hosts, depending on your installation type:

    • Approach A (Bitnami installations using system packages):

      • Edit any virtual host file inside the /opt/bitnami/apache2/conf/vhosts/ directory.
    • Approach B (Self-contained Bitnami installations):

      • To know which applications are running under virtual hosts, check the file at /opt/bitnami/apache2/conf/bitnami/bitnami-apps-vhosts.conf, as this file contains the list of applications that you need to update, if any.

      • Afterwards, edit the /opt/bitnami/apps/APP-NAME/conf/httpd-vhosts.conf file.

  • Configure Varnish™ to use the old Apache port (80) and specify the new port for Apache (81) in the configuration file.

    • Approach A (Bitnami installations using system packages):

      • Edit the file at /opt/bitnami/scripts/varnish-env.sh and add the following lines to the bottom of the file:

        export VARNISH_CONF_FILE="/opt/bitnami/varnish/etc/varnish/default.vcl"
        export VARNISH_PORT_NUMBER="80"
        
    • Approach B (Self-contained Bitnami installations):

      • Edit the file at /opt/bitnami/varnish/scripts/ctl.sh and update it so that it looks like this:

        #! /bin/sh
        ...
        VARNISH_PORT=80
        ...
        VARNISH_CONFIG_FILE=/opt/bitnami/varnish/etc/varnish/default.vcl
        ...
        
  • The Varnish™ configuration file at /opt/bitnami/varnish/etc/varnish/default.vcl contains the port that Apache is listening on. Update it to reflect the new Apache port (81):

    backend default {
        .host = "127.0.0.1";
        .port = "81";
    }
    
  • Restart the servers.

    $ sudo /opt/bitnami/ctlscript.sh restart varnish
    

Apache without caching should now be available at port 81 and Varnish™ at port 80 as a reverse proxy for Apache.

NOTE: You can disable Varnish™ at any time by following these instructions.

Block specified URLs from being cached by Varnish™

It is advisable to block phpMyAdmin, phpPgAdmin and/or server-status from being cached and public. To do this. add the following lines of code to the end of the default Varnish™ configuration file at /opt/bitnami/varnish/etc/varnish/default.vcl or in the Varnish™ configuration file for your application:

sub vcl_recv {
    if (req.url ~ "^/phpmyadmin/.*$" || req.url ~ "^/phppgadmin/.*$" || req.url ~ "^/server-status.*$") {
        return (synth(403, "For security reasons, this URL is only accessible using localhost (127.0.0.1) as the hostname"));
    }
}

Varnish is a registered trademark of Varnish Software AB and its affiliates.

Last modification June 9, 2020