flask - 无法访问部署在 Apache2 上的本地静态资源 Flask 应用程序
问题描述
我已经在 Ubuntu 16.04 x64(阿里巴巴云)的 Apache2 上使用 mod_wsgi 部署了一个简单的 Flask 应用程序。我在同一个 ubuntu 服务器实例上使用 mysql 作为我的数据库服务器。
我将本地静态资源文件保存在应用程序的静态目录中。每当我调用远程资源时,它都很好,但我的本地资源(即 css 文件、图像等)并没有发生同样的事情。
我知道默认情况下,静态目录对于烧瓶来说是静态的。在我的本地计算机中,一切正常。这是我尝试从 jinja2 模板获取静态资源的方式。
<div id="loader-div2"><img src={{ url_for('static',filename='simple_gif.gif') }}></div>
在我看来(myapp.py)到 jinja2 模板,这里是 url 端点:
app = Flask(__name__,static_url_path='/static')
@app.route('/')
def home():
return render_template('my_template.html')
# I've called this at the end
app.run(debug=False)
在我的 wsgi 文件中
#!/usr/bin/python
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/FlaskApp/")
from myapp import app as application
文件夹结构
/var/www/FlaskApp
|-myapp.py
|-flaskapp.wsgi
| |-static
| |-simple_gif.gif
| |-templates
| |-my_template.html
问题回顾: 无法访问 Apache2 服务器中的静态资源,但可以在本地机器中访问。如果我尝试执行http://XX.XX.XX.XX/static/simple_gif.gif ,我总是会得到 404 状态。出于同样的原因,我也无法访问那里的 css 文件。我被严重困在这里。任何帮助将不胜感激。
提前致谢
编辑 这是 apache 的配置
<VirtualHost *:80>
ServerName 127.0.0.1
WSGIScriptAlias / /var/www/FlaskApp/flaskapp.wsgi
<Directory /var/www/FlaskApp/>
Order allow,deny
Allow from all
Require all granted
</Directory>
Alias /static /var/www/FlaskApp/static
<Directory /var/www/FlaskApp/static/>
Order allow,deny
Allow from all
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
Edit2 我已经解决了这个问题。这是另一个让 apache 感到困惑的 apache conf 文件。非常感谢您的关注。
解决方案
推荐阅读
- c# - 从中心对称更新 WinForms 标签中的文本
- rust - 如何固定 RefCell 内容?
- visual-studio - 使用 Web 内容自定义 VSTS Wiki
- asp.net - ASP.NET Core Razor Pages 项目 - 已检测到不适用于集成托管管道模式的 ASP.NET 设置
- python - 不使用元组的多索引列的 set_index
- c - 在C中参考命名的结构成员直接初始化或分配变量数组的结构成员?
- javascript - 为什么没有将类添加到加载的导航元素?
- javascript - 如何在 Gutenberg 中使用序列化数据?
- sql-server - 将 case 语句切换为常量会大大降低查询速度
- visual-studio - 为什么 .NET Core 3.1 在我安装后没有出现在 Visual Studio 中?