title: Nginx not redirecting to url on successful authentication
body: <p>I am working on nginx auth_request module to put an authentication layer on all incoming requests to my server.
  The goal is to pass each url request first to a login page ,check for authorization and then redirect to the specific page mentioned for that url.
  For the authentication , I am using a simple flask app which will return 200 on success, and 401 on error.</p>
  
  <p>The issue I am facing is that on successful authentication, nginx gives a 404 error instead of redirecting to mentioned url.</p>
  
  <pre><code>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;}
  }
  </code></pre>
  
  <p>Also, here is the flask-app login function that I have written.</p>
  
  <pre><code>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
  </code></pre>