地址:https://github.com/dengqiang2015/ngx_http_captcha_modulenginx
此nginx模块可直接生成验证码和验证验证码,可在nginx配置中自定义验证码大小、长度、字体、过时时间等。此项目无需版权,可自由下载使用或二次开发。git
1. $ cp ngx_http_captcha_module.c /usr/local/src/nginx-1.14.0/src/http/modules/github
2. cd /usr/local/src/nginx-1.14.0redis
3. $ vim auto/modulesvim
添加代码字体
if :; then ngx_module_name=ngx_http_captcha_module ngx_module_incs= ngx_module_deps= ngx_module_srcs=src/http/modules/ngx_http_captcha_module.c ngx_module_libs= ngx_module_link=YES . auto/module fi
4. $ ./configure --with-debugspa
5. $ vim objs/Makefiledebug
找到code
objs/src/http/modules/ngx_http_captcha_module.o: $(CORE_DEPS) $(HTTP_DEPS) \ src/http/modules/ngx_http_captcha_module.c $(CC) -c -g $(CFLAGS) $(CORE_INCS) $(HTTP_INCS) \ -o objs/src/http/modules/ngx_http_captcha_module.o \ src/http/modules/ngx_http_captcha_module.c
改成图片
objs/src/http/modules/ngx_http_captcha_module.o: $(CORE_DEPS) $(HTTP_DEPS) \ src/http/modules/ngx_http_captcha_module.c $(CC) -c -g $(CFLAGS) $(CORE_INCS) $(HTTP_INCS) \ -I /usr/include -I /usr/local/include \ -L /usr/lib/ -L /usr/local/lib/ -lgd -lhiredis \ -o objs/src/http/modules/ngx_http_captcha_module.o \ src/http/modules/ngx_http_captcha_module.c
找到
-ldl -lrt -lpthread -lcrypt -lpcre -lz \
改成
-ldl -lrt -lpthread -lcrypt -lpcre -lz -lgd -lhiredis \
6. $ make && make install
location /captcha_img { captcha_redis_conf 127.0.0.1 6379; #redis配置,默认127.0.0.1 6379 captcha_init; #验证码初始化 captcha_width 130; #验证码宽度,默认130像素 captcha_height 30; #验证码高度,默认30像素 captcha_length 4; #验证码长度,默认4,最大长度6 captcha_font /data/font/elephant.ttf; #字体库 captcha_expire 3600; #过时时间, 默认3600秒 captcha_output; #输出图像 }
location /captcha_auth { captcha_redis_conf 127.0.0.1 6379; #redis配置,默认127.0.0.1 6379 captcha_auth; #验证 }
重启nginx
访问http://xxx/captcha_img显示验证码图片
访问http://xxx/captcha_auth?captcha_code=1234
captcha_code=1234为用户输入的验证码参数,用get请求方式