How to configure Nginx for a VPS?

31 Views Asked by At

I just upgraded my shared hosting to a VPS (both on hostinger), the VPS has 2 vCPUs and 8GB Ram (Shared hosting had 2vCPUs and less Ram), always below 5% CPU usage & 25% Ram usage.

However, the VPS is much slower than the shared hosting. It takes at least 2 times longer on the VPS to resolve a single request.

Shared hosting: it takes 636ms to resolve

VPS: it takes 1670ms to resolve

That is my current Nginx script - nginx.conf (exists in the laravel app docker image)

server {
    listen 80;
    server_name domain.com www.domain.com;

    # Redirect HTTP to HTTPS
    return 301 https://$server_name$request_uri;
}

server {
    listen 443 ssl;
    server_name domain.com www.domain.com;

    ssl_certificate /etc/ssl/certs/nginx-selfsigned.crt;
    ssl_certificate_key /etc/ssl/private/nginx-selfsigned.key;

    root /var/www/html/public;

    index index.php index.html index.htm;

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

    location ~ \.php$ {
        # include snippets/fastcgi-php.conf;
        fastcgi_pass app:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

Also, when opening the app that talks to the server, it feels like it resolves one request at a time, a couple of requests (loading the home page), takes 20 seconds to finish, everytime. On the shared hosting it wasn't more than 2 seconds.

I am using cloudflare on both (VPS and Hostinger).

Also, my server got 4 docker images.

  1. nginx:latest
  2. Laravel app (the one that has the nginx.conf)
  3. SQL instance
  4. phpmyadmin

Any help would be so much appreciated, as I am in prod and it's a complete nightmare. Thanks!

1

There are 1 best solutions below

0
Ravindra Bagale On

Enable Gzip compression to reduce the size of transmitted data.

Modify http block of your nginx configuration

http {
    gzip on;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
    gzip_min_length 1000;
    gzip_comp_level 2;
    gzip_proxied any;
    gzip_vary on;
    gzip_buffers 16 8k;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";
    gzip_http_version 1.1;
}