如何使用 Nginx 提供 Flask 静态文件? [英] How to serve Flask static files using Nginx?

查看:53
本文介绍了如何使用 Nginx 提供 Flask 静态文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有这种结构的 Web 应用程序:

I've a web application with this structure:

|
|__ static
   |__style.less
   |__images
|__ myapp.py
|__ wsgi.py

我已经设法使用 nginx 和 wsgi 运行了 Web 应用程序,但问题是没有提供静态文件,我的意思是,当我转到它们的 URL 时,服务器找不到它们.它给了我 404.

I've managed to run the web application using nginx and wsgi, but the problem is that the static files are not served, i mean, the server can't find them when i go to their URL. It gives me 404.

这是我的 nginx 配置文件部分:

Here's my nginx configuration file part:

 server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /var/www/public_html;
    index index.php index.html index.htm;

    server_name xxxxxxx.com;

    location / {
        try_files $uri $uri/ =404;
    }

    location /myapp {
        include uwsgi_params;
        uwsgi_pass unix:/var/www/public_html/myapp/myapp.sock;
}

有什么遗漏吗?

推荐答案

将此添加到您的 nginx 配置中

Add this to your nginx configuration

location ^~ /static/  {
    include  /etc/nginx/mime.types;
    root /project_path/;
}

用你的应用的绝对路径替换/project_path/,你应该注意它不包括静态目录和里面的所有内容/project_path/static/ 将在 url /static/ 中提供服务.

replace /project_path/ with your app's absolute path, you should note that it doesn't include static directory and all the contents inside /project_path/static/ will be serverd in url /static/.

这篇关于如何使用 Nginx 提供 Flask 静态文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