【发布时间】:2016-03-22 23:32:18
【问题描述】:
我已阅读答案 [https://stackoverflow.com/a/20895594/305883],但对我没有帮助。
我有一个烧瓶应用程序,可以在 localhost 中提供模板或在端点 /path/<int:id>/ 进行调试,但在使用 nginx 的生产环境中它会失败 带有 错误 404。
Flask 项目具有默认结构:
app.py
index.html
/templates/mytemplate.html
/static/..
mytemplate.html 将从/static/ 文件夹加载资源,使用 jinja 语法。
已编辑
目标:我希望应用服务:
root
/将提供 index.html/path/将打开“myTemplate.html”并用变量填充它 (神社);如果可能的话,我希望模板中包含静态资产(例如 js, css, images) 由 nginx 提供服务;/api/将提供 api rest。
在我的本地环境中,我使用的是烧瓶服务器,而不是 ningx,并且三个端点按预期工作。
在生产中,我使用烧瓶和 ningx。 根和 /api/ 边缘工作; /path/ ,用于烧瓶中的模板,不返回 error 404。
为了设置 nginx,我按照本教程中的步骤操作: [https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-uwsgi-and-nginx-on-ubuntu-14-04]
flask 中的模板由以下人员提供服务:
@application.route('/path/<int:id>/')
def graph_template(id):
meta = {'og:image':'/url/image.jpg'}
try:
key = decode(id)[0]
except:
# todo replace with error 400
return jsonify({'error':'Something got wrong. ID not found'})
return render_template('mytemplate.html', meta=meta, id = id)
我在调试和发现问题时遇到困难,使/path/显示模板。
nginx 配置
server {
listen 80;
# is this block to serve index on root only ?
# or will search for index files also in routes /path/ in ningx and/or flask?
root /var/www/mySite;
index index.html index.htm;
location /api {
# try_files $uri $uri/ /index.html;
include uwsgi_params;
uwsgi_pass unix:/var/www/mySite/myApp.sock;
# auth_basic "API Login";
# auth_basic_user_file /etc/nginx/pw_file;
# allow 127.0.0.1;
# allow XX.XX.XXX.XXX;
# deny all;
}
location /static {
alias /var/www/mySite/static;
}
}
我尝试了以下方法:
包括一个proxy_pass:
location / {
proxy_pass http://127.0.0.1:8080;
}
结果:导致错误 502
尝试将端口更改为:80
location / {
proxy_pass http://127.0.0.1:80;
}
结果:错误 404
尝试在 /path/ 端点使用 uwsgi_pass 套接字
location /path/ {
include uwsgi_params;
uwsgi_pass unix:/var/www/mySite/myApp.sock;
}
结果:这条路线不存在http://example.com/path/ 我不明白,因为烧瓶应该在这里提供模板 - 至少不是错误配置错误 502。
回答:[Python Flask server crashing on GET request to specific endpoint: 为每个端点显示了两个套接字 - 我需要使用这样的设置吗? 我正在尝试记录自己的套接字是什么并使用 nginx 文档,但很抱歉,我在这方面的能力不强,而且我在黑暗中移动了一点。
您能帮忙调试一下问题吗?
【问题讨论】:
标签: python templates nginx flask http-status-code-404