How to test nginx proxy timeouts
- by mkorszun
Target:
I would like to test all Nginx proxy timeout parameters in very simple scenario. My first approach was to create really simple HTTP server and put some timeouts:
Between listen and accept to test proxy_connect_timeout
Between accept and read to test proxy_send_timeout
Between read and send to test proxy_read_timeout
Test:
1) Server code (python):
import socket
import os
import time
import threading
def http_resp(conn):
conn.send("HTTP/1.1 200 OK\r\n")
conn.send("Content-Length: 0\r\n")
conn.send("Content-Type: text/xml\r\n\r\n\r\n")
def do(conn, addr):
print 'Connected by', addr
print 'Sleeping before reading data...'
time.sleep(0) # Set to test proxy_send_timeout
data = conn.recv(1024)
print 'Sleeping before sending data...'
time.sleep(0) # Set to test proxy_read_timeout
http_resp(conn)
print 'End of data stream, closing connection'
conn.close()
def main():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('', int(os.environ['PORT'])))
s.listen(1)
print 'Sleeping before accept...'
time.sleep(130) # Set to test proxy_connect_timeout
while 1:
conn, addr = s.accept()
t = threading.Thread(target=do, args=(conn, addr))
t.start()
if __name__ == "__main__":
main()
2) Nginx configuration:
I have extended Nginx default configuration by setting explicitly proxy_connect_timeout and adding proxy_pass pointing to my local HTTP server:
location / {
proxy_pass http://localhost:8888;
proxy_connect_timeout 200;
}
3) Observation:
proxy_connect_timeout - Even though setting it to 200s and sleeping only 130s between listen and accept Nginx returns 504 after ~60s which might be because of the default proxy_read_timeout value. I do not understand how proxy_read_timeout could affect connection at so early stage (before accept). I would expect 200 here. Please explain!
proxy_send_timeout - I am not sure if my approach to test proxy_send_timeout is correct - i think i still do not understand this parameter correctly. After all, delay between accept and read does not force proxy_send_timeout.
proxy_read_timeout - it seems to be pretty straightforward. Setting delay between read and write does the job.
So I guess my assumptions are wrong and probably I do not understand proxy_connect and proxy_send timeouts properly. Can some explain them to me using above test if possible (or modifying if required).