首页 > 解决方案 > 使用 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 上进行部署。

标签: nginxflask

解决方案


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')

推荐阅读