处理从NGINX到Unicorn(AngularJS to Rails API)的HTTP请求时,只能在Chrome中发生大的延迟

我有一个AngularJS前端发送请求到Rails API后端应用程序。 AngularJS前端运行在NGINX服务器上。 Rails API在Unicorn服务器上运行。

一般来说,他们在一起工作很好,但在一个案例中。 当我第一次加载主页大部分时间(但不是总是),有一个很大的延迟(大约12秒处理请求到Rails)。 我不确定,但我的猜测是,这只是从Nginx到Unicorn的第一个请求发生。

这对我来说是一个大问题,因为当用户第一次请求主页时,页面被加载并显示得相当快(显示所有组件需要大约2秒),但是用户必须等待这12秒,直到combobox加载了来自后端的数据。

我不知道服务器configuration。 我只是一个开发者。 我试图debugging请求,并试图找出瓶颈的地方,但我什么也没find。

请find我的NGINX access.log日志:

127.0.0.1 - - [12/Nov/2014:19:33:45 +0100] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36" 127.0.0.1 - - [12/Nov/2014:19:33:45 +0100] "GET /css/bootstrap.min.css HTTP/1.1" 304 0 "http://localhost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36" 127.0.0.1 - - [12/Nov/2014:19:33:45 +0100] "GET /css/bootstrap-responsive.min.css HTTP/1.1" 304 0 "http://localhost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36" 127.0.0.1 - - [12/Nov/2014:19:33:45 +0100] "GET /dist/css/yanpy.min.css HTTP/1.1" 304 0 "http://localhost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36" 127.0.0.1 - - [12/Nov/2014:19:33:45 +0100] "GET /css/icons.css HTTP/1.1" 304 0 "http://localhost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36" 127.0.0.1 - - [12/Nov/2014:19:33:45 +0100] "GET /css/menus.css HTTP/1.1" 304 0 "http://localhost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36" 127.0.0.1 - - [12/Nov/2014:19:33:45 +0100] "GET /css/join-us.css HTTP/1.1" 304 0 "http://localhost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36" 127.0.0.1 - - [12/Nov/2014:19:33:45 +0100] "GET /css/font-awesome.min.css HTTP/1.1" 304 0 "http://localhost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36" 127.0.0.1 - - [12/Nov/2014:19:33:45 +0100] "GET /css/font-awesome-ie7.min.css HTTP/1.1" 304 0 "http://localhost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36" 127.0.0.1 - - [12/Nov/2014:19:33:45 +0100] "GET /dist/css/yanpy-ext-1.min.css HTTP/1.1" 304 0 "http://localhost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36" 127.0.0.1 - - [12/Nov/2014:19:33:45 +0100] "GET /css/lightbox.css HTTP/1.1" 304 0 "http://localhost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36" 127.0.0.1 - - [12/Nov/2014:19:33:45 +0100] "GET /dist/js/yanpy-libs-1.min.js HTTP/1.1" 304 0 "http://localhost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36" 127.0.0.1 - - [12/Nov/2014:19:33:45 +0100] "GET /js/app.js HTTP/1.1" 304 0 "http://localhost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36" 127.0.0.1 - - [12/Nov/2014:19:33:45 +0100] "GET /js/services.js HTTP/1.1" 304 0 "http://localhost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36" 127.0.0.1 - - [12/Nov/2014:19:33:45 +0100] "GET /js/controllers.js HTTP/1.1" 304 0 "http://localhost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36" 127.0.0.1 - - [12/Nov/2014:19:33:45 +0100] "GET /dist/js/yanpy.min.js HTTP/1.1" 304 0 "http://localhost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36" 127.0.0.1 - - [12/Nov/2014:19:33:45 +0100] "GET /js/filters.js HTTP/1.1" 304 0 "http://localhost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36" 127.0.0.1 - - [12/Nov/2014:19:33:45 +0100] "GET /js/directives.js HTTP/1.1" 304 0 "http://localhost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36" 127.0.0.1 - - [12/Nov/2014:19:33:45 +0100] "GET /js/i18n.js HTTP/1.1" 304 0 "http://localhost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36" 127.0.0.1 - - [12/Nov/2014:19:33:45 +0100] "GET /js/properties/properties.js HTTP/1.1" 304 0 "http://localhost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36" 127.0.0.1 - - [12/Nov/2014:19:33:45 +0100] "GET /dist/js/yanpy-libs-2.min.js HTTP/1.1" 304 0 "http://localhost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36" 127.0.0.1 - - [12/Nov/2014:19:33:45 +0100] "GET /img/logo.png HTTP/1.1" 304 0 "http://localhost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36" 127.0.0.1 - - [12/Nov/2014:19:33:46 +0100] "GET /languages/locale-en.json HTTP/1.1" 304 0 "http://localhost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36" 127.0.0.1 - - [12/Nov/2014:19:33:46 +0100] "GET /lib/angular/i18n/angular-locale_en.js HTTP/1.1" 304 0 "http://localhost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36" 127.0.0.1 - - [12/Nov/2014:19:33:46 +0100] "GET /languages/locale-en.json HTTP/1.1" 304 0 "http://localhost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36" 127.0.0.1 - - [12/Nov/2014:19:33:46 +0100] "GET /partials/login.html HTTP/1.1" 304 0 "http://localhost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36" 127.0.0.1 - - [12/Nov/2014:19:33:46 +0100] "GET /img/glyphicons-halflings.png HTTP/1.1" 304 0 "http://localhost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36" 127.0.0.1 - - [12/Nov/2014:19:33:46 +0100] "GET /partials/home.html HTTP/1.1" 304 0 "http://localhost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36" 127.0.0.1 - - [12/Nov/2014:19:33:46 +0100] "GET /img/facilImage.png HTTP/1.1" 304 0 "http://localhost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36" 127.0.0.1 - - [12/Nov/2014:19:33:46 +0100] "GET /img/rapidoImage.png HTTP/1.1" 304 0 "http://localhost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36" 127.0.0.1 - - [12/Nov/2014:19:33:46 +0100] "GET /img/seguroImage.png HTTP/1.1" 304 0 "http://localhost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36" 127.0.0.1 - - [12/Nov/2014:19:33:46 +0100] "GET /img/carrusel1.jpg HTTP/1.1" 304 0 "http://localhost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36" 127.0.0.1 - - [12/Nov/2014:19:33:46 +0100] "GET /img/carrusel2.jpg HTTP/1.1" 304 0 "http://localhost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36" 127.0.0.1 - - [12/Nov/2014:19:33:46 +0100] "GET /img/carrusel3.jpg HTTP/1.1" 304 0 "http://localhost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36" 127.0.0.1 - - [12/Nov/2014:19:33:46 +0100] "GET /img/carrusel4.jpg HTTP/1.1" 304 0 "http://localhost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36" 127.0.0.1 - - [12/Nov/2014:19:33:46 +0100] "GET /partials/boat-searcher.html HTTP/1.1" 304 0 "http://localhost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36" 127.0.0.1 - - [12/Nov/2014:19:33:47 +0100] "GET /img/glyphicons-halflings-white.png HTTP/1.1" 304 0 "http://localhost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36" 

