如何在 Flask gunicorn nginx 设置中使用 send_static_file?

How to use send_static_file in Flask gunicorn nginx setup?

我想从我的 Flask 应用程序调用 send_static_file,它引用静态 html 文件。该代码在本地主机上运行良好,但是当我使用 nxingx 和 gunicorn 进行设置时,出现错误:

404 URL Not Found error.

我的烧瓶应用路由到:

from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from .frontend import frontend

application = Flask(__name__, instance_relative_config=True)

application.register_blueprint(frontend, url_prefix='/app')

@application.route('/')
def hello_world():
    return "hello world" ------>>>>>THIS WORKS
    #return application.send_static_file('frontend/static/dist/index.html') ------>>>>>>>THIS DOES NOT WORK

我的 nginx 配置如下所示:

server {
    listen 80;
    server_name 52.34.18.48;
    error_log /var/www/appname/nginx_errorlog.log;
    access_log /var/www/appname/nginx_accesslog.log;

    root /var/www/appname;

    location / {
        proxy_pass http://127.0.0.1:8000;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

当我只是return一个字符串时,显示的是字符串,但是当我return一个静态文件内容时,我得到错误。

更新 1:

运行 调试中的烧瓶 returns:

  • Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) * Restarting with stat * Debugger is active!
  • Debugger pin code: XXX-XXX-XXX 127.0.0.1 - - [29/Jan/2016 04:09:20] "GET / HTTP/1.0" 200 - 127.0.0.1 - - [29/Jan/2016 04:09:29] "GET /app HTTP/1.0" 301 - 127.0.0.1 - - [29/Jan/2016 04:09:29] "GET /app/ HTTP/1.0" 404 -

我做错了什么?

这个函数是Flask内部使用的。我不建议以这种方式使用它。

如果您需要提供静态文件,我建议配置 Nginx 来为您处理。这对于站点的某些部分非常高性能的情况非常有用!

如果需要使用Flask,请看send_from_directory。这可能就是您所需要的。