Enable/disable authentication based on the client IP address in Nginx

Source: Internet
Author: User

A few days ago, we helped the company build a navigation page for internal resources, allowing employees to access various common systems. Because this page contains some sensitive information, we want to authenticate it, but it is only enabled when it is accessed from the internet. When it is accessed from the company intranet, you do not need to enter the account password.
According to this requirement, I studied Nginx configuration and found that the satisfy command can solve this problem well. The following figure shows our configuration:

The code is as follows: Copy code

Server {
Listen 80;
Server_name intra.foobar.com;
Charset UTF-8;

Satisfy any;
Allow 192.168.0.0/22;
Allow 111.222.333.1/32;
Deny all;
Auth_basic "Intranet ";
Auth_basic_user_file/usr/local/nginx/conf/intranet-htpasswd;

Location /{
Root/usr/local/www/intranet;
Index index.html;
    }
}

Satisfy acts on all access phase handler. There are two parameter values: all and any, which indicate that all rules are satisfied and any rule is satisfied. Set it to any and use auth_basic and access modules to enable/disable authentication based on IP addresses.
Taking our rules as an example, the intranet segment 192.168.0.0/22 and the Client of 111.222.333.1 (fictitious) can be accessed, while other users need to perform authentication.
In the official document, satisfy supports ngx_http_access_module, ngx_http_auth_basic_module, and ngx_http_auth_request_module. It is not clear whether third-party authentication modules such as ngx_http_auth_digest and satisfy can work.

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.