NginxFastCGIキャッシュの使用

FastCGI Cache は、NginxHTTPサーバーレベルで実装されたデータキャッシングシステムです 

FastCGIキャッシュの利点  は、Nginxが要求を受信するとすぐに、キャッシュされた応答をユーザーに返す一方で、アプリケーション層がNginxキャッシュにある場合、着信HTTP要求をまったく処理しないことです。 

FastCGIキャッシュを使用することは、システムの負荷を軽減するための優れた方法です。

サイトにめったに変更されないページがある場合、または情報の更新の遅延が重要でない場合は、FastCGIキャッシュがまさに必要なものです。

NginxFastCGIキャッシュのしくみ

HTTP要求がNginxサーバーに到着し、しばらく前に同じ要求への応答がキャッシュに配置された場合、NginxはPHP-FPMを実行するためにこの要求を送信せず、代わりにこのNginxはキャッシュから結果を返します。

仕事

月へのフライト用のWebベースの制御システムがPHPで記述されているとします。各ユーザーは、システムに入り、スペースアプリケーションのメインページにアクセスするために、ユーザー名とパスワードを入力する必要があります。 

, , . . . SQL- .

, , . 

, , . .

?

  • , .

  • -. .

  • 1 .

 

:

http {

    include /etc/nginx/mime.types;

    server {
    
        listen 80;
        index index.php index.html;
        server_name moon-flight.aero;
        error_log /var/log/nginx/moon-flight.aero.error.log;
        access_log /var/log/nginx/moon-flight.aero.access.log
        root /var/www/moon-flight.aero/public;

        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }

        location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param PATH_INFO $fastcgi_script_name;
        }
    }
}

, fastcgi_cache_path   http.

fastcgi_cache_path /tmp/nginx_cache levels=1:2 keys_zone=fastcgicache:10m inactive=70m max_size=512m;

 /tmp/nginx_cache  , .  /tmp  , . , ,  /tmp/nginx_cache  .

— .  levels=1:2. ,  2. , , .

— . keys_zone=fastcgicache:10m ,  fastcgicache,  10m — . 10 80 000 . , .

— inactive=70m,  , , , . , ,  inactive, , .  inactive  10 .

 max_size=512m — . . , Nginx .

.  fastcgi_cache_key.  httpserver  location.

 server  fastcgi_cache_key:

fastcgi_cache_key "$scheme$request_method$host$request_uri$cookie_codeAuth";

:

— $scheme 

— $request_method

— $host

 $request_uri

— $cookie_codeAuth

. , . 

, :

http://moon-flight.aero/search/?query=10

 Nginx  :

$scheme

http

$request_method

GET

$host

moon-flight.aero

$request_uri

/search/?query=10

$cookie_codeAuth

Cookie codeAuth, . Cookie -.

, Cookie  codeAuth. 

Nginx Cookie, $cookie_.

, HTTP- :

Cookie: codeAuth=a7e30fbb7f4513redfd22049c6b5dzme306f4e

 "$scheme$request_method$host$request_uri$cookie_codeAuth" ,   , .  $cookie_codeAuth  .

 server   fastcgi_cachefastcgi_cache_validfastcgi_cache_bypass  fastcgi_no_cache   location.

 server.

:

  • ,  $request_uri  "/".

  •  GET  .  $request_method   GET.

  • ,  GET .  $query_string   "".

 3  (  server):

set $no_cache 0;

if ($request_method != GET) {
     set $no_cache 1;
}

if ($query_string != "") {
     set $no_cache 1;
}

if ($request_uri != "/") {
     set $no_cache 1;
}

fastcgi_cache, fastcgi_cache_valid, fastcgi_cache_bypass fastcgi_no_cache  location.

fastcgi_cache fastcgicache;
fastcgi_cache_valid 200 60m;
fastcgi_cache_bypass $no_cache;
fastcgi_no_cache $no_cache;

?

fastcgi_cache — , .  fastcgi_cache_path.

fastcgi_cache_valid — HTTP- . 60 200.

fastcgi_cache_bypass — , .  0, .

fastcgi_no_cache — , .  0, .

:

sudo service nginx reload

.

, ?

. ,  $upstream_cache_status

 server   add_header  :

add_header x-fastcgi-cache $upstream_cache_status;

x-fastcgi-cache — . 

 $upstream_cache_status  : 

  • MISS

  • BYPASS

  • EXPIRED

  • STALE

  • UPDATING

  • REVALIDATED

  • HIT

