Debian – NGINX – PHP 7 cache pour booster site Web

Configuring Nginx FastCGI Cache

Editer le fichier : /etc/nginx/nginx.conf

Ajouter ces 2 lignes dans la section http

fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=phpcache:100m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";


Edit Nginx Server Block.

vi /etc/nginx/sites-enabled/wordpress

Scroll down to the location ~ \.php$ section. Add the following lines in this section.

fastcgi_cache phpcache;
fastcgi_cache_valid 200 301 302 60m;
add_header X-FastCGI-Cache $upstream_cache_status;

The fastcgi_cache directive enables the memory cache previously created by fastcgi_cache_path directive. If you don’t include this directive, your server block will not use the cache.

The fastcgi_cache_valid sets the cache time depending on the HTTP status code. In the example above, responses with status code 200, 301, 302 will be cached for 60 minutes.

The 3rd line add a field in the response header that can be used to validate whether the request has been served from the FastCGI cache.

Now save and close the server block configuration file. Then test your Nginx configuration.

nginx -t

If the test is successful, reload Nginx.

service nginx reload
systemctl reload nginx

root@srv-khanh:/etc/nginx/sites-enabled# curl -I

HTTP/1.1 200 OK

Server: nginx

Date: Sun, 23 Apr 2017 15:48:52 GMT

Content-Type: text/html; charset=UTF-8

Connection: keep-alive

Vary: Cookie

Link: <>; rel=””

Link: <>; rel=shortlink

X-FastCGI-Cache: HIT


Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>




This site uses Akismet to reduce spam. Learn how your comment data is processed.