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

使用Flask+Nginx+uWSGI部署时POST请求出现内部服务器错误

Flask+uWSGI+Nginx部署后POST请求500错误排查

背景

用Flask开发的POST API,触发Selenium抓取数据并返回给前端,未配置Nginx时直接在服务器本地端口5000运行完全正常。

问题

按教程用Nginx和uWSGI部署Flask应用后,发送POST请求http://my_domain/find_data时始终返回内部服务器错误。

相关代码与配置

wsgi.py代码

from app import app
if __name__ == "__main__":
        app.run()

app.py代码(/find_data POST请求处理)

from flask import Flask, send_from_directory, stream_with_context, request, Response
from flask_restful import Api, Resource, reqparse
from flask_cors import CORS #部署时注释此行
from data_finder import DataFinder
from time import sleep
from contextlib import closing
import requests
import json 
from flask import jsonify
from flask import jsonify, make_response

app = Flask(__name__, static_url_path='', static_folder='frontend/build')
CORS(app) #部署时注释此行
api = Api(app)


@app.route("/")
def hello():
    return send_from_directory(app.static_folder, 'index.html')

@app.route("/find_data", methods = ['POST'])
def FindData():
    person = request.get_json()['person']
    print(person)
    results = {}
    data_finder = DataFinder()


    results['Spokeo'] = list(data_finder.get_spokeo_data(person))
    results['White Pages'] = list(data_finder.get_whitepages_data(person).keys())

    return make_response(jsonify(results), 200)

project.ini(uWSGI配置)

[uwsgi]
module = wsgi:app

master = true
processes = 5

socket = myproject.sock
chmod-socket = 660
vacuum = true

die-on-term = true

Nginx配置文件

server {
    listen 80;
    server_name my_domain www.my_domain;


    location / {
        include uwsgi_params;
        uwsgi_pass unix:/home/help/data-finder/privaseame_web_app/myproject.sock;
        uwsgi_read_timeout 900;
        proxy_read_timeout 1800;
        proxy_connect_timeout 1800;
        proxy_send_timeout 1800;
        send_timeout 1800;
    }

    client_header_timeout 900s;
    client_body_timeout 900s;
    keepalive_timeout 900s;
    send_timeout 900s;
}

环境信息

  • 服务器:DigitalOcean Ubuntu 18.04 LTS
  • Python版本:3.10.6

排查步骤

  1. 查看错误日志

    • 实时查看Nginx错误日志:tail -f /var/log/nginx/error.log
    • 查看uWSGI服务日志(若用systemd管理):journalctl -u 你的uWSGI服务名,或检查uWSGI配置是否指定了日志文件路径。
  2. 权限问题排查

    • 检查myproject.sock文件权限,确保Nginx进程用户(通常是www-data)有读写权限,可在uWSGI配置中添加uid = www-data和gid = www-data,或把Nginx用户加入uWSGI所属用户组。
    • 确认data_finder.py及相关依赖文件的访问权限,保证uWSGI进程能读取所有必要资源。
  3. Selenium无头环境配置

    • 服务器为无头环境,需给Selenium浏览器添加无头参数(如Chrome的--headless=new),同时确保服务器安装了对应版本的ChromeDriver和Chrome浏览器。
    • 核对服务器与本地的Selenium依赖版本,确保pip安装的包版本一致。
  4. 请求格式校验

    • 确保前端POST请求的Content-Type为application/json,否则request.get_json()会返回None,触发KeyError。可在代码中添加校验逻辑:
      if not request.is_json:
          return make_response(jsonify({"error": "请求必须为JSON格式"}), 400)
      person = request.get_json().get('person')
      if not person:
          return make_response(jsonify({"error": "缺少'person'字段"}), 400)
      
  5. 超时与资源限制

    • Selenium抓取耗时可能较长,uWSGI默认harakiri超时为60秒,超过会被终止进程,可在project.ini中添加harakiri = 900延长超时时间。
    • 检查服务器内存、CPU使用率,排查是否因多进程运行Selenium导致资源耗尽。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 22:20:30