,  HIT.

:

http {

    include /etc/nginx/mime.types;
    
    fastcgi_cache_path /tmp/nginx_cache levels=1:2 keys_zone=fastcgicache:10m inactive=70m max_size=512m;

    server {
    
        listen 80;
        index index.php index.html;
        server_name moon-flight.aero;
        error_log /var/log/nginx/moon-flight.aero.error.log;
        access_log /var/log/nginx/moon-flight.aero.access.log
        root /var/www/moon-flight.aero/public;
        
        fastcgi_cache_key "$scheme$request_method$host$request_uri$cookie_codeAuth";
        
        set $no_cache 0;

        if ($request_method != GET) {
            set $no_cache 1;
        }

        if ($query_string != "") {
            set $no_cache 1;
        }
    
        if ($request_uri != "/") {
            set $no_cache 1;
        }

        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }
        
        add_header x-fastcgi-cache $upstream_cache_status;

        location ~ \.php$ {
        
            fastcgi_cache fastcgicache;
            fastcgi_cache_valid 200 60m;
            fastcgi_cache_bypass $no_cache;
            fastcgi_no_cache $no_cache;
        
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param PATH_INFO $fastcgi_script_name;
        }
    }
}

 

Nginx :

sudo nginx -t

, , :

sudo service nginx reload

Apache Bench

Apache Bench -. C Apache Bench , . cookies.

Apache Bench Ununtu.

:

sudo apt-get update

:

sudo apt-get install apache2-utils

.

Apache Bench

FastCGI Cache $no_cache 1.

sudo service nginx reload

. , Apache Bench :

ab -c 5 -n 100 -C "codeAuth=a7e30fbb7f4513redfd22049c6b5dzme306f4e" http://moon-flight.aero

http://moon-flight.aero 100 , 5 . -C , cookie codeAuth.

:

This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking moon-flight.aero (be patient).....done


Server Software:        nginx/1.14.0
Server Hostname:        moon-flight.aero
Server Port:            80

Document Path:          /
Document Length:        31134 bytes

Concurrency Level:      5
Time taken for tests:   2.978 seconds
Complete requests:      100
Failed requests:        0
Total transferred:      3150400 bytes
HTML transferred:       3113400 bytes
Requests per second:    33.58 [#/sec] (mean)
Time per request:       148.878 [ms] (mean)
Time per request:       29.776 [ms] (mean, across all concurrent requests)
Transfer rate:          1033.25 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       23   29   5.0     27      50
Processing:    58  112  17.1    113     142
Waiting:       58  112  17.0    113     142
Total:         85  141  16.7    142     176

Percentage of the requests served within a certain time (ms)
  50%    142
  66%    149
  75%    154
  80%    159
  90%    163
  95%    166
  98%    170
  99%    176
 100%    176 (longest request)

3 , 100 . — 33.58 .

Apache Bench

$no_cache 0.

sudo service nginx reload

:

ab -c 5 -n 100 -C "codeAuth=a7e30fbb7f4513redfd22049c6b5dzme306f4e" http://moon-flight.aero

:

This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking moon-flight.aero (be patient).....done


Server Software:        nginx/1.14.0
Server Hostname:        moon-flight.aero
Server Port:            80

Document Path:          /
Document Length:        31134 bytes

Concurrency Level:      5
Time taken for tests:   1.068 seconds
Complete requests:      100
Failed requests:        0
Total transferred:      3150104 bytes
HTML transferred:       3113400 bytes
Requests per second:    93.64 [#/sec] (mean)
Time per request:       53.398 [ms] (mean)
Time per request:       10.680 [ms] (mean, across all concurrent requests)
Transfer rate:          2880.52 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       23   31   6.5     27      47
Processing:    14   17   7.8     16      93
Waiting:       14   17   7.8     16      93
Total:         37   47  10.1     44     122

Percentage of the requests served within a certain time (ms)
  50%     44
  66%     49
  75%     52
  80%     54
  90%     56
  95%     61
  98%     63
  99%    122
 100%    122 (longest request)

1 , 100 . — 93.64 .


:

ngx_http_fastcgi_moduleの説明は、  のリンクから入手できます。http//nginx.org/ru/docs/http/ngx http fastcgi_module.html

FastCGI Cache Nginxの設定方法に関するビデオ:https//www.youtube.com/watch?v = Nri2KOI3HJo t = 66s

Apacheベンチドキュメント:https//httpd.apache.org/docs/2.4/programs/ab.html




All Articles