Using Flask-Login to Authenticate Nginx Reverse Proxy
我正在实施Duo Labs的py_webauthn演示的修改版,以便向我的网站添加物理认证。该演示是在Flask中构建的,并将
我理想地希望Flask应用程序充当Nginx身份验证器,以便已登录的用户能够访问服务器上的其他
我最初曾尝试在Flask中实现反向代理,但是我发现的唯一可行的解??决方案需要Twisted框架(因为像Shellinabox之类的服务需要不断发出请求),因此需要建立一个完全独立的WSGI应用程序-不必要难以整合的中间人。
我的理想结果是使用户最初访问服务器并被
我应该如何整合Flask和Nginx?
(此处提供了演示库的app.py文件。Webauthn功能仅基于
我解决了! 可以将内置的Nginx
要复制,请在Flask中添加一个身份验证器:
1 2 3 4 5 6 | @app.route("/auth") def nginx_auth(): if current_user.is_authenticated: return"You are logged in! Sweet!" else: return 'Sorry, but unfortunately you\'re not logged in.', 401 |
然后,在Nginx中,将
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | location /ssh { auth_request /auth; proxy_pass http://localhost:4200/; } location = /auth { internal; proxy_pass https://localhost:8081/auth; proxy_pass_request_body off; proxy_set_header Content-Length""; proxy_set_header X-Original-URI $request_uri; } error_page 401 = @error401; location @error401 { return 302 /login; } |
(从Nginx文档中修改的代码。)