Nginx的訪問控制

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表示所有的。

實例:配置訪問控制
首先我們查看沒有限制的時候進行的輸出


圖片.png

編輯配置文件

    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

重新訪問我們的頁面


圖片.png

現(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

重新訪問我們的頁面


圖片.png

http_access_module模塊的局限性:


圖片.png

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,...
圖片.png
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)在我們再次訪問頁面的時候就需要輸入用戶名密碼


圖片.png

輸入剛才的賬號密碼就可以正常訪問了。

局限性:

一: 用戶信息依賴文件
二: 操作管理機械,效率低
解決方式:

一: nginx結(jié)合LUA實現(xiàn)高效驗證
二: nginx配合LDAP打通,利用nginx-auth-ldap模塊

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,555評論 19 139
  • 配置運行Nginx服務(wù)器用戶(組) 用于配置運行Nginx服務(wù)器用戶(組)的指令是user,其語法格式為: use...
    吃瓜的東閱讀 4,685評論 0 41
  • Page 1:nginx 服務(wù)器安裝及配置文件詳解 CentOS 6.2 x86_64 安裝 nginx 1.1 ...
    xiaojianxu閱讀 8,680評論 1 41
  • 1.簡介: ? Nginx:engine X ,2002年,開源,商業(yè)版? http協(xié)議:web服務(wù)器(類似于ht...
    尛尛大尹閱讀 2,006評論 0 3
  • 昨天學(xué)習了u旅行課程,這是第二次學(xué)習,感覺之前不懂的內(nèi)容,經(jīng)過二次鞏固明白了不少,重要的還是要實踐! 看到學(xué)習的伙...
    melody靜閱讀 146評論 0 0

友情鏈接更多精彩內(nèi)容