成功验证后Nginx不会重定向到url

时间:2019-01-31 06:29:00

标签: nginx auth-request

我正在使用nginx auth_request模块,以便在对服务器的所有传入请求上放置一个身份验证层。 目的是首先将每个url请求传递到登录页面,检查授权,然后重定向到该URL提到的特定页面。 对于身份验证,我正在使用一个简单的flask应用程序,如果成功,该应用程序将返回200,如果错误,则返回401。

我面临的问题是,成功进行身份验证后,nginx会显示404错误,而不是重定向到所提到的url。

server {

listen 8080;
server_name _;

location /url_1 {
auth_request /auth_pass;
error_page 401 = @error401;

auth_request_set $user $upstream_http_x_user;
auth_request_set $auth_status $upstream_status;
proxy_set_header x-user $user;

#the aim here is to redirect to google.com on successful authentication.
proxy_pass  http://www.google.com;
}

location = /auth_pass {
internal;
proxy_pass http://flask_app_server_address/login;
proxy_pass_request_body off;

proxy_set_header Content-Length "";
proxy_set_header X-Original-URI $request_uri;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded_for $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;

}

#error_page 401 = @error401;

location @error401{
        return 302 http://flask_app_server_address/login?url=$request_uri;}
}

这也是我编写的flask-app登录功能。

def login():

    if request.method=="GET":
        redirect_url=request.args.get('url')
        if flask_login.current_user.is_authenticated:
            return app.response_class(status=200)
        print "failed is_authenticated check"
        return render_template('login.html',redirect_url=redirect_url),401

    else:
        data= request.form
        username=data['username']
        password=data['password']
        redirect_url=data['requestParam']
        user=User.query.filter_by(login=username).first()
        if user is None or not (check_password_hash(user.password,password) or (user.password==password)):
            print "failed to login"
            return redirect('http://server_ip'+redirect_url),401

        flask_login.login_user(user)
        return redirect('http://server_ip'+redirect_url),302

0 个答案:

没有答案