正如你所看到的,大约需要2秒。 这一点我不知道为什么我不能看到在这个文件中的Rails API的请求(无论如何,你可以在我附加的快照中看到它们)。

在这个日志之后,我只需等待12秒就可以得到rails api请求:

 Started GET "/getTranslationLanguages" for 127.0.0.1 at 2014-11-12 19:33:59 +0100 Processing by LanguagesController#translationLanguages as HTML Language Load (0.4ms) SELECT `languages`.* FROM `languages` WHERE `languages`.`translation` = 1 Completed 200 OK in 2ms (Views: 1.0ms | ActiveRecord: 0.4ms) Started GET "/currencies" for 127.0.0.1 at 2014-11-12 19:33:59 +0100 Processing by CurrenciesController#index as HTML Currency Load (0.3ms) SELECT `currencies`.* FROM `currencies` Completed 200 OK in 3ms (Views: 1.8ms | ActiveRecord: 0.3ms) Started GET "/boat_people" for 127.0.0.1 at 2014-11-12 19:33:59 +0100 Processing by BoatPeopleController#index as HTML BoatPerson Load (0.3ms) SELECT `boat_people`.* FROM `boat_people` Completed 200 OK in 2ms (Views: 0.9ms | ActiveRecord: 0.3ms) Started GET "/getTranslationLanguages" for 127.0.0.1 at 2014-11-12 19:33:59 +0100 Processing by LanguagesController#translationLanguages as HTML Language Load (0.4ms) SELECT `languages`.* FROM `languages` WHERE `languages`.`translation` = 1 Completed 200 OK in 2ms (Views: 1.0ms | ActiveRecord: 0.4ms) Started GET "/currencies" for 127.0.0.1 at 2014-11-12 19:33:59 +0100 Processing by CurrenciesController#index as HTML Currency Load (0.3ms) SELECT `currencies`.* FROM `currencies` Completed 200 OK in 2ms (Views: 1.1ms | ActiveRecord: 0.3ms) 

