实际工作中碰到的一个小问题:
echo -e "GET /index.jsp HTTP/1.1\r\nHost:www.dianping.com\r\n"|nc 127.0.0.1 8080 (tomcat)
echo -e "GET /index.jsp HTTP/1.1\r\nHost:www.dianping.com\r\n"|nc -i 1 127.0.0.1 80 (nginx)
系统环境:
centos 6.3 Linux,64位、
nc-1.84-22.el6.x86_64
nginx
tomcat
用户请求------>负载均衡设备(F5,nginx集群)-------->应用服务器(nginx 80端口----->tomcat 8080端口)
问题:
echo -e "GET /index.jsp HTTP/1.1\r\nHost:www.dianping.com\r\n"|nc 127.0.0.1 8080 (tomcat)
echo -e "GET /index.jsp HTTP/1.1\r\nHost:www.dianping.com\r\n"|nc 1 127.0.0.1 80 (nginx)
nc通过域名以及ip:80端口访问相应页面获取不到页面内容,但是通过ip:8080端口可以正常获取到页面。
解决问题思路:
先在nginx的根目录下放一个静态文件,通过nc 连接ip:80端口访问,可以正常获取到页面数据
echo -e "GET /index.html HTTP/1.1\r\nHost:www.dianping.com\r\n"|nc 127.0.0.1 80 (nginx)
在nginx上配置一个8081的server,目录指向另外一个目录,创建index8081.html文件,此时通过nc连接ip:8081端口可以访问到此页面,通过ip:80端口是无法正常访问的。
在nginx上配置,80端口访问不到的文件反向代理到8081端口,一个是upstream的配置,一个是proxy_pass的配置。
此时通过ip:8081使用nc连接后可以读取到相应页面内容,通过ip:80端口连接后只是显示连接80端口成功。
问题出现在 80到8081 后端 proxy_pass 过程中,nc 连接时间过短,过早退出。
这个可以通过交互方式连接nginx 80端口可以看出,那有没有办法让nc暂停一下,等待数据的返回。
在man nc的输出中,可以看到
-i interval
Specifies a delay time interval between lines of text sent and received. Also causes a delay time between connections to multiple ports.
echo -e "GET /index.html HTTP/1.1\r\nHost:www.dianping.com\r\n"|nc -i 1 127.0.0.1 80
可以正常获取到数据了。
同样在正式的工作环境中,使用以下命令通过nginx就可以获取到tomcat中的内容了:
echo -e "GET /index.jsp HTTP/1.1\r\nHost:www.dianping.com\r\n"|nc -i 1 127.0.0.1 80
转载于:https://blog.51cto.com/liuqunying/1401036