LDAP應用篇(3)Nginx接入

雨帝夜泪發表於2024-07-17

實驗環境:Oracle Linux R8

在搞Nginx比較錯愕的是,居然 Nginx 並未內建對LDAP的支援,需要單獨編譯。然而 yum 安裝的 nginx 並不支援匯入模組,不得不再次溫習一下編譯安裝 Nginx 的路數了。

下載元件

github 中下載元件的原始碼:

cd ~ && git clone https://github.com/kvspb/nginx-auth-ldap.git

部署Nginx

環境準備

yum install openssl openssl-devel pcre-devel make autoconf gcc gcc-c++ -y

編譯

./configure  --user=nginx --group=nginx --prefix=/etc/nginx \
--error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log \
--with-http_ssl_module --with-http_v2_module --with-http_addition_module \
--with-http_gzip_static_module --with-http_random_index_module \
--add-module=/root/soft/nginx-auth-ldap

要注意的是,這裡的編譯,是編譯nginx而不是元件本身。編譯後的返回路徑提示為:

nginx path prefix: "/etc/nginx"
nginx binary file: "/etc/nginx/sbin/nginx"
nginx modules path: "/etc/nginx/modules"
nginx configuration prefix: "/etc/nginx/conf"
nginx configuration file: "/etc/nginx/conf/nginx.conf"
nginx pid file: "/etc/nginx/logs/nginx.pid"
nginx error log file: "/var/log/nginx/error.log"
nginx http access log file: "/var/log/nginx/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"

配置

Nginx 的主配置檔案的 http 指令中,新增下面的配置段:

ldap_server test1 {
  url ldaps://server.example.com:636/dc=example,dc=com?uid?sub?(objectClass=*);
  binddn "cn=admin,dc=example,dc=com";
  binddn_passwd "123456";
   memberuid;
  group_attribute_is_dn on;
  require valid_user;
}

上面這段配置沒有太鬧明白其含義,比如 group_attribute 的值元件的文件中用的是 uniquemember,而一般能搜到的文章都是用的 memberuid ,根據對屬性的反覆對比,這應該就是預設的過濾配置,及具備了該組屬性的使用者方可登入。當然這也只是猜測,沒有看到明確的說明出處!

根據常規對 Nginx 的管理,至少可以判斷出上面的塊配置,相當於定義了一個指令,因此可以在後面的相應位置,比如 server 或者 location 指令中進行配置,如果在同一個 server 指令範圍內都進行了引用,則後者會覆蓋前者的配置:

server {
    auth_ldap "Forbidden";
    auth_ldap_servers test1;
}

參考資料

  • nginx-auth-ldap

相關文章