/
Pack Hosting Panel

Set up Varnish

How to set up Varnish at Hipex?


Varnish is a http-accelerator reverse proxy and catches requests before they go to PHP. The first request will be sent through by Varnish and the server will deal with this request. The result of the request will be saved in the Varnish cache. With the next similar request the result will be served from the varnish cache and doesn't use any load on the server.

At Hipex, we offer the possibility to configure and manage Varnish easily by Hipex CLI. The Hipex CLI commands for Varnish are available to use for every Hipex environment.

Varnish is configured per domain. Multiple Varnish instances can run on a server. In this article, we explain how to enable Varnish via the Hipex CLI tools.

Configuration

To set up Varnish, it is necessary to be logged in on the concerned server. Then, you can run the following command to configure Varnish:

hipex varnish:setup

You can simply specify the allowed amount of memory thats varnish may use. You can use the --allowed_memory parameter, the default amount is 256 MB.

For a shop with many pages, this amount can be increased. Please note allocating too much memory can cause serious problems. When you increase to much memory for varnish it is possible that MySQL, Redis or other services don't get anough memory anymore. So, increase this with small steps and monitor well whether Varnish has too much or too little memory.

In the command below, we set Varnish with a memory usage of 1024 MB (=1GB).

hipex varnish:setup --allowed_memory 1024

When Varnish has already been set up, you can make use if the `varnish:manage command. Below, we increased the memory to 1024 MB:

hipex varnish:manage --memory 1024

Inschakelen

Once configured, varnish can be enabled. For this you can use the following command:

hipex varnish:enable

Application & SSL offloading

Because after enabling Varnish requests will no longer directly reach your application via https, PHP does not see these requests as http requests by default. If you also redirect to https in the application in these cases, you will get an infinite redirect loop. You have to be sure that the PHP server variable "$_SERVER['HTTPS]" is not turned โ€œonโ€.

The most modern applications do not only look at the server variable $_SERVER for a similar request anymore, but also look at the SSL offload header. If this is not the case, you can add this piece of PHP code to the entry point (usually index.php).

if (isset($_SERVER['HTTP_SSL_OFFLOADED'])) {
    $_SERVER['HTTPS'] = 'on';
}

Varnish commands

We can add a couple of commands to gain more control and monitoring over the requests that go through Varnish. To do this, add the following lines to the ~/.bash_aliases file. After this has been added, you must log in again to use the added commands.

alias varnishadm="varnishadm -n ~/domains/<domain-name>/var/run/"
alias varnishhist="varnishhist -n ~/domains/<domain-name>/var/run/"
alias varnishlog="varnishlog -n ~/domains/<domain-name>/var/run/"
alias varnishncsa="varnishncsa -n ~/domains/<domain-name>/var/run/"
alias varnishstat="varnishstat -n ~/domains/<domain-name>/var/run/"
alias varnishtop="varnishtop -n ~/domains/<domain-name>/var/run/"