How can I improve TTFB for Magento 2 with NGINX?

Posted February 22, 2021 788 views

I am running Magento 2 with NGINX, MariaDB, Varnish and Redis on a Digital Ocean droplet. I am using Cloudflare as my CDN and using their Polish feature to optimize images. However, my TTFB is still slow. I have read a number of articles saying that there could be some changes to my NGINX configuration that may help but I am not sure where to start. Any help would be appreciated. Below is my config.

upstream fastcgi_backend {
server  unix:/run/php/php7.4-fpm.sock;

server {
    if ($host = {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    if ($host = {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    listen 80;
    return 301 https://$server_name$request_uri;


server {
    listen 443 ssl http2;

    ssl_certificate /etc/letsencrypt/live/; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/; # managed by Certbot 
    ssl_trusted_certificate /etc/letsencrypt/live/;
    include snippets/ssl.conf;
    include snippets/letsencrypt.conf;
    include snippets/cloudflare.conf;
    include snippets/gzip.conf;

    #include snippets/phpmyadmin.conf;
    #include snippets/magentoadmin.conf;    

    location / {
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header X-Forwarded-Port 443;
        proxy_set_header Host $host;
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
        proxy_buffer_size 128k;
        proxy_buffers 4 256k;
        proxy_busy_buffers_size 256k;

    access_log /var/log/nginx/;
    error_log /var/log/nginx/;


server {

    set $MAGE_ROOT /var/www/html/;
    set $MAGE_MODE production; # or developer

    include /var/www/html/;

edited by KFSys

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
1 answer

Hi @StatusCue,

Firstly, I believe this delay can be due to some other stuff rather than Nginx itself. What I always check is some online tool that performs checks on my website/application. Recently I’ve started using the following tool:

It gives good suggestions as to what can be resolved and what can be imporved.

Next would be to optimize your PHP-FPM config. This is one of the most crucial things. Not having a proper PHP-FPM config may result in having high load or not the best experience.

You can give the following article a read as well:

And lastly, take a look into using Opcache, it’s a really good browser caching tool when used with PHP-FPM properly.