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

How to use varnish + nginx + lua to build a website downgrade system

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

Share

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

This article mainly explains "how to use varnish + nginx + lua to build a website downgrade system". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use varnish + nginx + lua to build a website downgrade system".

Preface

Usually, after a website database is hung up, the consequences will be very serious. Basically, the whole site is basically unavailable. For some websites, it is also good to provide basic browsing services when the database is down. This article will try to use varnish + nginx + lua to build a website downgrade system to achieve the whole goal.

Demotion target

The goal of the downgrade scheme is to show the cached page data to the user when there is a fatal failure of the website (such as 500 errors, no service can be provided). To provide basic browsing services.

Provide only basic browsing services

The data browsed are all non-login data.

Manual and automatic demotion are supported. Automatic degradation is when the number of 500 errors returned by the backend reaches a certain threshold within a period of time (excluding 503). Manual downgrade is operated from the control interface.

Downgrade scheme storage

Use varnish as storage. Effectively save the physical memory, and maintain a good performance.

Update

Use the crond script to parse the request url from nginx's access log, and then send a request to varnish to update varnish's cache. Asynchronous update of cache to reduce the pressure on nginx.

Downgrade

Manual downgrade and automatic downgrade are supported. After downgrading, nginx automatically extracts data from varnish and returns it to the user.

Flow chart

Process description

When a user requests a nginx, nginx determines whether it is currently in a degraded state. If it is in a degraded state, get the data directly from varnish. Non-degraded status, transfer the request to php-fpm.

When the crond script requests varnish to update the cache data, no cache update occurs if the current varnish is in a degraded state. If it is not in a degraded state, the request is transferred to nginx to get the data. The acquired data is then cached into varnish.

Varnish automatically monitors the status of the backend nginx. If it is detected that nginx is already in a degraded state, varnish will also automatically enter the degraded state.

Installation and deployment

Vanish is installed in the / home/varnish directory. The installation steps are as follows:

First, install libpcre.

Sudo yum install pcre pcre-devel

Second, install varnish.

. / configure-- prefix=/home/varnishmakesudo-u admin make installsudo-u admin mkdir-p / home/varnish/vcache/sudo chown admin:admin-R / home/varnishsudo-u admin touch / home/varnish/vcache/varnish_cache.datasudo chmod 777 / home/varnish/vcache/varnish_cache.data

Third, modify the varnish configuration file and deploy the related scripts. Click to download the file package. The configuration file is named default.vcl.

Finally, start varnish. The startup script is also in the package, named varnishctl

Sudo / home/admin/varnish/sbin/varnishctl start

Note: after startup, you can use the varnishlog command to see if it is working properly. If the following words appear, it is running normally. The return status of http is 200.

$/ home/varnish/bin/varnishlog0 Backend_health-default Still healthy 4--X-RH 4 2 4 0.002698 0.001722 HTTP/1.1 200 OK deployment lua script

The lua script is in the / home/admin/nginx/data/lua directory. Make sure there are the following scripts in the directory.

Pc_get_downgrade_data.luainit.luapc_status_stat.luapc_get_status.luapc_set_satus.lua

These scripts are available in the downloaded package.

Modify nginx configuration file

First, add in the http domain

Init_by_lua_file 'lua/init.lua';lua_shared_dict pc_status 1m configupstream varnish Luaxiaxiasharedanddict pc_auto_status 1m witch varnish configupstream varnish {server 127.0.0.1 purl 8080 weight=1 max_fails=2 fail_timeout=5s;}

Finally, add the following configuration to the macro in the server domain.

Location @ php {include fastcgi_params;} location @ var {proxy_pass http://varnish$str_params;} location ~ * ^ (. +\ .php) (. *) ${# check downgrade status, then get data from varnish set $str_params $uri; content_by_lua_file lua/pc_get_downgrade_data.lua;} location / hl_get_auto_status {if ($white_ip = 0) {return 403 } content_by_lua_file lua/pc_get_auto_status.lua;} location / hl_get_status {if ($white_ip = 0) {return 403;} content_by_lua_file lua/pc_get_status.lua;} location / hl_set_status {if ($white_ip = 0) {return 403 } content_by_lua_file lua/pc_set_status.lua;} log_by_lua_file lua/pc_status_stat.lua; deployment crond script

Script varnish_crond.php. Add execute commands in crond. It is executed every minute.

Request from crond, user-agent data is varnish_crond. Request special handling of user-agent for varnish_crond. Ensure the normal request and return the relevant data.

Downgrade management varnish downgrade

Simply let the monitoring script check.php specified in the varnish configuration return a 500error. Varnish monitors that the specified script is not available and automatically enters a degraded state.

When the script returns to200, varnish automatically returns to normal.

Nginx downgrade

Set downgrade:

Curl-H "Host:demo.bo56.com"-I http://127.0.0.1/hl_set_status?status=1

Return to normal:

Curl-H "Host:demo.bo56.com"-I http://127.0.0.1/hl_set_status?status=0

View the demotion status:

Curl-H "Host:demo.bo56.com"-I http://127.0.0.1/hl_get_status

If the returned value is 1, it is degraded.

Thank you for reading, the above is the content of "how to use varnish + nginx + lua to build a website downgrade system". After the study of this article, I believe you have a deeper understanding of how to use varnish + nginx + lua to build a website downgrade system, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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