0

I already asked this question on StackOverflow but unfortunately no-one had an answer.

I am currently using NGINX as a revers proxy (because I am using a few docker containers) for an API (apache) I created. I am have removed most timeouts as some of my requests will take just under 60 seconds and that is not an issue.

However, one of my requests should NEVER take over 3 seconds and I would like to cancel it and send an empty response if it does.

Why I need this, is because sometimes my apache overloads (clients get queued) and TTL gets HUGE. And if it's the case, I really need to kill the request for api.webservice.me/public/js (it should not stay blocked and loading for too long). And It's no use to do this on apache side as the people requesting api.webservice.me/public/js will wait in the queue.

How is the correct way to do this?

So what i need is:

60 seconds timeout for api.webservice.me/public/anything
60 seconds timeout for api.webservice.me/public/second-request
3 seconds timeout for api.webservice.me/public/js

And if possible, respond with a empty 200 status

Here is the part handling my API

# api.website.me
upstream api.website.me {
    ip_hash;
    ## Can be connected with "nginx-proxy" network
    # serverapi_webservice_9
    server 172.18.0.13:80;
}
server {
    server_name api.website.me;
    listen 443 ssl http2 ;
    access_log /var/log/nginx/access.log vhost;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers 'XXXXX'
    ssl_prefer_server_ciphers on;
    ssl_session_timeout 5m;
    ssl_session_cache shared:SSL:50m;
    ssl_session_tickets off;
    ssl_certificate /etc/nginx/certs/api.website.me.crt;
    ssl_certificate_key /etc/nginx/certs/api.website.me.key;
    location / {
        proxy_pass http://api.website.me;
    }
}
server {
    server_name api.website.me;
    listen 80 ;
    access_log /var/log/nginx/access.log vhost;
    location / {
        proxy_pass http://api.website.me;
    }
}
1

Set proxy_read_timeout

proxy_read_timeout 3s;

Defines a timeout for reading a response from the proxied server. The timeout is set only between two successive read operations, not for the transmission of the whole response. If the proxied server does not transmit anything within this time, the connection is closed.

  • I added location /public/test.php { proxy_read_timeout 3s;.. However, when I create a php script with a sleep in it and a for, it does timeout with 504, but it takes like 20 seconds, not 3... Also, I am hoping for the timeout not to send a 504 status code, but a 200. – Bastian Jakobsen May 8 at 8:47
  • 1
    If you want to send a proper answer instead of an error you will have to implement the time limitation in the backend. You will not be able to do this in any reverse proxy. – Gerald Schneider May 8 at 9:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.