Use Nginx to add login verification to an existing Web site (without adding changes to existing site code)

Source: Internet
Author: User
Tags auth
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.