1,Nginx的echo模块介绍
echo模块是国人编写的nginx的第三方模块,下载官方nginx后需要再下载echo模块,并且配合nginx编译安装,安装此模块后可以在nginx的url访问中可以通过echo命令输出字符到用户的浏览器中,可用于检测nginx的可访问性,检测nginx的配置的正确性等,总之在调试配置nginx环节,echo命令非常有用。
2,下载echo模块源码包:
# cd /usr/local/src/ # wget https://codeload.github.com/openresty/echo-nginx-module/tar.gz/v0.58 # tar xvzf v0.58 # ls echo-nginx-module-0.58
3,查看Nginx现有编译的参数:
# /opt/nginx/sbin/nginx -V nginx version: nginx/1.8.1 built by gcc 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC) built with OpenSSL 1.0.1c 10 May 2012 TLS SNI support enabled configure arguments: --prefix=/opt/nginx --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.37 --with-zlib=/usr/local/src/zlib-1.2.8 --with-openssl=/usr/local/src/openssl-1.0.1c --add-module=/usr/local/src/echo-nginx-module-0.58
4,从新编译nginx,添加echo模块:
# ./configure --prefix=/opt/nginx --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.37 --with-zlib=/usr/local/src/zlib-1.2.8 --with-openssl=/usr/local/src/openssl-1.0.1c --add-module=/usr/local/src/echo-nginx-module-0.58 #make
注:千万不要make install,否则就会覆盖安装nginx了。
5,拷贝可执行文件nginx覆盖现在的nginx可执行文件
# /etc/init.d/nginx stop # cp objs/nginx /opt/nginx/sbin/ # /etc/init.d/nginx start
6,验证:
# cat nginx.conf #user nobody; worker_processes 1; events { worker_connections 1024; } http { include mime.types; #include conf.d/vhost.conf; default_type application/octet-stream; #log_format main ‘$remote_addr - $remote_user [$time_local] "$request" ‘ # ‘$status $body_bytes_sent "$http_referer" ‘ # ‘"$http_user_agent" "$http_x_forwarded_for"‘; #access_log logs/access.log main; sendfile on; tcp_nopush on; keepalive_timeout 65; gzip on; server { listen 80; server_name 127.0.0.1; location / { echo "123"; } access_log logs/tomcat1_access.log; } }
访问验证:
# curl 127.0.0.1 123
7,注意:
1.echo命令只能放在url请求中,如果放在url请求外,会报错 如果报[emerg]: "echo" directive is not allowed here in ,请检查echo放置的位置
2.一次url请求,echo 只能打印一行,如果有逻辑判断,且判断成功,则echo会执行判断成功里边的echo,否则执行最后一句echo(此处不一定正确,在测试中发现是此现象)
3.如果echo后边有配置return 或者配置 proxy_pass,则echo的输出会被覆盖,即浏览器无法看到echo的内容
4.echo的内容不是写在nginx的配置文件中,而是输出到浏览器中,所以echo的打印字符的查看请在浏览器中查看
本文出自 “baiyubao的博客” 博客,请务必保留此出处http://baiyubao.blog.51cto.com/2845008/1758500
原文地址:http://baiyubao.blog.51cto.com/2845008/1758500