Home > front end >  Django API Endpoints 504 Timeout Only When Authenticated
Django API Endpoints 504 Timeout Only When Authenticated

Time:01-28

I have an Application Server (APP) and an API Server (API). Requests to /api are sent via proxy_pass to the API server. I can access an api endpoint by going directly to the API server, or the preferred way via the APP server. Everything works as expected. The problem comes in as soon as I authenticate/login to my Django application. When I authenticate and I try and reach an api endpoint via my APP server I get a 504 Gateway Timeout. This has nothing to do with increasing the timeout configuration as I am able to reach the API server when I am not logged in.

  • Django 3.2.x
  • Python 3.9
  • Gunicorn
  • Nginx
  • APP and API server are using exactly the same codebase.

See Nginx configuration below:

APP Server

server {
        server_name cr-prod-app.example.com;
        
        location ^~ /static {
                alias /home/www/app/static_prod;
        }       
        
        location / {
                proxy_pass http://unix:/home/www/app/cr.sock;
        }       
        
        location /api {
                proxy_pass              https://cr-prod-api.example.com/api;
                proxy_http_version      1.1;
                proxy_set_header        Upgrade $http_upgrade;
                proxy_set_header        Connection 'upgrade';
                proxy_set_header        Host $host;
                proxy_set_header        X-Real-IP $remote_addr;
                proxy_cache_bypass      $http_upgrade;
        }

        listen 443 ssl; # managed by Certbot
        ssl_certificate /etc/letsencrypt/live/cr-prod-app.example.com/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/cr-prod-app.example.com/privkey.pem; # managed by Certbot
        include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
        proxy_connect_timeout 1000s;
        proxy_read_timeout 1000s;
}

server {
        if ($host = cr-prod-app.example.com) {
                return 301 https://$host$request_uri;
        } # managed by Certbot
        listen       80;
        server_name  cr-prod-app.example.com;
        return 404; # managed by Certbot
}

API Server

upstream cr-prod-api {
                server unix:/home/www/app/cr.sock;
        }

server {
        server_name  cr-prod-api;

        location /api {
        proxy_pass              http://cr-prod-api/api;
        proxy_http_version      1.1;
        proxy_set_header        Upgrade $http_upgrade;
        proxy_set_header        Connection 'upgrade';
        proxy_set_header        Host $host;
        proxy_set_header        X-Real-IP $remote_addr;
        proxy_cache_bypass      $http_upgrade;
        }


        listen 443 ssl; # managed by Certbot
        ssl_certificate /etc/letsencrypt/live/cr-prod-api.example.com/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/cr-prod-api.example.com/privkey.pem; # managed by Certbot
        include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

server {
        if ($host = cr-prod-api.example.com) {
                return 301 https://$host$request_uri;
        } # managed by Certbot


        server_name  cr-prod-api.example.com;
        listen 80;
        return 404; # managed by Certbot
}

Thanks for your help in advance!

CodePudding user response:

Turns out the answer was pretty simple, I forgot to whitelist the API server IP address on the database firewall configuration.

  •  Tags:  
  • Related