学习了,
多谢春哥的及时回复啊,敬业~
On 10月12日, 上午5时57分, agentzh <agen...@gmail.com> wrote:
> Hello!
>
> 2012/10/11 linfeng zhang:
>
> > 1》现在在用test nginx自动化来写测试脚本,在一开始在perl脚本xxx.t配置的时候,test nginx 用"---
> > config" 来标示一个server,即测试时产生的nginx.conf里面会有一个server段,
> > 但是现在想通过xxx.t在nginx.conf里产生多个server段,请问怎么实现的?我试过在xxx.t里写多个"---
> > config"字段,但是发现nginx.conf里还是只会有一个server, 用最后的一个"--- config"做server字段?
>
> 你如果想定义自己的虚拟主机,可以使用 --- http_config 段:
>
> http://search.cpan.org/perldoc?Test::Nginx::Socket#http_config
>
> 比如这里有一个来自 ngx_lua 测试集的实例:
>
> === TEST 3: sanity
> --- http_config
> server {
> listen unix:$TEST_NGINX_HTML_DIR/nginx.sock;
> default_type 'text/plain';
>
> server_tokens off;
> location /foo {
> content_by_lua 'ngx.say("foo")';
> more_clear_headers Date;
> }
> }
> --- config
> location /test {
> rewrite_by_lua '
> local sock = ngx.socket.tcp()
> local ok, err = sock:connect("unix:$TEST_NGINX_HTML_DIR/nginx.sock")
> if not ok then
> ngx.say("failed to connect: ", err)
> return
> end
>
> ngx.say("connected: ", ok)
>
> local req = "GET /foo HTTP/1.0\\r\\nHost:
> localhost\\r\\nConnection: close\\r\\n\\r\\n"
> -- req = "OK"
>
> local bytes, err = sock:send(req)
> if not bytes then
> ngx.say("failed to send request: ", err)
> return
> end
>
> ngx.say("request sent: ", bytes)
>
> while true do
> print("calling receive")
> local line, err = sock:receive()
> if line then
> ngx.say("received: ", line)
>
> else
> ngx.say("failed to receive a line: ", err)
> break
> end
> end
>
> ok, err = sock:close()
> ngx.say("close: ", ok, " ", err)
> ';
>
> content_by_lua return;
> }
> --- request
> GET /test
> --- response_body
> connected: 1
> request sent: 57
> received: HTTP/1.1 200 OK
> received: Server: nginx
> received: Content-Type: text/plain
> received: Content-Length: 4
> received: Connection: close
> received:
> received: foo
> failed to receive a line: closed
> close: nil closed
>
>
>
> > 2》由于刚接触nginx,不熟悉,在请教个nginx的问题,想实现某个server响应客户端的请求超时如何实现的?
> > 我看到nginx里面有一个send_timeout 默认值是60s,
> > 那如果设置为0s,是会达到这个server响应超时吗?但是我实验感觉也能得到正常的结果啊?
>
> 直接和客户端相关的超时主要有下面几种配置:
>
> 1. client_header_timeout: 读取客户端请求头(request header)阶段的超时设置:
>
> http://wiki.nginx.org/HttpCoreModule#client_header_timeout
>
> 2. client_body_timeout: 读取客户端请求体(request body)阶段的超时设置:
>
> http://wiki.nginx.org/HttpCoreModule#client_body_timeout
>
> 3. send_timeout: 发送响应时的超时设置:
>
> http://wiki.nginx.org/HttpCoreModule#send_timeout
>
> 当把 client_read_timeout 置为 0 时,下面这个简单的接口在访问时就直接被断连接了:
>
> client_header_timeout 0;
> location /t {
> echo hello;
> }
>
> 而 send_timeout 想要在本地环境生效还是比较有难度的,因为多级写缓冲的存在,只要不是数据量很大同时网络上比较差,一般情况下是较难用小响应来触发"写不动"的情况的。当然,-我为此专门写过一个
> mockeagain 工具,可以在本机 lo 设备上轻松摸拟一次只能写出一个字节的极端情形:
>
> https://github.com/agentzh/mockeagain
>
> Best regards,
> -agentzh
>
> P.S. 建议未来把不同的问题拆分为不同主题的邮件,这样方便大家讨论 :) 多谢合作!