python - 使用 Flask、Nginx 和 uwsgi 获取 404

标签 python nginx uwsgi

我在 /find_bili_user/app.py 中有一个 Flask 应用程序,其 virtualenv 在 /python_envs/env1/ 中,

app.py:

from flask import Flask, jsonify, request, make_response

from biliuser import getdanmu

app = Flask('__name__')


@app.route('/find_bili_user.json', methods=['GET'])
def get_user():
    avid = request.args.get('avid')
    if not avid or not avid.isdigit():
        return invalid_avid()
    else:
        message = getdanmu(int(avid))
        if not message:
            return failed()
        else:
            response = jsonify(message)
            return response


@app.errorhandler(400)
def invalid_avid():
    return make_response(jsonify({'error': 'invalid avid.'}), 400)


@app.errorhandler(400)
def failed():
    return make_response(jsonify({'error': 'failed parsing comments.'}), 400)


if __name__ == '__main__':
    app.run()

我的 uwsgi 配置:

[uwsgi]
chdir = /find_bili_user
app = app
module = %(app)
home = /python_envs/env1
pythonpath = %(chdir) 
socket = %(chdir)/app_uwsgi.sock
chmod-socket    = 666
callable = app
logto = /var/log/uwsgi/%n.log
processes = 4
threads = 2

和 nginx 配置:

server {
    listen      80;
    charset     utf-8;
    client_max_body_size 75M;
    location / {
        include uwsgi_params;
        uwsgi_pass unix:/find_bili_user/app_uwsgi.sock;
    }
}

当我启动 nginx 和 uwsgi 时,我收到以下日志:

*** Starting uWSGI 2.0.13.1 (64bit) on [Tue Jun 21 08:20:59 2016] ***
compiled with version: 4.8.4 on 21 June 2016 07:28:15
os: Linux-3.13.0-83-generic #127-Ubuntu SMP Fri Mar 11 00:25:37 UTC 2016
nodename: vultr.guest
machine: x86_64
clock source: unix
detected number of CPU cores: 1
current working directory: /find_bili_user
detected binary path: /python_envs/env1/bin/uwsgi
chdir() to /find_bili_user
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 5764
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to UNIX address /find_bili_user/app_uwsgi.sock fd 3
Python version: 3.4.3 (default, Oct 14 2015, 20:31:36)  [GCC 4.8.4]
Set PythonHome to /python_envs/env1
Python main interpreter initialized at 0x21e8b30
python threads support enabled
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 332224 bytes (324 KB) for 8 cores
*** Operational MODE: preforking+threaded ***
added /find_bili_user/ to pythonpath.
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x21e8b30 pid: 1503 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (pid: 1503, cores: 2)
spawned uWSGI worker 2 (pid: 1505, cores: 2)
spawned uWSGI worker 3 (pid: 1506, cores: 2)
spawned uWSGI worker 4 (pid: 1507, cores: 2)

我可以访问IP地址并获得nginx欢迎页面,但是xx.xx.xx.xx/find_bili_user.json返回错误404,看来nginx没有向uwsgi。我的配置有问题吗?

最佳答案

已解决。原来我没有删除 /etc/nginx/sites-enabled/default 中的内容。

关于python - 使用 Flask、Nginx 和 uwsgi 获取 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37939111/

相关文章:

python - 为什么我的 Flask 应用程序中的图像无法正常显示?

python - 在散点图上绘制 matplotlib.animation 中的垂直线

php - 如何将 Symfony2 路由转储到 nginx?

python - 当使用 Apache/uWSGI 或 Werkzeug 时,flask json 输出看起来有所不同

python - django 和 uwsgi 上的语言环境 UnicodeEncodeError

python - 除了 (MyClass, self) 之外,您还会将其他任何内容传递给 super() 吗?

python - Apache2 (Ubuntu 16.04) 上的 Django 报告 'No module named mysite.settings'

ruby-on-rails - Rails ajax 文件上传和 nginx

Nginx 动态位置路径配置

django - 如何修复uWSGI内部路由?