Warm tip: This article is reproduced from serverfault.com, please click

python-Flask和uwsgi无法从静态文件夹呈现文件

(python - Flask & uwsgi not rendering files from static folder)

发布于 2020-11-28 15:47:46

我正在按照本教程进行操作,一切都很好,但是该应用程序未呈现静态文件夹中的文件(css,图像,脚本等)。

我对文件夹(sudo chmod a + rwx static)和子文件夹赋予了所有权限,但仍然没有渲染。

我正在使用的代码是以前使用几乎相同的配置进行工作的代码(回想起以前的Ubuntu 18.04)

app = Flask(__name__,static_url_path='',static_folder="static")

而我的nginx配置文件:

 location / {
    include uwsgi_params;
    uwsgi_pass unix:/home/sydney/ecocathlon/ecocathlon.sock;
}
 location /static {
     root /home/sydney/ecocathlon/static;
 }

你对我如何允许程序访问文件夹有任何想法吗?

提前致谢,

悉尼河

Questioner
Sydney R
Viewed
0
v25 2020-11-29 00:52:08

使用alias代替root

location /static {
     alias /home/sydney/ecocathlon/static;
}

基于这个答案

另外,我将static_url_path不使用你的应用程序定义,也static_folder因为你只是将其设置为默认值:

app = Flask(__name__)

static_url_path控制应用程序如何生成指向静态文件的URL,因此你将迫使它生成链接之类的方法,/something.js而不是将/static/something.js其设置为空字符串。