[On Premises]{class="badge informative" title="Applies to Adobe Commerce on-premises projects only."}

Configure nginx for Varnish caching configure-web-server-for-varnish-caching

When Varnish is used as the full-page cache in front of Adobe Commerce, Varnish typically listens on the public HTTP port and forwards requests to nginx on a non-default backend port such as 8080. Update the nginx site configuration for your Commerce origin server to listen on the backend port that Varnish will use.

NOTE
If your Commerce project is deployed on cloud, full-page caching uses Fastly instead of Varnish. The topics in this section apply to on-premises installations only.

The following sections use port 8080 as an example.

Change the nginx listen port for the Commerce origin server:

  1. Open the nginx site configuration for your Adobe Commerce origin server in a text editor.

The location depends on your operating system and nginx layout. For example, Ubuntu often uses a file under /etc/nginx/sites-available/.

  1. In the server block for the Commerce site, change the listen directive from the public HTTP port to the backend port that Varnish will use to reach nginx.

    For example, change

    code language-conf
    server {
        listen 80;
        server_name example.com;
        root /var/www/html/magento2;
        include /var/www/html/magento2/nginx.conf.sample;
    }
    

    to:

    code language-conf
    server {
        listen 8080;
        server_name example.com;
        root /var/www/html/magento2;
        include /var/www/html/magento2/nginx.conf.sample;
    }
    
  2. Save the file.

  3. Verify the nginx configuration:

    code language-shell
    nginx -t
    
  4. Restart nginx:

    code language-shell
    systemctl restart nginx
    

Modify the Varnish system configuration

After updating nginx to listen on the backend port, configure Varnish to forward requests to that host and port. For example:

backend default {
    .host = "192.0.2.55";
    .port = "8080";
}

Modify the default VCL

This section discusses how to provide minimal configuration so Varnish returns HTTP response headers. This enables you to verify that Varnish works before you configure the Commerce application to use Varnish.

To minimally configure Varnish:

  1. Back up default.vcl:

    code language-shell
    cp /etc/varnish/default.vcl /etc/varnish/default.vcl.bak
    
  2. Open /etc/varnish/default.vcl in a text editor.

  3. Locate the following stanza:

    code language-conf
    backend default {
        .host = "127.0.0.1";
        .port = "80";
    }
    
  4. Replace the value of .host with the fully qualified hostname or IP address and listen port of the Varnish backend or origin server; that is, the server providing the content Varnish will accelerate.

    Typically, this is your web server. See Backend servers in the Varnish guide.

  5. Replace the value of .port with the web server’s listen port (8080 in this example).

    Example: nginx is installed on host 192.0.2.55 and listening on port 8080:

    code language-conf
    backend default {
        .host = "192.0.2.55";
        .port = "8080";
    }
    
    note info
    INFO
    If Varnish and nginx are running on the same host, Adobe recommends that you use an IP address or hostname and not localhost.
  6. Save your changes to default.vcl and exit the text editor.

  7. Restart Varnish:

    code language-shell
    service varnish restart
    

If Varnish fails to start, try running it from the command line as follows:

varnishd -d -f /etc/varnish/default.vcl

This should display error messages.

INFO
If Varnish does not start as a service, you must configure SELinux rules to allow it to run.

Verify Varnish is working

The following sections discuss how you can verify that Varnish is working but without configuring Commerce to use it. You should try this before you configure Commerce.

Perform the tasks discussed in the following sections in the order shown:

Start Varnish

Enter: service varnish start

If Varnish fails to start as a service, start it from the command line as follows:

  1. Start the Varnish CLI:

    code language-shell
    varnishd -d -f /etc/varnish/default.vcl
    
  2. Start the Varnish child process:

    When prompted, enter start

    The following messages display to confirm a successful start:

    code language-text
    child (29805) Started
    200 0
    
    Child (29805) said
    Child (29805) said Child starts
    

netstat

Log in to the Varnish server and enter the following command:

netstat -tulpn

Look for the following output in particular:

tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      32614/varnishd
tcp        0      0 127.0.0.1:58484             0.0.0.0:*                   LISTEN      32604/varnishd
tcp        0      0 :::8080                     :::*                        LISTEN      26822/nginx
tcp        0      0 ::1:48509                   :::*                        LISTEN      32604/varnishd

The preceding shows Varnish running on port 80 and nginx running on port 8080.

If you do not see output for varnishd, make sure that Varnish is running.

See netstat options.

Install the Commerce software

Install the Commerce software if you have not already done so. When prompted for a Base URL, use the Varnish host and port 80 (for Varnish) because Varnish receives all incoming HTTP requests.

Possible error installing Commerce:

Error 503 Service Unavailable
Service Unavailable
XID: 303394517
Varnish cache server

If you experience this error, edit default.vcl and add a timeout to the backend stanza as follows:

backend default {
   .host = "127.0.0.1";
   .port = "8080";
   .first_byte_timeout = 600s;
}

Verify HTTP response headers

Now you can verify that Varnish is serving pages by looking at HTML response headers returned from any page.

Before you can look at headers, you must set Commerce for developer mode. There are several ways to do it, the simplest of which is to modify .htaccess in the Commerce application root. You can also use the magento deploy:mode:set command.

Set Commerce for developer mode

To set Commerce for developer mode, use the magento deploy:mode:set command.

Look at the Varnish log

Make sure that Varnish is running then enter the following command on the Varnish server:

varnishlog

In a web browser, go to any Commerce page.

A long list of response headers display in your command prompt window. Look for headers like the following:

-   BereqHeader    X-Varnish: 3
-   VCL_call       BACKEND_FETCH
-   VCL_return     fetch
-   BackendOpen    17 default(10.249.151.10,,8080) 10.249.151.10 60914
-   Backend        17 default(10.249.151.10,,8080)
-   Timestamp      Bereq: 1440449534.261791 0.000618 0.000618
-   ReqHeader      Host: 10.249.151.10
-   ReqHeader      Connection: keep-alive
-   ReqHeader      Content-Length: 86
-   ReqHeader      Cache-Control: max-age=0
-   ReqHeader      Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
-   ReqHeader      Origin: http://10.249.151.10

If headers like these do not display, stop Varnish, check your default.vcl, and try again.

Look at HTML response headers

There are several ways to look at response headers, including using a browser plug-in or a browser inspector.

The following example uses curl. You can enter this command from any machine that can access the Commerce server using HTTP.

curl -I -v --location-trusted '<your Commerce base URL>'

For example,

curl -I -v --location-trusted 'http://192.0.2.55/magento2'

Look for headers like the following:

Content-Type: text/html; charset=iso-8859-1
X-Varnish: 15
Age: 0
Via: 1.1 varnish-v6
X-Magento-Cache-Debug: HIT
recommendation-more-help
commerce-operations-help-configuration