OSX 上的 NGINX - 运行速度非常慢

OSX 上的 NGINX - 运行速度非常慢

我一直在尝试在我的 OSX 10.7 Lion 计算机上启动并运行 NGINX。我让它运行,但是每隔几个简单的 html 页面就需要很长时间才能加载;例如:

<html> 
  <body>
    test
  </body> 
</html>

php 也会发生同样的事情:

<?php
  echo('hi');
?>

如果我点击刷新,它似乎在渲染之前几乎重建了整个页面并创建了某种对象。速度太慢了……

如果我进行一些编码然后返回刷新页面,这种情况似乎最常发生。(10-20 秒以上,然后返回并刷新页面需要 4-6 秒)..几乎看起来一旦空闲就需要一段时间才能重新唤醒。

我绞尽脑汁想了解到底发生了什么,希望有人能为我解释一下。

系统配置:

  • 操作系统:OSX 10.7.2

  • 处理器:2 x 2.66 GHZ 双核 Intel Xeon

  • 内存:8GB 667MHz

Nginx 版本:1.0.11

PHP 版本:5.3.9

我已经从 OSX 的干净格式安装了它(我最初以为这是我的错误,但遗憾的是事实并非如此)。

更新

根据 Fox 在评论中的建议,更新我的 error_log 文件以包含调试后,我现在看到以下消息出现在我的 error_log 中:

2012/01/23 11:57:02 [info] 88015#0: *26 client closed prematurely connection 
while reading client request line, client: 127.0.0.1, server: sandbox.local

更新二

使用 chrome 检查后,我发现 DNS 解析似乎需要一点时间? 在此处输入图片描述

更新三-已解决

更新两个修复后的 /etc/hosts 文件供使用:

127.0.0.1 沙箱.本地

::1 沙箱.本地

感谢@thinice,我能够通过 strace 并注意到所有直接从 telnet 定位到 localhost 的请求总是即时的;然后提示 DNS 检查并最终找到这个!

OSX /etc/hosts 错误

我不确定这是否是 nginx 错误;因为当我之前安装 appache 时它运行正常。

============

这是我的配置文件:

NGINX 配置

user petrogad staff;
worker_processes  2;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

pid        /usr/local/ngnix/var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include mime.types;

    default_type text/plain;
    server_tokens off;

    sendfile on;
    tcp_nopush on;
    keepalive_timeout 10;

   # gzip on;
   # gzip_comp_level 2;
   # gzip_proxied any;
   # gzip_types text/plain text/css text/javascript application/json application/x-javascript text/xml application/xml application/xml+rss;

    index index.html index.php;
    include /usr/local/ngnix/conf/sites-enabled/*.link;
}

服务器配置

{
    listen 80;
    server_name sandbox.local;
    root /www/sandbox;

    access_log /Users/petrogad/logs/ngnix-sites/sandbox_log.txt;
    error_log /Users/petrogad/logs/ngnix-sites/sandbox_log.txt;

    location /
    {
        # First attempt to serve request as file, then
        # as directory, then fall back to index.html
        try_files $uri $uri/ /index.html;
        autoindex on;
    }

    include /usr/local/ngnix/conf/php.conf;
}

PHP 包含

fastcgi_intercept_errors on;

location ~ \.php$
{
    #fastcgi_intercept_errors on;
    fastcgi_param PATH_INFO         $fastcgi_path_info;
    fastcgi_param PATH_TRANSLATED   $document_root$fastcgi_path_info;
    fastcgi_param QUERY_STRING      $query_string;
    fastcgi_param REQUEST_METHOD    $request_method;
    fastcgi_param CONTENT_TYPE      $content_type;
    fastcgi_param CONTENT_LENGTH    $content_length;
    fastcgi_param SCRIPT_NAME       $fastcgi_script_name;
    fastcgi_param SCRIPT_FILENAME   $request_filename;
    fastcgi_param REQUEST_URI       $request_uri;
    fastcgi_param DOCUMENT_URI      $document_uri;
    fastcgi_param DOCUMENT_ROOT     $document_root;
    fastcgi_param SERVER_PROTOCOL   $server_protocol;
    fastcgi_param GATEWAY_INTERFACE CGI/1.1;
    fastcgi_param SERVER_SOFTWARE   nginx;
    fastcgi_param REMOTE_ADDR       $remote_addr;
    fastcgi_param REMOTE_PORT       $remote_port;
    fastcgi_param SERVER_ADDR       $server_addr;
    fastcgi_param SERVER_PORT       $server_port;
    fastcgi_param SERVER_NAME       $server_name;

    fastcgi_read_timeout 60; # Set fairly high for debugging

    fastcgi_pass  127.0.0.1:9001; # Non-default port
    fastcgi_index index.php;
}

快速 CGI 配置

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

PHP-FPM 配置

[global]
pid = /usr/local/php-5.3.9/var/run/php-fpm.pid
daemonize = yes

[www]
listen = 127.0.0.1:9001
user = petrogad
group = staff
pm = dynamic
pm.max_children = 10
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 10
pm.max_requests = 500

尝试使用以下 nginx 配置的简单解决方案,结果与上面的一样缓慢:

user petrogad staff;
worker_processes  2;


pid        /usr/local/ngnix/var/run/nginx.pid;

events {
    worker_connections  1024;
}


http {

    include mime.types;

    default_type text/plain;
    server_tokens off;

    sendfile on;
    tcp_nopush off;
    keepalive_timeout 0;

    index index.html;

  server
  {
    listen 80;
    server_name sandbox.local;
    root /www/sandbox;

    access_log /Users/petrogad/logs/ngnix-sites/sandbox_log.txt;
    error_log /Users/petrogad/logs/ngnix-sites/sandbox_log.txt;

    location /
    {
        autoindex on;
    }

  }



}

答案1

首先删除所有不必要的配置选项。

将设置设置为最基本的“我仅提供 HTML 文件”默认配置。删除优化。

慢慢地开始一次添加几个方面,重新启动堆栈并进行测试。

如果您雄心勃勃,您可以strace在单个服务器进程上运行来获取时间。看看这个速成课程

相关内容