Skip to content

Commit be34359

Browse files
committed
Tune nginx for high traffic and upstream instability
1 parent 082be6e commit be34359

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

nginx.conf.template

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
worker_processes auto;
22

33
events {
4-
worker_connections 1024;
4+
worker_connections 4096;
5+
multi_accept on;
56
}
67

78
http {
89
resolver ${DNS_RESOLVER} valid=30s ipv6=off;
910
resolver_timeout 5s;
11+
keepalive_timeout 65s;
12+
keepalive_requests 10000;
1013

1114
log_format cache '$remote_addr - $remote_user [$time_local] "$request" '
1215
'$status $body_bytes_sent "$http_referer" '
@@ -30,6 +33,8 @@ http {
3033
proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504 http_404;
3134
proxy_cache_background_update on;
3235
proxy_cache_lock on;
36+
proxy_cache_lock_timeout 30s;
37+
proxy_cache_lock_age 120s;
3338

3439
server {
3540
listen 80;
@@ -53,9 +58,14 @@ http {
5358
proxy_buffering on;
5459
proxy_http_version 1.1;
5560
proxy_set_header Connection "";
56-
proxy_connect_timeout 6h;
61+
# Fail fast when the upstream is unreachable; long-running work is in read/send.
62+
proxy_connect_timeout 5s;
5763
proxy_read_timeout 6h;
5864
proxy_send_timeout 6h;
65+
proxy_socket_keepalive on;
66+
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
67+
proxy_next_upstream_timeout 20s;
68+
proxy_next_upstream_tries 3;
5969
proxy_set_header X-Real-IP $remote_addr;
6070
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
6171
proxy_set_header X-Forwarded-Proto $scheme;
@@ -88,9 +98,14 @@ http {
8898
proxy_buffering on;
8999
proxy_http_version 1.1;
90100
proxy_set_header Connection "";
91-
proxy_connect_timeout 6h;
101+
# Fail fast when the upstream is unreachable; long-running work is in read/send.
102+
proxy_connect_timeout 5s;
92103
proxy_read_timeout 6h;
93104
proxy_send_timeout 6h;
105+
proxy_socket_keepalive on;
106+
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
107+
proxy_next_upstream_timeout 20s;
108+
proxy_next_upstream_tries 3;
94109
proxy_set_header X-Real-IP $remote_addr;
95110
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
96111
proxy_set_header X-Forwarded-Proto $scheme;

nginx.conf.template.backup

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
worker_processes auto;
22

33
events {
4-
worker_connections 1024;
4+
worker_connections 4096;
5+
multi_accept on;
56
}
67

78
http {
89
resolver ${DNS_RESOLVER} valid=30s;
910
resolver_timeout 5s;
11+
keepalive_timeout 65s;
12+
keepalive_requests 10000;
1013

1114
upstream owlery {
1215
server ${UPSTREAM_SERVER};
@@ -31,9 +34,11 @@ http {
3134
proxy_cache_valid 200 7776000s;
3235
proxy_cache_valid 404 600s;
3336

34-
proxy_cache_use_stale updating;
37+
proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504 http_404;
3538
proxy_cache_background_update on;
3639
proxy_cache_lock on;
40+
proxy_cache_lock_timeout 30s;
41+
proxy_cache_lock_age 120s;
3742

3843
server {
3944
listen 80;
@@ -57,16 +62,18 @@ http {
5762
proxy_buffering on;
5863
proxy_http_version 1.1;
5964
proxy_set_header Connection "";
60-
proxy_connect_timeout 6h;
65+
# Fail fast when the upstream is unreachable; long-running work is in read/send.
66+
proxy_connect_timeout 5s;
6167
proxy_read_timeout 6h;
6268
proxy_send_timeout 6h;
69+
proxy_socket_keepalive on;
6370
proxy_set_header Host $http_host;
6471
proxy_set_header X-Real-IP $remote_addr;
6572
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
6673
proxy_set_header X-Forwarded-Proto $scheme;
6774
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
68-
proxy_next_upstream_timeout 30s;
69-
proxy_next_upstream_tries 2;
75+
proxy_next_upstream_timeout 20s;
76+
proxy_next_upstream_tries 3;
7077
add_header X-Cache-Status $upstream_cache_status;
7178
add_header X-Cache-Key "$request_method$request_uri";
7279
proxy_ignore_headers Cache-Control Expires Set-Cookie;
@@ -96,16 +103,18 @@ http {
96103
proxy_buffering on;
97104
proxy_http_version 1.1;
98105
proxy_set_header Connection "";
99-
proxy_connect_timeout 6h;
106+
# Fail fast when the upstream is unreachable; long-running work is in read/send.
107+
proxy_connect_timeout 5s;
100108
proxy_read_timeout 6h;
101109
proxy_send_timeout 6h;
110+
proxy_socket_keepalive on;
102111
proxy_set_header Host $http_host;
103112
proxy_set_header X-Real-IP $remote_addr;
104113
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
105114
proxy_set_header X-Forwarded-Proto $scheme;
106115
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
107-
proxy_next_upstream_timeout 30s;
108-
proxy_next_upstream_tries 2;
116+
proxy_next_upstream_timeout 20s;
117+
proxy_next_upstream_tries 3;
109118
add_header X-Cache-Status $upstream_cache_status;
110119
add_header X-Cache-Key "$request_method$request_uri";
111120
proxy_ignore_headers Cache-Control Expires Set-Cookie;

0 commit comments

Comments
 (0)