1
server {
    server_name 127.0.0.1;
    listen 3000;

    location = /auth {
        internal;
        proxy_pass http://127.0.0.1:8088/auth;

    }


    location / {
        auth_request /auth;

        # force Nginx to preserver the response proxy_set_header
        auth_request_set $falure_reason $sent_http_x_authenticationfail;

        error_page 401 =200 /login;
        proxy_pass http://127.0.0.1:9000;
    }

    location /login {           
        proxy_pass http://127.0.0.1:8081/sso;
    }
}

I am working on a authentication service. The idea is when a request hit root, it will be authenticated(checking token) through /auth and the auth server sits http://127.0.0.1:8088/auth to handle all auth request. Then if the token is not set, auth server will respond a 401 and error page will catch that and route to /login to proxy to my login server http://127.0.0.1:8081/sso. But the problem is, when I go through / -> /auth -> /login, the request url return from server is still 127.0.0.1:3000/sso instead of ttp://127.0.0.1:8081/sso because afterward I need to send a subsequent post request to http://127.0.0.1:8081/sso with user login id/password. But the nginx point me to 127.0.0.1:3000/sso on the browser.

Jason Liu
  • 11
  • 4

1 Answers1

0

Eventually, I figured out myself. Because the nginx acts as a reverse proxy, the client would only have access to the proxy url 127.0.0.1:3000/sso since nginx takes care of forwarding request and only expose its url to the client instead of its underlying server's url. Therefore, to send the post request back from the client side to http://127.0.0.1:8081/sso, I need to set a sso location in the nginx server to forward all request from 127.0.0.1:3000/sso to http://127.0.0.1:8081/sso as a consistent proxy mechanism.

Jason Liu
  • 11
  • 4