Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx http auth request module will only return default error pages

Tags:

nginx

I am using nginx as a single point of entry to my entire solution.

my goal is to send some url to be authenticated before continuing.

I have found a module named: ngx_http_auth_request_module which suits right in place to solve my problem.

http://nginx.org/en/docs/http/ngx_http_auth_request_module.html

i am compiling my nginx like this: ./configure --with-http_auth_request_module

my code looks like this:

http {

    server {
        listen       8080 default_server;

        server_name _;

        location /api {
            auth_request /auth;
            proxy_pass  http://localhost:8000;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
        }

        location = /auth {
            proxy_pass http://localhost:8000/auth/user;
            proxy_pass_request_body off;
            proxy_set_header Content-Length "";
            proxy_set_header X-Original-URI $request_uri;
        }

    }
}

my problem is that if my server returns and error, like 401, then a default page gets shown. what i want is to be able to return the json that was returned from my authentication service. this is useless without this. what is the point of showing just a generic 401 page? i want to return my proxied json response.

please help.

like image 600
lobengula3rd Avatar asked Oct 19 '22 16:10

lobengula3rd


1 Answers

auth_request is just for authentication. This little hack should work for you

error_page 401 /auth;

After auth error it'll go to /auth location again, this time as ordinary request.

like image 170
SuddenHead Avatar answered Oct 27 '22 18:10

SuddenHead