Flask应用本地正常,部署至Heroku后出现500错误求助
Hey there, let's figure out why your Flask app is throwing a 500 error on Heroku even though it works locally. Looking at your Heroku logs, uWSGI starts up successfully and the app is marked as "up", so the issue is occurring when handling the actual request—not during initial startup. Here's how to debug this step by step:
1. Enable Detailed Error Messages
First, you need to see the actual error stack trace instead of just a generic 500. You can do this two ways:
- Set the
FLASK_DEBUGenvironment variable on Heroku: Runheroku config:set FLASK_DEBUG=1(note: don't leave this enabled for production, it's only for debugging). - Or temporarily add
app.debug = Trueto your Flask app code before deploying—this will show the full error page when you visit the site.
2. Verify Environment Variables
Local Flask apps often rely on environment variables (like database URIs, secret keys, API keys) that might not be set on Heroku.
- Run
heroku configto list all environment variables set on your Heroku app. - Compare this list to the variables you use locally (check your
.envfile or whatever you use for local config). Any missing variables could cause crashes when the app tries to access them.
3. Check Full Application Logs
The logs you provided only show uWSGI startup and request status—they don't include the actual error from your Flask app.
- Use
heroku logs --tailto stream real-time logs as you make requests to your app. This will capture any exceptions thrown by your Flask code. - If you haven't already, add logging to your Flask app to explicitly log errors. For example:
This will ensure error details get logged to Heroku's logs.import logging from flask import Flask app = Flask(__name__) logging.basicConfig(level=logging.DEBUG) @app.route('/') def index(): try: # Your existing route logic here except Exception as e: app.logger.error(f"Error in index route: {str(e)}", exc_info=True) raise
4. Confirm Dependencies Are Complete
Make sure your requirements.txt includes every package your app needs. Sometimes local environments have globally installed packages that aren't listed in requirements.txt.
- Regenerate your requirements file locally with
pip freeze > requirements.txt, then commit and redeploy to Heroku. - Check Heroku's build logs (run
heroku logs --source build) to ensure all packages install successfully—missing or failed installs can cause runtime errors.
5. Validate Port Configuration
Heroku assigns a dynamic port to your app, so your uWSGI or Flask setup must respect the PORT environment variable. Looking at your logs, uWSGI is binding to port 46173 which matches Heroku's dynamic port, but double-check your uwsgi.ini to ensure it's using the port correctly. For example:
[uwsgi] http-socket = :$(PORT) module = your_app_module:app master = true processes = 1
6. Test a Minimal Route
To isolate the issue, create a simple test route that doesn't rely on external services (like databases):
@app.route('/test') def test(): return "Hello from Heroku!"
Deploy this change and visit /test. If this works, the problem is in your original route logic—likely something that works locally but fails on Heroku (like file paths, database connections, or external API calls).
Your Original Logs for Reference
2020-07-19T15:26:53.980308+00:00 heroku[web.1]: Starting process with command `uwsgi uwsgi.ini` 2020-07-19T15:26:56.005660+00:00 app[web.1]: [uWSGI] getting INI configuration from uwsgi.ini 2020-07-19T15:26:56.005777+00:00 app[web.1]: *** Starting uWSGI 2.0.19.1 (64bit) on [Sun Jul 19 15:26:56 2020] *** 2020-07-19T15:26:56.005777+00:00 app[web.1]: compiled with version: 7.5.0 on 19 July 2020 10:11:11 2020-07-19T15:26:56.005780+00:00 app[web.1]: os: Linux-4.4.0-1074-aws #78-Ubuntu SMP Mon Jun 15 03:53:20 UTC 2020 2020-07-19T15:26:56.005781+00:00 app[web.1]: nodename: 7626bfb4-732b-4bf1-aaee-9c0f34c4f14e 2020-07-19T15:26:56.005784+00:00 app[web.1]: machine: x86_64 2020-07-19T15:26:56.005803+00:00 app[web.1]: clock source: unix 2020-07-19T15:26:56.005804+00:00 app[web.1]: pcre jit disabled 2020-07-19T15:26:56.005804+00:00 app[web.1]: detected number of CPU cores: 8 2020-07-19T15:26:56.005806+00:00 app[web.1]: current working directory: /app 2020-07-19T15:26:56.005808+00:00 app[web.1]: detected binary path: /app/.heroku/python/bin/uwsgi 2020-07-19T15:26:56.005833+00:00 app[web.1]: your processes number limit is 256 2020-07-19T15:26:56.005833+00:00 app[web.1]: your memory page size is 4096 bytes 2020-07-19T15:26:56.005851+00:00 app[web.1]: detected max file descriptor number: 10000 2020-07-19T15:26:56.005851+00:00 app[web.1]: lock engine: pthread robust mutexes 2020-07-19T15:26:56.006016+00:00 app[web.1]: thunder lock: disabled (you can enable it with --thunder-lock) 2020-07-19T15:26:56.006504+00:00 app[web.1]: uwsgi socket 0 bound to TCP address :46173 fd 3 2020-07-19T15:26:56.006560+00:00 app[web.1]: Python version: 3.7.7 (default, May 19 2020, 10:55:09) [GCC 7.5.0] 2020-07-19T15:26:56.022083+00:00 app[web.1]: *** Python threads support is disabled. You can enable it with --enable-threads *** 2020-07-19T15:26:56.022084+00:00 app[web.1]: Python main interpreter initialized at 0x559e3e177740 2020-07-19T15:26:56.022088+00:00 app[web.1]: your server socket listen backlog is limited to 100 connections 2020-07-19T15:26:56.022089+00:00 app[web.1]: your mercy for graceful operations on workers is 60 seconds 2020-07-19T15:26:56.022258+00:00 app[web.1]: mapped 145840 bytes (142 KB) for 1 cores 2020-07-19T15:26:56.022319+00:00 app[web.1]: *** Operational MODE: single process *** 2020-07-19T15:26:56.107306+00:00 heroku[web.1]: State changed from starting to up 2020-07-19T15:26:58.287306+00:00 app[web.1]: WSGI app 0 (mountpoint='') ready in 2 seconds on interpreter 0x559e3e177740 pid: 4 (default app) 2020-07-19T15:26:58.287370+00:00 app[web.1]: *** uWSGI is running in multiple interpreter mode *** 2020-07-19T15:26:58.287371+00:00 app[web.1]: spawned uWSGI master process (pid: 4) 2020-07-19T15:26:58.289069+00:00 app[web.1]: spawned uWSGI worker 1 (pid: 8, cores: 1) 2020-07-19T15:27:02.000000+00:00 app[api]: Build succeeded 2020-07-19T15:28:31.907010+00:00 heroku[router]: at=info method=GET path="/" host=equities-trade-capture.herokuapp.com request_id=e49b77fc-d2ca-4dc6-9c10-6b1ece7ffba6 fwd="103.143.147.150" dyno=web.1 connect=1ms service=183ms status=500 bytes=389 protocol=https 2020-07-19T15:28:31.907650+00:00 app[web.1]: {address space usage: 287412224 bytes/274MB} {rss usage: 54067200 bytes/51MB} [pid: 8|app: 0|req: 1/1] 10.37.161.80 () {60 vars in 1151 bytes} [Sun Jul 19 15:28:31 2020] GET / => generated 290 bytes in 182 msecs (HTTP/1.1 500) 2 headers in 99 bytes (1 switches on core 0) 2020-07-19T15:28:35.710404+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=equities-trade-capture.herokuapp.com request_id=d1beeb28-8d7d-4bd2-a29d-f7cd4be447b0 fwd="103.143.147.150" dyno=web.1 connect=1ms service=23ms status=500 bytes=389 protocol=https
内容的提问来源于stack exchange,提问作者Hemant Satam

