NeoWang
NeoWang

Reputation: 18513

Why is the initial connection time for a HTTP request so long?

My web app sits behind a Nginx. Occasionally, the loading of my web page takes more than 10 seconds, I used Chrome DevTools to track the timing, and it looks like this: enter image description here

The weird thing is, when the page loads slowly, the initial connection time is always 11 seconds long. And after this slow request, subsequent loading of the same page becomes very fast.

What is the possible problem that cause this?

P.S. If this is caused by a resource limitation on my server, can I see some errors/warnings in some system log?

Upvotes: 28

Views: 49232

Answers (4)

Saeb Amini
Saeb Amini

Reputation: 24400

I was experiencing the same with every second request, and after some testing, I found out that strangely this only happens if you're hitting localhost. If you use 127.0.0.1 or your local network IP address there are no "Initial connection" delays.

This seems to be because Chromium gets confused trying to do a DNS lookup for localhost for every other request, likely because of a change in localhost DNS resolution introduced since Windows 7 to let it be resolved via DNS. Uncommenting 127.0.0.1 localhost from the hosts file unfortunately does not seem to resolve this issue.

Another workaround is to add 127.0.0.1 customhost to your hosts file and hit your local dev server via customhost instead.

Upvotes: 1

Gideon Pyzer
Gideon Pyzer

Reputation: 24028

The initial connection refers to the time taken to perform the initial TCP handshake and negotiating SSL (where applicable). The slowness could be caused by congestion, where the server has hit a limit and can't respond to new connections while existing ones are pending. You could look into some performance enhancements in your Nginx configuration.

Upvotes: 16

Grant
Grant

Reputation: 115

Double check your DNS records.

I had this same problem. My issue was with my site's DNS records. I had accidentally added a second 'A' record which pointed to another server of mine in addition to my proper front end server. But both were for the same hostname.

So sometimes my client was trying to connect to the front end and everything worked properly. Other times it was trying to hit the other server and that was causing my 21.07 s delay.

When I deleted the incorrect DNS entry it solved things.

Upvotes: 1

宫宜栋
宫宜栋

Reputation: 57

Use dig command to check you domain name resolution process. if return multi answer section, check these ips is valid.

Upvotes: 3

Related Questions