OpenResty上各种测试用例实操(1)
生活随笔
收集整理的這篇文章主要介紹了
OpenResty上各种测试用例实操(1)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
下面記錄了我閱讀《OpenResty最佳實踐》過程中對一些例子的實踐操作示例,記錄以作備忘。
一.Location的組合使用
演示一個location只作為內部調用接口,被另一個location調用
reload配置文件后,在終端輸入
更復雜一些的情況:
有多個子請求來并行或串行調用,它們的執行時間的比較,下面是改造后的nginx.conf
worker_processes 1;
error_log logs/error.log;
events {worker_connections 1024;
}http {server{listen 6699;location = /sum {internal;content_by_lua_block{ngx.sleep(0.1)local args = ngx.req.get_uri_args()ngx.say(tonumber(args.a) + tonumber(args.b))}}location = /subduction {internal;content_by_lua_block{ngx.sleep(0.1)local args = ngx.req.get_uri_args()ngx.say(tonumber(args.a) - tonumber(args.b))}}location = /app/test_parallels {content_by_lua_block{local start_time = ngx.now()local res1, res2 = ngx.location.capture_multi({{"/sum", {args={a=3, b=8}}},{"/subduction", {args={a=3, b=8}}}})ngx.say("status:", res1.status, " response:", res1.body)ngx.say("status:", res2.status, " response:", res2.body)ngx.say("time used:", ngx.now() - start_time)}}location = /app/test_queue {content_by_lua_block{local start_time = ngx.now()local res1 = ngx.location.capture_multi({{"/sum", {args={a=3, b=8}}}})local res2 = ngx.location.capture_multi({{"/subduction", {args={a=3, b=8}}}})ngx.say("status:", res1.status, " response:", res1.body)ngx.say("status:", res2.status, " response:", res2.body)ngx.say("time used:", ngx.now() - start_time)}}location / {default_type text/html;content_by_lua_block {ngx.say("Hello World!")}}}
}
檢查nginx.conf語法正確后,熱加載nginx
在命令行輸入如下命令進行對比測試,發現當兩個子請求沒有依賴關系時,并行比串行效率高一倍
另一個場景是,外部重定向,可以跨域名的。例如從 A 網站跳轉到 B 網站是絕對允許的。在 CDN 場景的大量下載應用中,一般分為調度、存儲兩個重要環節。調度就是通過根據請求方 IP 、下載文件等信息尋找最近、最快節點,應答跳轉給請求方完成下載。
下面是配置文件內容
語法檢查無誤后,重新加載生效
在命令行輸入如下兩個命令測試
參考文獻
[1].https://moonbingbing.gitbooks.io/openresty-best-practices/content/openresty/work_with_location.html
總結
以上是生活随笔為你收集整理的OpenResty上各种测试用例实操(1)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在CentOS 6.6上搭建OpenRe
- 下一篇: 在Ubuntu 14.04 64bit上