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

Flask项目部署至Azure Linux 17服务器后触发内部错误求助

Hey, that 500 internal error is super common when deploying Flask apps to Azure Linux App Services—let’s break down the most likely issues and fix them step by step.

第一步:先抓准具体的错误原因(日志是关键)

Local runs work fine, but server errors almost always leave clues in the logs. Here’s how to get the real error message on Azure:

  • Use Azure Portal Log Stream: Go to your App Service → Monitoring → Log Stream. You’ll see live logs that show exactly what’s crashing (like import errors, permission issues, or missing directories).
  • Use CLI: Run az webapp log tail --name <your-app-name> --resource-group <your-resource-group> in your local terminal to pull real-time logs.
  • Temporary Debug Mode (for troubleshooting only): Set the environment variable FLASK_DEBUG=True in Azure (Configuration → Application settings) or add app.debug = True to your code. This will show the full error stack trace in the browser—but turn this off for production since it’s a security risk.
常见问题及修复方案

Based on your code snippet, here are the top culprits:

1. Deprecated werkzeug.secure_filename Import

In newer versions of Werkzeug (2.0+), werkzeug.secure_filename was moved to werkzeug.utils.secure_filename. If your Azure environment has a newer Werkzeug version than your local setup, this will throw an ImportError and trigger the 500 error.

Fix it by updating your import:

# Replace this
# import werkzeug

# With this
from werkzeug.utils import secure_filename

Then update your save line to use the imported function:

filename = secure_filename(f.filename)

2. File Path & Permission Issues

When you run locally, files save to your current working directory—but on Azure, the working directory might be different, and you might not have write permissions to random folders. Plus, if you’re relying on a folder that exists locally but wasn’t deployed to Azure (like an uploads folder), saving will fail.

Fix this by using a dedicated, guaranteed-writable directory:

import os
from flask import Flask, request
from werkzeug.utils import secure_filename

app = Flask(__name__)

# Create an uploads folder in your app's root directory
UPLOAD_FOLDER = os.path.join(app.root_path, "uploads")
# Make sure the folder exists (Azure won't create it automatically)
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER

@app.route('/database', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        f = request.files['file']
        filename = secure_filename(f.filename)
        # Use the full path to the uploads folder
        file_path = os.path.join(app.config["UPLOAD_FOLDER"], filename)
        f.save(file_path)
        # Pass the full path to readFile instead of just the filename
        Finaldata = readFile(file_path)
        # Rest of your logic...

Azure’s /home/site/wwwroot directory (where your app lives) is always writable, so this path will work reliably.

3. Missing Dependencies or Version Mismatches

If your readFile function uses libraries like pandas, requests, or others, make sure they’re listed in your requirements.txt file—and that the versions match your local environment. Azure installs dependencies from this file during deployment, so missing libraries will cause runtime errors.

Double-check your requirements.txt includes everything, e.g.:

Flask==2.3.3
Werkzeug==2.3.7
pandas==2.1.4
requests==2.31.0

4. Network Issues in readFile

If readFile calls an external API, Azure’s server might not have access to it (due to firewall rules or network restrictions). Test this by using the Kudu console (App Service → Development Tools → Advanced Tools → Go):

  • Run curl https://your-api-url.com to see if the API is reachable from the server.
  • If it’s blocked, you might need to add Azure’s outbound IPs to the API’s allowlist, or check if your App Service has a VNet setup that restricts internet access.
Final Check

After making these changes, redeploy your app and test again. If you still get errors, go back to the logs—they’ll tell you exactly what’s wrong.

内容的提问来源于stack exchange,提问作者SineCo

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 06:27:33