There is a module in the nginx that has this function, (the previous article expands, here is called the module, after the unification is called the module, the module may be accurate point, because the extension is the individual file, but the module is embeds in the main file), this module is called Drizzle-nginx-module, the download address is:
Https://github.com/openresty/drizzle-nginx-module
To compile this is not easy, the above address also has a description:
Download drizzle library First, address: http://openresty.org/download/drizzle7-2011.07.21.tar.gz
Compilation and configuration options:
Copy Code code as follows:
./configure--without-server
Make libdrizzle-1.0
Make install-libdrizzle-1.0
Next is the output, HTTP directly output JSON string, how to do, but also the next module, there are too many modules,
Https://github.com/openresty/rds-json-nginx-module
The final configuration options are as follows:
Copy Code code as follows:
./configure--PREFIX=/OPT/NGINXRW--with-http_gzip_static_module--with-http_stub_status_module--with-http_ssl_ Module--add-module=/data/src/lua-nginx-module-0.9.8--add-module=/data/src/ngx_devel_kit-0.2.19--add-module=/ data/src/drizzle-nginx-module-0.1.7--add-module=/data/src/rds-json-nginx-module-master
Finally start Nginx, there may be reported drizzle shared library can not find the profile:/etc/ld.so.conf, after changing the configuration, remember to use the Lddconfig re-entry
How to use it
First, declare a MySQL server in the HTTP module of Nginx
Copy Code code as follows:
Upstream Backend {
Drizzle_server 172.21.107.247:3306 Dbname=oneplus_user_sso password= user=root protocol=mysql;
}
Next is the declaration location handled the
Copy Code code as follows:
Location/mysql {
Set $my _sql ' select * from T_user limit 1 ';
Drizzle_query $my _sql;
Drizzle_pass backend;
Drizzle_connect_timeout 500ms; # Default 60s
Drizzle_send_query_timeout 2s; # Default 60s
Drizzle_recv_cols_timeout 1s; # Default 60s
Drizzle_recv_rows_timeout 1s; # Default 60s
Rds_json on;
}
Where Rds_json on indicates the output is in JSON format
Next, visit address: Http://172.21.107.174/mysql,
You can output a user's information, and the format is JSON