varnish yum安装
https://packagecloud.io/varnishcache/varnish60lts/packages/el/7/varnish-6.0.13-1.el7.x86_64.rpm?distro_version_id=140



repo :
curl -s https://packagecloud.io/install/repositories/varnishcache/varnish60lts/script.rpm.sh | sudo bash

[root@host2 ~]# cat /etc/yum.repos.d/varnishcache_varnish60lts.repo 
[varnishcache_varnish60lts]
name=varnishcache_varnish60lts
baseurl=https://packagecloud.io/varnishcache/varnish60lts/el/7/$basearch
repo_gpgcheck=1
gpgcheck=0
enabled=1
gpgkey=https://packagecloud.io/varnishcache/varnish60lts/gpgkey
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire=300

[varnishcache_varnish60lts-source]
name=varnishcache_varnish60lts-source
baseurl=https://packagecloud.io/varnishcache/varnish60lts/el/7/SRPMS
repo_gpgcheck=1
gpgcheck=0
enabled=1
gpgkey=https://packagecloud.io/varnishcache/varnish60lts/gpgkey
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire=300


sudo yum install varnish-devel-6.0.13-1.el7.x86_64 -y

sudo yum install varnish-6.0.13-1.el7.x86_64 -y 



Running transaction
  正在安装    : jemalloc-3.6.0-1.el7.x86_64                                                                     1/2 
  正在安装    : varnish-6.0.13-1.el7.x86_64                                                                     2/2 
  验证中      : jemalloc-3.6.0-1.el7.x86_64                                                                     1/2 
  验证中      : varnish-6.0.13-1.el7.x86_64                                                                     2/2 

已安装:
  varnish.x86_64 0:6.0.13-1.el7                                                                                     

作为依赖被安装:
  jemalloc.x86_64 0:3.6.0-1.el7  
  
[root@host2 ~]# find /var -name "*.rpm"|xargs -I {} cp -r {} /root/rpm
[root@host2 ~]# ls rpm
iperf-2.0.13-1.el7.x86_64.rpm            telnet-0.17-66.el7.x86_64.rpm
jemalloc-3.6.0-1.el7.x86_64.rpm          varnish-6.0.13-1.el7.x86_64.rpm
kernel-ml-6.8.5-1.el7.elrepo.x86_64.rpm  vim-common-7.4.629-8.el7_9.x86_64.rpm
nmap-6.40-19.el7.x86_64.rpm              vim-enhanced-7.4.629-8.el7_9.x86_64.rpm
sysstat-10.1.5-20.el7_9.x86_64.rpm


 ls /etc/varnish/default.vcl
/etc/varnish/default.vcl


/etc/varnish/varnish.params



 cat /usr/lib/systemd/system/varnish.service
[Unit]
Description=Varnish Cache, a high-performance HTTP accelerator
After=network-online.target nss-lookup.target

[Service]
Type=forking
KillMode=process

# Maximum number of open files (for ulimit -n)
LimitNOFILE=131072

# Locked shared memory - should suffice to lock the shared memory log
# (varnishd -l argument)
# Default log size is 80MB vsl + 1M vsm + header -> 82MB
# unit is bytes
LimitMEMLOCK=85983232

# Enable this to avoid "fork failed" on reload.
TasksMax=infinity

# Maximum size of the corefile.
LimitCORE=infinity

ExecStart=/usr/sbin/varnishd \
      -a :6081 \
      -a localhost:8443,PROXY \
      -p feature=+http2 \
      -f /etc/varnish/default.vcl \
      -s malloc,256m
ExecReload=/usr/sbin/varnishreload

[Install]
WantedBy=multi-user.target

 cat /etc/varnish/default.vcl
#
# This is an example VCL file for Varnish.
#
# It does not do anything by default, delegating control to the
# builtin VCL. The builtin VCL is called when there is no explicit
# return statement.
#
# See the VCL chapters in the Users Guide at https://www.varnish-cache.org/docs/
# and https://www.varnish-cache.org/trac/wiki/VCLExamples for more examples.

# Marker to tell the VCL compiler that this VCL has been adapted to the
# new 4.0 format.
vcl 4.0;

# Default backend definition. Set this to point to your content server.
backend default {
    .host = "127.0.0.1";
    .port = "8080";
}

sub vcl_recv {
    # Happens before we check if we have this in cache already.
    #
    # Typically you clean up the request here, removing cookies you don't need,
    # rewriting the request, etc.
}

