You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu环境下部署Flask SSL服务器:Nginx与Gunicorn通信异常及域名访问超时问题求助

Troubleshooting ERR_CONNECTION_TIMED_OUT for Flask + Nginx + SSL Setup on Ubuntu

Let's break down and fix your issue step by step — the timeout error points to a few clear areas we can check and resolve quickly.

1. Fix the Port Mismatch (Immediate Quick Win)

Looking at your configs, there's a critical mismatch:

  • Your Nginx config forwards traffic to http://127.0.0.1:8000
  • But you're running your Flask test app on port 5000

Nginx is trying to talk to a port where nothing's listening, which is a big part of the problem. Fix this in one of two ways:

  • Option 1: Update your Flask code to use port 8000
    if __name__ == "__main__":
        app.run(host="0.0.0.0", port=8000, debug=True)
    
    Then restart your Flask app with python3 test.py.
  • Option 2: Update Nginx to forward to port 5000
    Edit your /etc/nginx/sites-enabled/flask_app file, change:
    proxy_pass http://127.0.0.1:8000;
    
    to:
    proxy_pass http://127.0.0.1:5000;
    
    Then validate the Nginx config and restart:
    sudo nginx -t
    sudo systemctl restart nginx
    

2. Open Firewall & Cloud Security Group Ports

ERR_CONNECTION_TIMED_OUT almost always means external traffic can't reach your server's 80 (HTTP) and 443 (HTTPS) ports.

Check Ubuntu's UFW Firewall

  • List current rules: sudo ufw status
  • If 80/tcp and 443/tcp aren't allowed, add them:
    sudo ufw allow 80/tcp
    sudo ufw allow 443/tcp
    sudo ufw reload
    

Check Cloud Provider Security Groups

If you're using a cloud server (AWS, GCP, Azure, etc.), don't forget to update your security group/network ACL to allow inbound traffic on ports 80 and 443. This is one of the most commonly missed steps!

3. Validate Nginx is Running Correctly

Even if your config looks right, confirm Nginx is working as expected:

  • Check config syntax for errors: sudo nginx -t (fix any reported issues immediately)
  • Check Nginx status: sudo systemctl status nginx — it should show active (running)
  • Inspect logs for clues:
    sudo tail -f /var/log/nginx/error.log
    sudo tail -f /var/log/nginx/access.log
    

You're using Flask's built-in development server, which isn't designed for production use. Since you mentioned Gunicorn, let's set that up properly:

Step 1: Install Gunicorn

pip3 install gunicorn

Step 2: Run Flask with Gunicorn on Port 8000

gunicorn --bind 127.0.0.1:8000 test:app

Here, test is your Python filename (test.py), and app is your Flask instance name.

Step 3: Set Up Gunicorn as a Systemd Service (Auto-Start on Boot)

Create a service file to keep Gunicorn running in the background:

sudo nano /etc/systemd/system/flask_app.service

Add this content (replace placeholders with your actual username and app directory):

[Unit]
Description=Gunicorn instance for Flask chatbot app
After=network.target

[Service]
User=your_username
Group=www-data
WorkingDirectory=/path/to/your/app/folder
ExecStart=/usr/bin/gunicorn --workers 3 --bind 127.0.0.1:8000 test:app

[Install]
WantedBy=multi-user.target

Then start and enable the service:

sudo systemctl start flask_app
sudo systemctl enable flask_app
sudo systemctl status flask_app

5. Rule Out Certificate Issues (Less Likely for Timeouts)

Your timeout error isn't typical of certificate problems (cert issues usually throw SSL/TLS-specific errors like NET::ERR_CERT_INVALID). But to be thorough:

  • Verify your Certbot certificate is valid:
    sudo certbot certificates
    
    If it shows Status: Valid, your certificate is working fine.

Final Checks

After making these changes:

  • Confirm your app is listening on port 8000: sudo lsof -i :8000 (you should see the Gunicorn/Flask process)
  • Test connectivity from an external machine: curl -v https://viberchatbot.ga or use telnet viberchatbot.ga 443 to see if the connection can be established.

内容的提问来源于stack exchange,提问作者i.svirchev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 00:57:31