Add validation functionality without changing existing site code:
1. Assuming the existing Web site backend Nodejs, the port 3000,nginx configuration is as follows
server {
listen ;
server_name localhost;
Location/{
proxy_pass http://localhost:3000;
}
2. To add validation to the site now, you need to do the following without changing the original code:A. First you need to add the Http_auth_request module (attachment) for Nginx, and if Nginx version 1.5.4+ you do not need this module B. Create a new Node.js Express Web site, Port 3001, and add login verification features: C. Modify the Nginx configuration file as follows:
server {
listen;
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;
}
principle: access to port 80, first sent by Auth_request to the/auth authentication request, if it is validated then return 200,nginx continue to send the request to 3000 port, if/auth return 403, Then Nginx will not send the request to Port 3000, and finally define the 403 location.