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.