Question

How to fix infinite page reload? [Flask app with Gunicorn and Nginx on Ubuntu 18.04]

Posted March 9, 2020 415 views
NginxPythonPython FrameworksUbuntu 18.04

Hi,

I deployed a Flask app on an Ubuntu 18.04 VPS using Gunicorn and Nginx as the driving servers. I followed this official tutorial for help. I am using a separate server block for my domain on Nginx and not using the default.

The website loads just fine on my domain. However, there are two issues –

  1. The JavaScript elements are often unresponsive or take too long to respond.
  2. On a particular route, the app seems to enter an infinite page reload loop.

Interestingly, none of these issues exist when I run the Gunicorn instance on localserver (port 5000).

Details about the infinitely reloading route are –

It’s a separate blueprint for displaying and browsing tags that looks like this - @tags_bp.route('/<language>/tags/<selected_tags>', methods=['GET']).
This route renders a template with some responses.
There’s an allied route - @tags_bp.route('/<language>/tags/<selected_tags>/<variable_ID>', methods=['GET']) - that only returns a JSON object response to AJAX calls made by the JavaScript that then gets rendered on the page.


I am using the default .serivce and location block as shown below.
/etc/systemd/system/myproject.service

[Unit]
Description=Gunicorn instance to serve myproject
After=network.target

[Service]
User=sammy
Group=www-data
WorkingDirectory=/home/<USER>/myproject
Environment="PATH=/home/<USER>/myproject/myprojectenv/bin"
ExecStart=/home/<USER>/myproject/myprojectenv/bin/gunicorn --workers 3 --bind unix:myproject.sock -m 007 wsgi:app

[Install]
WantedBy=multi-user.target

/etc/nginx/sites-available/myproject

server {
    listen 80;
    server_name your_domain www.your_domain;

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/<USER>/myproject/myproject.sock;
    }
}

I am suspecting the error is occurring somewhere between Nginx and Gunicorn. I have tried modifying the proxy_headers using this configuration on the Flask documentation but that didn’t help either.

Would greatly appreciate any help to fix this!

Cheers.

Submit an answer

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!