nginx转发代理-失败(24:打开的文件太多), - php

我正在使用带有nginx的php curl作为代理。这是我的代码:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$curl_scraped_page = curl_exec($ch);
curl_close($ch);

echo $curl_scraped_page;

在某个时候运行此Nginx负载非常慢,并且有时它返回错误500。

日志说

失败(24:打开的文件太多),

一些更多的细节:

root@proxy-s2:~# ulimit -Hn
4096
root@proxy-s2:~# ulimit -Sn
1024

服务器上没有其他任何运行的内容,并且没有其他脚本在使用此代理。

是Nginx错误吗?怎么解决呢?

要么

还有什么呢?如何解决?

我没有更改默认的Nginx配置
Nginx重新启动解决了该问题(我暂时认为)

这是我的nginx.conf

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    gzip  on;

    server {
        listen       8080;

        location / {
            resolver 8.8.8.8;
            proxy_pass http://$http_host$uri$is_args$args;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

最佳

top - 09:23:55 up 21:51,  1 user,  load average: 0.09, 0.13, 0.08
KiB Mem:    496164 total,   444328 used,    51836 free,    12300 buffers
KiB Swap:        0 total,        0 used,        0 free.   336228 cached Mem

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
    8 root      20   0       0      0      0 S  0.0  0.0   4:57.56 rcuos/0
 4904 nobody    20   0   97796  14128   1012 R  0.0  2.8   4:19.82 nginx
    7 root      20   0       0      0      0 S  0.0  0.0   2:11.35 rcu_sched
    3 root      20   0       0      0      0 S  0.0  0.0   0:18.50 ksoftirqd/0
  832 root      20   0  139208   6808    172 S  0.0  1.4   0:13.11 nova-agent
   45 root      20   0       0      0      0 S  0.0  0.0   0:06.21 xenbus
   74 root      20   0       0      0      0 S  0.0  0.0   0:03.03 kworker/u30:1
  155 root      20   0       0      0      0 S  0.0  0.0   0:02.73 jbd2/xvda1-8
   46 root      20   0       0      0      0 R  0.0  0.0   0:02.39 kworker/0:1
   57 root      20   0       0      0      0 S  0.0  0.0   0:01.91 kswapd0
    1 root      20   0   33448   2404   1136 S  0.0  0.5   0:01.47 init
  391 root      20   0   18048   1336    996 S  0.0  0.3   0:00.97 xe-daemon
 1034 syslog    20   0  255840   2632    784 S  0.0  0.5   0:00.90 rsyslogd
 1107 root      20   0   61364   3048   2364 S  0.0  0.6   0:00.73 sshd
   40 root      rt   0       0      0      0 S  0.0  0.0   0:00.29 watchdog/0
  316 root      20   0   19472    456    252 S  0.0  0.1   0:00.12 upstart-udev-br
    6 root      20   0       0      0      0 S  0.0  0.0   0:00.11 kworker/u30:0
 1098 root      20   0   23652   1036    784 S  0.0  0.2   0:00.08 cron
 7935 root      20   0  105632   4272   3284 S  0.0  0.9   0:00.07 sshd
  330 root      20   0   51328   1348    696 S  0.0  0.3   0:00.06 systemd-udevd
 7953 root      20   0   22548   3428   1680 S  0.0  0.7   0:00.05 bash
  678 root      20   0   15256    524    268 S  0.0  0.1   0:00.04 upstart-socket-
 8647 root      20   0   25064   1532   1076 R  0.0  0.3   0:00.03 top

mpstat

root@proxy-s2:~# mpstat
Linux 3.13.0-55-generic (proxy-s2)      07/09/2015      _x86_64_        (1 CPU)

09:22:17 AM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
09:22:17 AM  all    0.94    0.00    1.63    0.16    0.00    2.16    0.92    0.00    0.00   94.20

iostat

root@proxy-s2:~# iostat
Linux 3.13.0-55-generic (proxy-s2)      07/09/2015      _x86_64_        (1 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.94    0.00    3.80    0.16    0.92   94.19

Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
xvdc              0.01         0.02         0.00       1710          0
xvda              3.16         4.19        88.56     322833    6815612

参考方案

请尝试以下操作,对您的limit.conf进行以下更改。

vi /etc/security/limits.conf

对于打开的文件

软无档案64000
硬nofile 64000

对于最大的用户进程
软nproc 47758
硬nproc 47758

对于最大内存大小
软RSS无限
硬RSS无限

对于虚拟内存
软如无限
硬如无限

PHP getallheaders替代 - php

我正在尝试从服务器上的apache切换到nginx。唯一的问题是我在PHP脚本中使用的getallheaders()函数,该函数不适用于Nginx。我已经尝试过用户在getallheaders函数上的php站点上提供的注释,但这并不返回所有请求标头。请告诉我如何解决这个问题。我真的想切换到Nginx。 参考方案 您仍然可以使用它,但是您必须像这里一样重新定义…

PHP:将字符串拆分为字母和数字部分的最佳方法 - php

我有几个格式的字符串AA11 AAAAAA1111111 AA1111111 分离字符串的字母和数字部分的最佳方法(最有效)? 参考方案 如果它们都是一系列字母,然后是一系列数字,并且没有非字母数字字符,那么sscanf()可能比regexp更有效$example = 'AAA11111'; list($alpha,$numeric) =…

php-casperjs获取内部文本 - php

我正在为casperjs使用php包装器-https://github.com/alwex/php-casperjs我正在网上自动化一些重复的工作,我需要访问一个项目的innerText,但是我尚不清楚如何从casperjs浏览器访问dom。我认为在js中我会var arr = document.querySelector('label.input…

PHP:检查目录是否为空的最佳和最简便的方法是什么 - php

Improve this question 我有一个包含数百个动态生成文件夹的根目录。随着时间的流逝,在这些目录必须为空的情况下,需要从系统中删除其中一些文件夹。实现这一目标的最佳的最短,最简单和/或最有效的方法是什么? 参考方案 使用glob:if (count(glob("path/*")) === 0 ) { // empty gl…

PHP:从函数返回值并直接回显它? - php

这可能是一个愚蠢的问题,但是……的PHPfunction get_info() { $something = "test"; return $something; } html<div class="test"><?php echo get_info(); ?></div> 有没有办…