Search code examples
rubynginxsinatraserverunicorn

Basic server optimization – Slow server with 1 connection


My server runs incredibly slowly... with no traffic at all apart from myself. I am proxying Unicorn server with Nginx. I know very little about server optimisation. A total of 13 seconds first loading time to 10 on second.

Network:

https://i.sstatic.net/1aHf9.png

https://i.sstatic.net/1S7Vi.png

https://i.sstatic.net/l4IuJ.png

What is the quickest way for me to shave 10 seconds off it?

Note: Assets have not been precompiled.

Edit: If I cut Nginx out of the process and serve directly to Unicorn I'm down to 1.72 seconds. It looks as if Nginx is the great culprit. I still don't have a clue why Nginx is slowing me down so much.


Solution

  • Okay so...

    The bottleneck was indeed due to Nginx or rather the interplay between Nginx and Unicorn. It was not resolved until I made Unicorn and Nginx agree on a socket in their respective configuration files.

    Unicorn needed this:

    listen ENV["UNICORN_PORT"].to_i || 4567, :tcp_nopush => true
    listen "#{@dir}tmp/sockets/unicorn.sock", :backlog => 64
    

    and nginx needed an upstream configuration pointing to the socket.

    upstream unicorn_server {
        server unix:/home/user/someapp/tmp/sockets/unicorn.sock
        fail_timeout=0;
    }
    

    Server first response time is now down to 1-2 seconds.