server { listen 80; server_name localhost; location /{ proxy_pass http://localhost:3000; } }
server { listen 80; server_name localhost; location /{ auth_request /auth; proxy_pass http://localhost:3000; } location /auth{ proxy_pass http://localhost:3001; proxy_pass_request_body off; proxy_set_header Content-Length ""; proxy_set_header X-Original-URI $request_uri; } location /login{ proxy_pass http://localhost:3001; } error_page 403 /login; }
原理:訪問80端口,先由auth_request將request送至/auth驗證請求,若是是驗證了的則返回200,nginx繼續將請求發送至3000端口,若是/auth返回403,則nginx不會將請求發送至3000端口,最後定義一下403的位置便可。node