Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask Nginx uWSGI 504 Gateway Timeout Error

I'm desperately trying to figure out the issue of why I cannot deploy my flask application with uWSGI nginx and Ubuntu server.

Here is my error in nginx error log:

2019/05/12 09:00:19 [error] 10154#10154: *1 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 24.27.57.18, server: itinareport.tk, request: "POST /login HTTP/1.1", upstream: "uwsgi://unix:/home/pinchrep2/itinarep/itinarep.sock", host: "www.itinareport.tk", referrer: "http://www.itinareport.tk/login"

Upon having everything setup with flask uWSGI and nginx I'm able to put my domain name in and it takes me to my web page (Login Page). Upon attempting to login the page pauses for some time and then I receive the 504 Gateway Time-Out

Please advise me on what I should do. This is my first time ever deploying an application and it's been taking me about a week of trying different configurations. I should have you know that I have my flask application connecting to a database through ssh tunnel to another server. Not sure if that would cause this issue as well. Please help if possible or point me in the right direction.

Here is my server block configuration file

server{
    listen 80;                                                                                                                           
    server_name itinareport.tk www.itinareport.tk;                                                                                       

    location / {                                                                                                                         
    include uwsgi_params;                                                                                                            
    uwsgi_pass unix:/home/pinchrep2/itinarep/itinarep.sock;                                                                          
}                                                                                                                                    

Here is my service file:

[Unit]                                                                                                                                  
Description=uWSGI instance to serve itinarep
After=network.target

[Service]
User=pinchrep2
Group=www-data
WorkingDirectory=/home/pinchrep2/itinarep
Environment="PATH=/home/pinchrep2/itinarep/it_venv/bin"
ExecStart=/home/pinchrep2/itinarep/it_venv/bin/uwsgi --ini itinarep.ini

[Install]
WantedBy=multi-user.target  

Here is my ini file

[uwsgi]
module = wsgi:app

master = true
processes = 5

socket = itinarep.sock
chmod-socket = 660
vacuum = true

die-on-term=true 
like image 811
Andrew Venson Avatar asked Oct 30 '25 04:10

Andrew Venson


1 Answers

Sometimes when deploying a flask app with library like Keras and others they have some issue with threading in uWSGI.

You can change a few things in myproject.ini to make it work:

  1. add these setting in myproject.ini
master = false <-------- this
processes = 1 <--------- and this
  1. add cheaper = 0 to your myproject.ini
  2. add lazy-apps = true to your myproject.ini

So now your myproject.ini file may look like:

[uwsgi]
module = wsgi:app

master = false <-------- this
processes = 1 <--------- and this
socket = myproject.sock
chmod-socket = 660
vacuum = true
die-on-term = true

cheaper = 0 <----------- also this, if option 1 is not enough or doesn't work

lazy-apps = true <------ in some case, this might help
like image 150
Khushhalm Avatar answered Nov 01 '25 18:11

Khushhalm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!