看,从Nginx的最后一个日志条目到Unicorn的第一个日志条目有12秒的延迟。

这一次我不知道发生了什么。

我也附上Google工具的快照。 它显示或多或less与Nginx access.log相同的信息。 但是这一次,您可以看到Rails API(即getTranslationLanguages)的请求延迟为12.40秒。

Unicorn服务器请求延迟

为什么要等12秒? 可能是任何caching? 我还能检查什么?

更新 :我不知道为什么它似乎与caching或cookie有关。 当我在浏览器中使用caching和Cookie删除浏览历史logging,并加载网站时,问题不再发生。 之后,如果我只刷新页面,问题就会发生。

重要更新现在,我很确定这个问题位于独angular兽。 如果我只是closuresUnicorn并启动webrick(没有任何其他更新在我的应用程序或代码)的问题不会再发生。

我附上我的Unicorn.rbconfiguration文件:

 # config/unicorn.rb if ENV["RAILS_ENV"] == "development" worker_processes 1 stderr_path "/Users/Rober/Projects/yanpy/dev/yanpyapi/log/unicorn.stderr.log" stdout_path "/Users/Rober/Projects/yanpy/dev/yanpyapi/log/unicorn.stdout.log" elsif ENV["RAILS_ENV"] == "test" worker_processes 1 stderr_path "/home/ubuntu/env/test/www/yanpyapi-test/log/unicorn.stderr.log" stdout_path "/home/ubuntu/env/test/www/yanpyapi-test/log/unicorn.stdout.log" listen 8081 else #worker_processes 3 worker_processes 1 stderr_path "/home/ubuntu/env/production/www/yanpyapi/log/unicorn.stderr.log" stdout_path "/home/ubuntu/env/production/www/yanpyapi/log/unicorn.stdout.log" end timeout 30 

注意:你不必担心环境,这两个问题都在发生。

更新:当我在我的浏览器中直接发送请求到Unicorn的Rails端点,即http://localhost:8080/getTranslationLanguages我在浏览器中以json格式立即得到结果。 我的意思是,这个问题不会发生。 所以,看起来问题是只有当请求从Nginx发送到Unicorn。

重要更新我已经确认这个问题只发生在谷歌浏览器。 Safari永远不会发生这种情况。 甚至,我注意到,当我在Google工具中运行审计工具时,它会以某种方式清理所有内容,所以每次运行审计工作时都能正常工作。

更新:我已经安装和彪马testing。 它正在工作。 我不知道为什么Unicorn和Chrome的组合不起作用。