fi
在编译的时候,添加上--add-dynamic-module选项,将模块添加进来。笔者这里使用的是--add-dynamic-module=/root/nginx-1.12.0/nginx_auth_mysql,其中的nginx_auth_mysql目录用于存放模块的源代码。
在进行编译的过程中,笔者遇到了如下错误:
/root/nginx-1.12.0/nginx_auth_mysql/ngx_http_auth_mysql_module.c: In function ‘ngx_http_auth_mysql_check_md5’:
/root/nginx-1.12.0/nginx_auth_mysql/ngx_http_auth_mysql_module.c:488:19: error: ‘MD5_DIGEST_LENGTH’ undeclared (first use in this function)
u_char md5_str[2*MD5_DIGEST_LENGTH + 1];
^
/root/nginx-1.12.0/nginx_auth_mysql/ngx_http_auth_mysql_module.c:488:19: note: each undeclared identifier is reported only once for each function it appears in
auth_mysql_host: the host of the MySQL server. Default is 127.0.0.1.
auth_mysql_port: on which port to connect to the MySQL server. Default is 3306.
auth_mysql_user: username for connection to the MySQL server. Default is root.
auth_mysql_password: password for connection to the MySQL server. Default is empty.
auth_mysql_database: name of the database. Required.
auth_mysql_table: name of the table, which holds the user record.
You can have more than one table separated by comas. Default is users.
auth_mysql_user_column: name of the username column. Default is username.
auth_mysql_password_column: name of the password column. Default is password.
auth_mysql_conditions: Additional SQL conditions. They will be placed after and AND.
Default is empty string.
auth_mysql_group_table: name of the table, which holds the groups information.
You can have more than one table separated by comas. Default is the users table.
auth_mysql_group_column: name of the group name column. Default is name.
auth_mysql_group_conditions: Additional SQL conditions applied only in group queries.
They will be placed after an AND. Default is empty string.
auth_mysql_encryption_type: the format of the password field. Should be one of:
none: the password is stored in plaintext in the database;
md5: in the database is stored a md5 hash of the password;
phpass: a portable php hash of the password is stored. See:
http://www.openwall.com/phpass/ for more information.
The default is md5.
auth_mysql_allowed_users: whitespace delimited list of allowed users.
auth_mysql_allowed_groups: whitespace delimited list of allowed groups.
If both allowed_users and allowed_groups are defined, either of them has to satisfied.
笔者这里使用mysql数据库创建认证用户的内容如下所示,创建nginx数据库,在nginx数据库里面添加一个nginx_auth的数据表,存放user字段和password字段,并且password字段用md5进行加密:
$ mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3337
Server version: 5.5.44-MariaDB MariaDB Server
Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> use nginx;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [nginx]> show tables;
+-----------------+
| Tables_in_nginx |
+-----------------+
| nginx_auth |
+-----------------+
1 row in set (0.00 sec)
MariaDB [nginx]> select * from nginx_auth;
+------+----------------------------------+
| user | password |
+------+----------------------------------+
| tom | d077f244ddf8r70e5ea758bd8352fcd8 |
+------+----------------------------------+
1 row in set (0.00 sec)
在nginx.conf配置文件当中使用的配置如下所示:
......
......
== WRITING A NEW ECNRYPTION TYPE ==
Add an entry in the ngx_http_auth_mysql_enctypes array. It has to be a struct
with two elements:
ngx_str_t> The name under which it should be referenced in the config file
ngx_uint_t (*checker)(ngx_http_request_t *r, ngx_str_t sent_password, ngx_str_t actual_password)
A function, which given the request (mostly used for logging and memory allocation through its r->pool),
the password sent by the user and the password in the database has to determine whether they match.
If they match it should return NGX_OK, if they don’t it should return NGX_DECLINED. If other error
occures, it should log it and return NGX_ERR.
Currently salts aren't supported, but if there are schemes, which require them it is quite easy.
Questions/patches may be sent to Nikolay Bachiyski, nikolay@automattic.com