`504 Gateway Time-out` when sending emails with Djoser and Django in production
I am encountering an issue in my Django production environment when using Djoser for user registration and login management. When a user requests to create an account, the API takes about a minute and then returns a 504 Gateway Time-out
error. In my local environment, the process works correctly, and the activation email is sent without issues.
What I've Tried:
Increased timeout options in Nginx.(no effect)
server { listen ${LISTEN_PORT}; location /static { alias /vol/web/static; } location / { uwsgi_pass ${APP_HOST}:${APP_PORT}; include /etc/nginx/uwsgi_params; client_max_body_size 10M; # new code uwsgi_read_timeout 120; proxy_read_timeout 300s; proxy_connect_timeout 300s; proxy_send_timeout 300s; } }
Added logs in the email-sending function, and the function appears to be called correctly.
class ActivationEmail(email.ActivationEmail): template_name = "user/email/activation.html" def send(self, to, *args, **kwargs): print(f"Sending activation mail to {to}") try: super().send(to, *args, **kwargs) except: print(f"Couldn't send mail to {to}") raise print(f"Activation mail sent successfully to {to}")
I also checked environment variables to ensure they are consistent between local and production environments. The email configuration is the same both locally and in production. However, in production, the email does not arrive in the inbox, and the 504 error persists.
I think the error came from nginx since locally I have no nginx configuration. That's is the major difference between the two environments. The changes I did in timeout options in nginx render no effect since the API always wait one minute to respond with the time out error.
Question:
What could be causing the 504 error when sending emails in production? Are there any additional configurations I should check in Nginx or in my email settings? How can I diagnose and resolve this issue?