sub vcl_backend_response {
    # Happens after we have read the response headers from the backend.
    #
    # Here you clean the response headers, removing silly Set-Cookie headers
    # and other mistakes your backend does.
}

sub vcl_deliver {
    # Happens when we have all the pieces we need, and are about to send the
    # response to the client.
    #
    # You can do accounting or modifying the final object here.
}



systemctl status  varnish
● varnish.service - Varnish Cache, a high-performance HTTP accelerator
   Loaded: loaded (/usr/lib/systemd/system/varnish.service; disabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since 日 2024-05-12 23:13:36 CST; 3s ago
  Process: 3568 ExecStart=/usr/sbin/varnishd -a :6081 -a localhost:8443,PROXY -p feature=+http2 -f /etc/varnish/default.vcl -s malloc,256m (code=exited, status=255)

5月 12 23:13:36 host2 systemd[1]: Starting Varnish Cache, a high-performance HTTP accelerator...
5月 12 23:13:36 host2 varnishd[3568]: Error: Could not get socket localhost:8443: Cannot assign requested address
5月 12 23:13:36 host2 varnishd[3568]: (-? gives usage)
5月 12 23:13:36 host2 systemd[1]: varnish.service: control process exited, code=exited status=255
5月 12 23:13:36 host2 systemd[1]: Failed to start Varnish Cache, a high-performance HTTP accelerator.
5月 12 23:13:36 host2 systemd[1]: Unit varnish.service entered failed state.
5月 12 23:13:36 host2 systemd[1]: varnish.service failed.
cat /etc/varnish/default.vcl
# Default backend definition. Set this to point to your content server.
backend default {
    .host = "127.0.0.1";
    .port = "80";
}




安装nginx
yum list | grep ^nginx && yum -y install nginx*

mkdir /usr/share/nginx/html/hello
echo "192.168.40.131" > /usr/share/nginx/html/hello/index.html
echo "192.168.40.131" > /usr/share/nginx/html/index.html

systemctl start nginx

http://192.168.40.131/hello/

vim /usr/lib/systemd/system/varnish.service

[Unit]
Description=Varnish Cache, a high-performance HTTP accelerator
After=network-online.target nss-lookup.target

[Service]
Type=forking
KillMode=process

# Maximum number of open files (for ulimit -n)
LimitNOFILE=131072

# Locked shared memory - should suffice to lock the shared memory log
# (varnishd -l argument)
# Default log size is 80MB vsl + 1M vsm + header -> 82MB
# unit is bytes
LimitMEMLOCK=85983232

# Enable this to avoid "fork failed" on reload.
TasksMax=infinity

# Maximum size of the corefile.
LimitCORE=infinity

ExecStart=/usr/sbin/varnishd \
          -a :6081 \
          -a 0.0.0.0:8443,PROXY \
          -p feature=+http2 \
          -f /etc/varnish/default.vcl \
          -s malloc,256m
ExecReload=/usr/sbin/varnishreload

[Install]
WantedBy=multi-user.target
~                                  
[root@host2 ~]# systemctl daemon-reload
[root@host2 ~]# systemctl start varnish
[root@host2 ~]# systemctl status  varnish
● varnish.service - Varnish Cache, a high-performance HTTP accelerator
   Loaded: loaded (/usr/lib/systemd/system/varnish.service; disabled; vendor preset: disabled)
   Active: active (running) since 日 2024-05-12 23:22:42 CST; 4s ago
  Process: 4592 ExecStart=/usr/sbin/varnishd -a :6081 -a 0.0.0.0:8443,PROXY -p feature=+http2 -f /etc/varnish/default.vcl -s malloc,256m (code=exited, status=0/SUCCESS)
 Main PID: 4595 (varnishd)
    Tasks: 217
   CGroup: /system.slice/varnish.service
           ├─4595 /usr/sbin/varnishd -a :6081 -a 0.0.0.0:8443,PROXY -p feature=+http2 -f /etc/varnish/default.vcl -s malloc,256m
           └─4605 /usr/sbin/varnishd -a :6081 -a 0.0.0.0:8443,PROXY -p feature=+http2 -f /etc/varnish/default.vcl -s malloc,256m

5月 12 23:22:42 host2 varnishd[4592]: Warnings:
5月 12 23:22:42 host2 varnishd[4592]: VCL compiled.
5月 12 23:22:42 host2 varnishd[4595]: Version: varnish-6.0.13 revision a395739fa63cddec305142eabefec0a4fd5339e7
5月 12 23:22:42 host2 varnishd[4592]: Debug: Version: varnish-6.0.13 revision a395739fa63cddec305142eabefec0a4fd5339e7
5月 12 23:22:42 host2 varnishd[4592]: Debug: Platform: Linux,6.8.5-1.el7.elrepo.x86_64,x86_64,-junix,-smalloc,-sdefault,-hcritbit
5月 12 23:22:42 host2 varnishd[4595]: Platform: Linux,6.8.5-1.el7.elrepo.x86_64,x86_64,-junix,-smalloc,-sdefault,-hcritbit
5月 12 23:22:42 host2 varnishd[4595]: Child (4605) Started
5月 12 23:22:42 host2 varnishd[4592]: Debug: Child (4605) Started
5月 12 23:22:42 host2 varnishd[4595]: Child (4605) said Child starts
5月 12 23:22:42 host2 systemd[1]: Started Varnish Cache, a high-performance HTTP accelerator.

vim /usr/lib/systemd/system/varnish.service

ExecStart=/usr/sbin/varnishd \
      -a :6081 \
      -a 0.0.0.0:8443 \
      -p feature=+http2 \
      -f /etc/varnish/default.vcl \
      -s malloc,256m
ExecReload=/usr/sbin/varnishreload


[root@host2 ~]# systemctl daemon-reload
[root@host2 ~]# systemctl restart varnish


http://192.168.40.131:8443/hello/


vim /etc/varnish/default.vcl 

backend web1 {
  .host = "192.168.40.131";
  .port = "80";
}

sub vcl_deliver {
if (obj.hits > 0) {
       set resp.http.X-Cache = "HIT from " + req.http.host;
        set resp.http.X-Cache-Hits = obj.hits;
    } else {
       set resp.http.X-Cache = "MISS from " + req.http.host;
    }
   return (deliver);
   }


 systemctl daemon-reload
 systemctl restart   varnish
 systemctl status   varnish


varnishncsa


192.168.40.1 - - [12/May/2024:23:41:26 +0800] "GET http://192.168.40.131:8443/hello/ HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36"




安装nginx2
yum list | grep ^nginx && yum -y install nginx*

mkdir /usr/share/nginx/html/hello
echo "192.168.40.132" > /usr/share/nginx/html/hello/index.html
echo "192.168.40.132" > /usr/share/nginx/html/index.html
systemctl start nginx

http://192.168.40.132/hello/


vim /etc/varnish/default.vcl 
#backend default {
#    .host = "192.168.40.131";
#    .port = "80";
#}


backend web1 {
  .host = "192.168.40.131";
  .port = "80";
}
backend web2 {
  .host = "192.168.40.132";
  .port = "80";
}



sub vcl_recv {
if (req.http.host ~ "(?i)^(www.)?lzj1234.cn$") {
        set req.http.host = "www.lzj1234.cn";
        set req.backend_hint = web1;
      } elseif  (req.http.host ~ "(?i)^bbs.lzj1234.cn$") {
            set req.backend_hint = web2;
    return(hash);
     }

}


 systemctl daemon-reload
 systemctl restart   varnish
 systemctl status   varnish
 
 
 
 pc的hosts
 
 
192.168.40.131 www.lzj1234.cn
192.168.40.131 bbs.lzj1234.cn





http://www.lzj1234.cn:8443/hello/index.html


192.168.40.131

http://bbs.lzj1234.cn:8443/hello/index.html



elinks http://www.lzj1234.cn:8443/hello/index.html --dump


elinks http://bbs.lzj1234.cn:8443/hello/index.html --dump




 if (req.http.host ~ "^(www.)?lzj1234.cn") {
    set req.http.host = "www.lzj1234.cn";
    set req.backend_hint = web1;
  } elseif  (req.http.host ~ "^bbs.lzj1234.cn") {
        set req.backend_hint = web2;

 }


[root@host2 ~]# elinks http://www.lzj1234.cn:8443/hello/index.html --dump
   192.168.40.131

[root@host2 ~]# elinks http://bbs.lzj1234.cn:8443/hello/index.html --dump
   192.168.40.132



elinks http://www.lzj1234.cn:8443 --dump


elinks http://bbs.lzj1234.cn:8443 --dump


[root@host2 ~]# elinks http://www.lzj1234.cn:8443 --dump
   192.168.40.131
 
[root@host2 ~]# elinks http://bbs.lzj1234.cn:8443 --dump
   192.168.40.132