nginx的訪問控制主要分為兩類:
- 基于IP的訪問控制 http_access_module
- 基于用戶的信任登錄 http_auth_basic_module
對于http_access_module模塊:
模塊允許限制訪問某些客戶端地址。訪問也可以通過密碼子請求結(jié)果或JWT來限制滿足控制地址和密碼的同時訪問限制。
配置語法:
Syntax: allow address | CIDR | unix: | all;
Default: —
Context: http, server, location, limit_except
Syntax: deny address | CIDR | unix: | all;
Default: —
Context: http, server, location, limit_except
語法中address表示地址,CIDR表示網(wǎng)段。unix:指定了特殊值,則允許訪問所有UNIX域套接字。all表示所有的。
實例:配置訪問控制
首先我們查看沒有限制的時候進行的輸出

編輯配置文件
location ~ ^/admin.html {
root /opt/app/code;
deny 122.233.229.350;
allow all;
index index.html index.htm;
}
其中 ~ 表示對請求路徑URL模式匹配,表示跟目下以admin.html開頭的家目錄設(shè)置在/opt/app/code。
然后檢查配置重啟
[root@hongshaorou conf.d]# nginx -tc /etc/nginx/nginx.conf
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@hongshaorou conf.d]# systemctl reload nginx
重新訪問我們的頁面

現(xiàn)在配置本機可以訪問
location ~ ^/admin.html {
root /opt/app/code;
allow 122.233.329.0/24;
deny all;
index index.html index.htm;
}
然后檢查配置重啟
[root@hongshaorou conf.d]# nginx -tc /etc/nginx/nginx.conf
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@hongshaorou conf.d]# systemctl reload nginx
重新訪問我們的頁面

http_access_module模塊的局限性:

nginx的訪問控制限制是針對客戶端的IP來進行限制的,但是nginx并不確定真正的客戶端是哪個,凡是和nginx進行交互的都被當做是客戶端。(remote_addr是直接和nginx通信的IP)如果我們訪問不是直接訪問到服務(wù)端而是由中間代理進行(如上圖),訪問控制這時就會失效。
局限性解決方法總結(jié):
方法一: 采用http頭信息控制訪問,如HTTP_X_FORWARD_FOR
方法二: 結(jié)合geo模塊
方法三: 通過HTTP自定義變量傳遞
http_x_forwarded_for頭信息控制訪問 會更好的解決該問題,它要求訪問時必須帶上所有用到的ip的地址信息
我們看一下http_x_forwarded_for記錄過程:
http_x_forwarded_for = Client IP, Proxy(1)IP, Proxy(2)IP,...

http_auth_basic_module模塊
配置語法
Syntax: auth_basic string | off;
Default: auth_basic off;
Context: http, server, location, limit_except
Syntax: auth_basic_user_file file;
Default: —
Context: http, server, location, limit_except
語法講解:
auth_basic 默認關(guān)閉,開啟的話輸入一段字符串即可。
auth_basic_user_file 該文件存儲用戶賬號密碼。
我們看一下官網(wǎng)的文件格式
# comment
name1:password1
name2:password2:comment
name3:password3
密碼加密方式有多中這里我們使用htpasswd
can be generated using the “htpasswd” utility from the Apache HTTP Server distribution or the “openssl passwd” command
想要使用該方式,首先要裝對應(yīng)的包
[root@hongshaorou conf.d]# yum -y install httpd-tools
接下來創(chuàng)建對應(yīng)的use_file文件
[root@hongshaorou conf.d]# cd ..
[root@hongshaorou nginx]# ls
conf.d koi-utf mime.types nginx.conf uwsgi_params
fastcgi_params koi-win modules scgi_params win-utf
[root@hongshaorou nginx]# htpasswd -c ./auth_conf hongshaorou
New password:
Re-type new password:
Adding password for user hongshaorou
[root@hongshaorou nginx]# cat auth_conf
hongshaorou:$apr1$kNj29q4w$zOu18mCbh06.nOTGNMe7h/
編輯配置文件
location ~ ^/admin.html {
root /opt/app/code;
auth_basic "Auth access test! input your password!";
auth_basic_user_file /etc/nginx/auth_conf;
index index.html index.htm;
}
進行語法檢查
[root@hongshaorou conf.d]# nginx -tc /etc/nginx/nginx.conf
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@hongshaorou conf.d]# systemctl reload nginx
現(xiàn)在我們再次訪問頁面的時候就需要輸入用戶名密碼

輸入剛才的賬號密碼就可以正常訪問了。
局限性:
一: 用戶信息依賴文件
二: 操作管理機械,效率低
解決方式:
一: nginx結(jié)合LUA實現(xiàn)高效驗證
二: nginx配合LDAP打通,利用nginx-auth-ldap模塊