By thatgurjot
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 –
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.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Hello,
These issues could potentially be due to a number of different factors, so here are a few things you can do to try and troubleshoot your Flask application.
Check the browser console for JavaScript errors: If your JavaScript elements are often unresponsive or slow to respond, there might be some errors happening in the client-side code. Open the browser’s console (usually through the developer tools) and check if there are any errors logged there when you use your application.
Check your server logs: Both Gunicorn and Nginx log errors and important events, and these logs can often provide insights about issues with your application. The Gunicorn logs will be most useful for issues happening in your Flask application itself, while the Nginx logs can help diagnose issues with the connection between Nginx and Gunicorn, or between the client and Nginx. The default locations for these logs are /var/log/nginx/error.log for Nginx and syslog (visible through the journalctl command) for Gunicorn when running as a systemd service.
Try using more Gunicorn workers: Your Gunicorn configuration is currently set to use 3 worker processes. If your application is CPU-intensive or has long-running requests, this might not be enough to handle all incoming requests, especially if you have significant traffic. You can increase the number of workers in the Gunicorn command in your systemd service file (--workers 3).
Check your AJAX calls: The page reload loop might be caused by the AJAX calls in your JavaScript code. Perhaps there’s an error in the response that’s causing the JavaScript code to refresh the page. You can inspect these AJAX calls and their responses using the Network tab in your browser’s developer tools.
Check your Flask route handling: Make sure your Flask routes are correctly handling all possible request scenarios and returning appropriate responses. The infinite page reload loop could potentially be caused by an issue in your route handling code.
Best,
Bobby
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.