Corey Ogburn
Corey Ogburn

Reputation: 24717

Nginx reverse proxy holding open most connections just over 1 second?

My config:

upstream api-backend {
        server localhost:9005;
}

server {
        listen 80;

        lingering_close off;

        root /var/www/html;
        index index.html;

        location /api/ {
                proxy_http_version 1.1;
                proxy_set_header Connection "";
                add_header 'Access-Control-Allow-Origin' '*';
                add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT';
                proxy_pass      http://api-backend/;
        }

        location / {
                expires off;
                try_files $uri $uri/ =404;
        }
}

I'm serving up files out of /var/www/html and the /api endpoint forwards to a Go service. The Go service finishes requests in a few ms but Chrome's network tab reports the requests are almost all taking a few ms longer than 1s.

I believe it's Nginx that's keeping the connection open. How can I figure out what's keeping the connection alive and how to close it as fast as possible?

It seems like once in a blue moon nginx will decide to close as soon as the Go service is done and I'll get 20-40 ms times shown in Chrome. I have no clue what is causing it to sometimes close quickly.

Upvotes: 1

Views: 1610

Answers (1)

dr.dimitru
dr.dimitru

Reputation: 2702

You should remove lingering_close off; from config. According to the docs:

The value “off” tells nginx to never wait for more data and close the connection immediately. This behavior breaks the protocol and should not be used under normal circumstances.

And to manage how Nginx will wait for response from proxied server, use:

Upvotes: 1

Related Questions