nginx - 使用 Nginx 保护烧瓶应用程序中的一条路由的密码
问题描述
我有一个 Flask restful 服务,我想使用 nginx 使用用户名/密码保护一个特定的路由。当用户输入网址时,他会得到一个 excel 文件(报告)。我已按照本教程创建简单的身份验证。我已经添加了那里提到的参数,但没有效果。这是我的配置文件:
server {
listen 80;
real_ip_header X-Forwarded-For;
set_real_ip_from 127.0.0.1;
server_name localhost;
root /var/www/html/psycho-test-rest/psycho_front/dist;
location /tests/get_all_users {
include uwsgi_params;
uwsgi_pass unix:/var/www/html/psycho-test-rest/socket.sock;
uwsgi_modifier1 30;
auth_basic "Restricted Content";
auth_basic_user_file /etc/nginx/.htpasswd;
}
location ~ ^/(tests|CRUD)/ {
include uwsgi_params;
uwsgi_pass unix:/var/www/html/psycho-test-rest/socket.sock;
uwsgi_modifier1 30;
}
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
/tests/get_all_users 是我要保护的位置。当我在浏览器中输入 url 时,我得到了文件,但 nginx 不要求输入密码。配置文件有问题吗?我不确定这是否重要,但尚未启用 https。在我为这条路线设置密码后,我正在考虑设置它。
我在 ubuntu 16.04 上进行部署。
解决方案
login_required
在那条路线上使用像“”这样的装饰器:
def loginRequired():
if not session.get('logged_in'):
if not request.endpoint == 'dashboard_route.loginPage':
return redirect(url_for('dashboard_route.loginPage'))
@loginRequired
@mod.route("/")
def dashHome():
return render_template('index.html')
推荐阅读
- delphi - 从 Delphi 程序在 Edge 中打开本地文件
- php - simplexml_load_file 无法从 URL 获取数据
- powershell - 带有 powershell 的 Azure DevOps 任务内联脚本不会因错误而使任务失败
- nix - Cardano-node nix-build 错误:未找到选择路径“scripts.ff.node”中的属性“ff”
- r - 如何将频率表转换为数据框以制作条形图?
- ios - 如何快速从另一个视图控制器设置按钮的标题?
- javascript - TensorFlowJS 需要 HTML 吗?或者我可以从我的电脑上传图片和视频吗?
- ios - 为什么存档中缺少我的 swift 应用程序图标
- flutter - Flutter - 使用 GestureDetector 在两个图像之间切换
- amazon-web-services - ECS 任务定义部署中的多个容器与 github 操作