Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Varnish basic configuration to achieve dynamic and static separation of web sites

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Because the program access of a web site has local characteristics: time locality: after a data has been accessed, it may be accessed again soon; spatial locality: when a data is accessed, the surrounding data may also be accessed; varnish can cache this part of the data.

Cached data with frequently accessed data can be called a hot spot: caches are also localized; timeliness: if cache space is exhausted: use LRU, the least recent use of algorithms; clean up expired caches

The basic working principle of varnish:

Varnish caches the data that can be cached in a way similar to the HTPP reverse proxy, and responds directly to the cached data to the client.

Without the corresponding data, it will point the request to the back-end machine and get the response data to respond to the client.

When the varnish has a cache, the response usually takes only a very short time, which is usually several orders of magnitude faster than directly accessing the back-end machine, so cache the cacheable pages into the varnish as much as possible.

Varnish working model diagram:

The processing mechanism diagram of varnish:

Which data can be cached or not cached: 1. Try to cache the public data of the site; 2. Use to exclude the user's private data.

Configure a wordperss site to use the varnish cache server and do static and dynamic separation

Basic Topology Diagram:

Varnish configuration (based on cenots7, the whole configuration process should avoid the influence of iptables and selinux): ntpdata 172.16.0.1 synchronization time

Install the varnish program:

Yum install varnish

Edit the main configuration file for varnish:

Vim / etc/varnish/varnish.params add the last line: DAEMON_OPTS= "- p thread_pools=3-p thread_pool_min=5-p thread_pool_max=1000-p thread_pool_timeout=300"

Start the varnish program:

Systemctl restart varnish

Edit the vcl configuration file for varnish:

Vim / etc/varnish/default.vcl

Make the following basic configuration:

Vcl 4.0th # Default backend definition. Set this to point to your content server.backend default {.host = "192.168.5.109"; .port = "80";} backend nginxsrvs {.host = "192.168.5.108"; .port = "80";} 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. If (req.method = = "PURGE") {return (purge);} if (req.url ~ "(? I) ^ / (login | admin)") {return (pass) } if (req.url ~ "(? I)\. (html | htm | css | svg | js | jpg | jpeg | png | gif | pdf)") {set req.backend_hint = nginxsrvs;} else {set req.backend_hint = default;} sub vcl_purge {return (synth (200, "Purged")) } sub vcl_deliver {if (obj.hits > 0) {set resp.http.X-Cache = "HIT via" + server.ip } else {set resp.http.X-Cache = "Miss via" + server.ip;}

Compile and load the default.vcl file into the varnish program:

Varnishadm-S secret enters the cli interface of varnish configuration to perform compilation loading: vcl.load test1 default.vcl vcl.use test1 quit configuration apm dynamic server: yum install httpdmkdir-p / apps/data (permissions need to be considered)

Cp the source file of wordpress to the directory

Edit the site profile for httpd

Vim / etc/httpd/conf.d/wordpress.conf

DirectoryIndex index.php index.html ServerName www.abc.com DocumentRoot / apps/data/wordpress Options FollowSymLinks AllowOverride None Require all granted

Start the httpd program:

Systemctl restart httpd configuration nfs service is used to share website files yum install nfs-utilsvim / etc/exports/apps/data * (rw,all_squash,anonuid=48) configure nginx static server: yum install nginx # the official yum source provided by nginx is used here

Configure the / etc/nginx/conf.d/default.conf file with the following modifications

Vim / etc/nginx/conf.d/default.conflocation / {root / apps/data/wordpress; # indicates the path to the web program index index.html index.htm;}

Start the nginx service program

Systemctl restart nginx

Change the hosts file to do the access test.

The basic configuration implementation of varnish has been completed.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report