Question

How can I improve TTFB for Magento 2 with NGINX?

Posted February 22, 2021 788 views
Nginx

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 = www.mydomain.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


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


    listen 80;
    server_name mydomain.com www.mydomain.com;
    return 301 https://$server_name$request_uri;





}

server {
    listen 443 ssl http2;
    server_name mydomain.com www.mydomain.com;    

    ssl_certificate /etc/letsencrypt/live/mydomain.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/mydomain.com/privkey.pem; # managed by Certbot 
    ssl_trusted_certificate /etc/letsencrypt/live/mydomain.com/chain.pem;
    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_pass http://127.0.0.1:6081;
        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/mydomain.com-access.log;
    error_log /var/log/nginx/mydomain.com-error.log;


}

server {
    listen 127.0.0.1:8080;
    server_name mydomain.com www.mydomain.com;

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

    include /var/www/html/mydomain.com/nginx.conf.sample;


}
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:

https://tools.pingdom.com/

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:

https://serversforhackers.com/c/php-fpm-multiple-resource-pools

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

Regards,
KFSys