How To Install Open Source HTTP Accelerator for NGINX on CentOS 7: Varnish

Varnish is an open-source HTTP accelerator that acts as a caching layer between web servers and clients to improve website performance and responsiveness. In this article, we'll explore how to accelerate NGINX with Varnish on CentOS 7 for more efficient website delivery.

Preparation

Before starting, make sure you have done the following preparations:

  1. Make sure you have CentOS 7 installed and have root privileges. You can find the installation steps and requirements for CentOS 7 on the official website.

  2. Check system dependencies and requirements. In order to successfully install and configure Varnish and NGINX, you need to meet some basic requirements, such as network connection, storage space, and system resources. Make sure your system meets these requirements to avoid problems.

Step 1: Install and configure NGINX

First, we need to install and configure NGINX as our web server. Following are the steps to install NGINX on CentOS 7:

  1. Install NGINX with the following command:
sudo yum install nginx
  1. Once the installation is complete, start the NGINX service with the following command:
sudo systemctl start nginx
  1. Make sure NGINX has started successfully, you can check its status with the following command:
sudo systemctl status nginx
  1. Now, open your web browser and enter the IP address or domain name of the server. If you see the NGINX welcome page, the installation and configuration was successful.

Best practices for configuring NGINX to work with Varnish:

  • Make sure that the port that NGINX listens on matches the backend port in the Varnish configuration.
  • Disable caching in the NGINX configuration so that Varnish can completely take over caching functionality.

Step 2: Install and configure Varnish

Next, we'll install and configure Varnish to work with NGINX. Following are the steps to install Varnish on CentOS 7:

  1. Download and add the Varnish repository. Add the Varnish repository to your system with the command:
sudo curl -o /etc/yum.repos.d/varnish.repo https://packagecloud.io/varnishcache/varnish66/config_file.repo?os=centos&dist=7
  1. Install Varnish. Install Varnish with the following command:
sudo yum install varnish
  1. Once the installation is complete, start the Varnish service with the following command:
sudo systemctl start varnish
  1. Check if Varnish started successfully, you can check its status with the following command:
sudo systemctl status varnish

Configure Varnish cache and cache rules:

  • Open the Varnish configuration file /etc/varnish/default.vcl, you can customize the configuration according to your needs.
  • Configure Varnish's cache rules to determine what content needs to be cached, how long to cache, etc.

Step 3: Configure NGINX as a backend for Varnish

Now, we need to configure NGINX to forward requests to Varnish. Following are the steps to configure NGINX:

  1. Open the NGINX configuration file /etc/nginx/nginx.confand add the following code snippet to the end of the file:
location / {
    proxy_pass http://127.0.0.1:6081;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $host;
}
  1. Save and close the configuration file.

  2. Restart NGINX for configuration changes to take effect:

sudo systemctl restart nginx

Step 4: Test and optimize performance

After installing and configuring Varnish and NGINX, we need to test it and optimize it for performance. Here are some suggestions for testing and optimization:

  1. Use a performance testing tool, such as ApacheBench or Siege, to stress test your website and compare the performance difference between using Varnish and not using Varnish.

  2. Adjust Varnish's performance parameters, such as cache size, cache time, etc., according to your test results.

  3. Make sure to monitor and log Varnish and NGINX performance metrics regularly for optimization and troubleshooting purposes.

case study:

  • Case 1: Load Balancing

Describes how to use Varnish for load balancing with multiple NGINX backend servers and provides data on performance improvements.

  • Case 2: Static resource caching

Shows how to configure Varnish to cache static resources (such as images, CSS, and JavaScript files) to reduce the load on backend servers and improve website loading speed.

case study

Case 1: Load Balancing

In some cases, one NGINX server may not be able to handle all request loads. To solve this problem, we can use Varnish to achieve load balancing and distribute requests to multiple NGINX backend servers. Here are the steps to load balance with Varnish on CentOS 7:

  1. Configure multiple NGINX backend servers. Make sure these servers all have NGINX installed and listening on different ports.

  2. Modify the Varnish configuration file /etc/varnish/default.vcland add the following configuration code:

backend backend1 {
    .host = "127.0.0.1";
    .port = "8080";
}

backend backend2 {
    .host = "127.0.0.1";
    .port = "8081";
}

sub vcl_backend_fetch {
    set req.backend = backend1;
}

sub vcl_backend_response {
    if (beresp.status == 503 && req.retries < 3) {
        set req.backend = backend2;
        return (retry);
    }
}

The above configuration distributes requests to two backend servers with ports 8080 and 8081 respectively.

  1. Save and close the configuration file.

  2. Restart the Varnish service:

sudo systemctl restart varnish

Through this load balancing configuration, Varnish will distribute requests to two NGINX backend servers, thereby improving the scalability and fault tolerance of the system.

Case 2: Static resource caching

Static resources such as images, CSS, and JavaScript files typically take up the majority of a website's load time. In order to improve the loading speed of the website, we can use Varnish to cache these static resources. Here are the steps to use Varnish to cache static resources on CentOS 7:

  1. Open the Varnish configuration file /etc/varnish/default.vcl.

  2. Add the following configuration code to define the static resources that need to be cached:

sub vcl_recv {
    if (req.url ~ "^/static/") {
        return (hash);
    }
}

sub vcl_backend_response {
    if (bereq.url ~ "^/static/") {
        set beresp.ttl = 1d;
        set beresp.http.cache-control = "public, max-age=86400";
    }
}

The above configuration will treat /static/the URL request beginning with url as a static resource, and set the cache time to 1 day.

  1. Save and close the configuration file.

  2. Restart the Varnish service:

sudo systemctl restart varnish

With this configuration, Varnish will cache /static/responses to URL requests starting with , thereby reducing the load on the backend server and improving the loading speed of the website.

in conclusion

In this article, we discussed how to install the open source HTTP accelerator Varnish for NGINX on CentOS 7. We learned the steps to install and configure NGINX and Varnish, and saw how they can be used together to improve the performance and responsiveness of your website. We also present case studies of load balancing and static resource caching, showing the application of Varnish in different scenarios.

Using Varnish as a caching layer for NGINX can greatly improve the performance of your website and provide a better user experience. Through reasonable configuration and optimization, you can further improve the scalability and stability of the system.

Guess you like

Origin blog.csdn.net/weixin_43025343/article/details/131727805