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 Cache to realize Service degradation in FastCGI

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article is about how to use Cache to achieve service degradation in FastCGI. I think it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it.

The architecture diagram is as follows:

Degradation

The key point of implementation is to handle exceptions through error_page and complete service degradation:

Limit_conn_zone $server_name zone=perserver:1m; error_page 500 502 503 504 = @ degradation; fastcgi_cache_path / tmp levels=1:2 keys_zone=degradation:100m inactive=10d max_size=10g; upstream php {server 127.0.0.1 server 9000; server 127.0.0.1 degradation; fastcgi_cache_path 9001;} server {listen 80; limit_conn perserver 1000; server_name *. Xip.io Root / usr/local/www; index index.html index.htm index.php; location / {try_files $uri $uri/ / index.php$is_args$args;} location ~\. Php$ {set $cache_key $request_method://$host$request_uri; set $cache_bypass "1"; if ($arg_degradation = "on") {set $cache_bypass "0" } try_files $uri = 404; include fastcgi.conf; fastcgi_pass php; fastcgi_intercept_errors on; fastcgi_next_upstream error timeout; fastcgi_cache degradation; fastcgi_cache_lock on; fastcgi_cache_lock_timeout 1s; fastcgi_cache_valid 200 301 302 10h; fastcgi_cache_min_uses 10 Fastcgi_cache_use_stale error timeout invalid_header updating http_500 http_503; fastcgi_cache_key $cache_key; fastcgi_cache_bypass $cache_bypass Add_header X-Cache-Status $upstream_cache_status; add_header X-Response-Time $upstream_response_time;} location @ degradation {rewrite. $request_uri?degradation=on last;}}

A little tip: xip.io is used when setting up the domain name, so you don't have to set up hosts with it, which is convenient for debugging.

What is used in the code is the function included by default in Nginx, which we can think of as a general version, but compared with the goal in our architecture diagram, we will find that it does not implement the function of globally activating caching. How can it be realized? The simplest method is to determine whether the system is healthy or not by the number of errors per unit time, set the corresponding threshold, and activate the cache globally once the limit is exceeded. We can implement a customized version through Lua:

Lua_shared_dict fault 1m; limit_conn_zone $server_name zone=perserver:1m; error_page 500502 503 504 = @ degradation; fastcgi_cache_path / tmp levels=1:2 keys_zone=degradation:100m inactive=10d max_size=10g; upstream php {server 127.0.0.1 degradation; fastcgi_cache_path 9000; server 127.0.0.1 degradation; fastcgi_cache_path 9001 } init_by_lua 'get_fault_key = function (timestamp) if not timestamp then timestamp = ngx.time () end return os.date ("fault:minute:%M" Timestamp) end get_fault_num = function (timestamp) local fault = ngx.shared.fault local key = get_fault_key (timestamp) return tonumber (fault:get (key)) or 0 end incr_fault_num = function (timestamp) local fault = ngx.shared.fault local key = get_fault_key (timestamp) if not fault:incr (key, 1) then fault:set (key, 1 600) end end' Server {listen 80; limit_conn perserver 1000; server_name * .xip.io; root / usr/local/www; index index.html index.htm index.php Location / {rewrite_by_lua'if ngx.var.arg_degradation then return ngx.exit (ngx.OK) end local ok = true for i = 0 1 do local num = get_fault_num (ngx.time ()-I * 60) if num > 1000 then ok = false break end end if not ok then local query = "degradation=on" if ngx.var.args Then ngxngx.var.args = ngx.var.args.. "&". Query else ngx.var.args = query end end'; try_files $uri $uri/ / index.php$is_args$args;} location ~\ .php$ {set $cache_key $request_method://$host$request_uri; set $cache_bypass "1" If ($arg_degradation = "on") {set $cache_bypass "0";} try_files $uri = 404; include fastcgi.conf; fastcgi_pass php; fastcgi_intercept_errors on; fastcgi_next_upstream error timeout; fastcgi_cache degradation; fastcgi_cache_lock on; fastcgi_cache_lock_timeout 1s Fastcgi_cache_valid 200301 302 10h; fastcgi_cache_min_uses 10; fastcgi_cache_use_stale error timeout invalid_header updating http_500 http_503 Fastcgi_cache_key $cache_key; fastcgi_cache_bypass $cache_bypass; add_header X-Cache-Status $upstream_cache_status; add_header X-Response-Time $upstream_response_time } location @ degradation {content_by_lua'if ngx.var.arg_degradation then return ngx.exit (ngx.HTTP_INTERNAL_SERVER_ERROR) end local res = ngx.location.capture (ngx.var.request_uri) {args = "degradation=on"}) ngx.status = res.status for name, value in pairs (res.header) do ngx.header [name] = value end ngx.print (res.body) incr_fault_num ()' }}

Explanation: in fact, the logic of obtaining cache key names in real cases is a little complicated, and everything is simple in view of the limited space.

When the system is normal, it runs in dynamic mode, and the data is rendered through PHP-FPM; when the system is abnormal, the global cache is activated and runs in static mode, and the data is rendered through the cache. Through the test, it is found that when the system switches from normal to abnormal, the PHP-FPM is abandoned, so the RPS jumps from one thousand to ten thousand. This reminds me of the scene when I saw Saint when I was a child: whenever the undead bird was knocked down by the enemy, he could always stand up again and burst into greater energy.

In addition, it should be noted that in the event of a failure, if a large number of cache expires occur, then interactive behavior will still occur with PHP-FPM because cache reconstruction is involved, which may affect performance. There is no particularly good solution at this time. If the version of Nginx is enough, you can consider activating fastcgi_cache_revalidate. In this way, once PHP-FPM judges that the system is in an abnormal situation. Then you can directly return 304 to achieve cache duration.

The above is how to use Cache to achieve service degradation in FastCGI. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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