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

Php nginx转发代理-失败(24:打开的文件太多),,php,curl,nginx,proxy,Php,Curl,Nginx,Proxy,我正在使用带有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

我正在使用带有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 bug吗?如何解决

还可能是什么?如何解决?

  • 我没有更改默认的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;
        }
    }
}

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

请尝试以下操作,在limits.conf中执行以下更改

vi/etc/security/limits.conf

用于打开的文件
  • 软文件64000
  • 硬文件64000

    对于最大用户进程
  • 软nproc 47758

  • 硬nproc 47758

    最大内存大小
  • 软rss无限

  • 硬rss无限

    用于虚拟内存
  • 软如无边

  • 硬如无边

只需将其放在Nginx配置文件的顶部即可:

worker_rlimit_nofile 40000;

events {
    worker_connections  4096;
}

我想我发现了问题:

这是nginx error.log

2015/07/09 14:17:27 [error] 15390#0: *7549 connect() failed (111: Connection refused) while connecting to upstream, client: 23.239.194.233, server: , request: "GET http://www.lgqfz.com/ HTTP/1.1", upstream: "http://127.0.0.3:80/", host: "www.lgqfz.com", referrer: "http://www.baidu.com"
2015/07/09 14:17:29 [error] 15390#0: *8121 connect() failed (111: Connection refused) while connecting to upstream, client: 204.44.65.119, server: , request: "GET http://www.lgqfz.com/ HTTP/1.1", upstream: "http://127.0.0.3:80/", host: "www.lgqfz.com", referrer: "http://www.baidu.com"
2015/07/09 14:17:32 [error] 15390#0: *8650 connect() failed (101: Network is unreachable) while connecting to upstream, client: 78.47.53.98, server: , request: "GET http://188.8.253.161/ HTTP/1.1", upstream: "http://188.8.253.161:80/", host: "188.8.253.161", referrer: "http://188.8.253.161/"
这是对我的代理服务器的DDOS攻击,我通过只允许我的IP访问该代理服务器来阻止该攻击

我最近发现这很常见-当你抓取一个站点,并且该站点将你识别为一个抓取者时,它有时会攻击你的代理,直到它们变黑。
这类网站的一个例子是amazon.com

,什么是Nginx配置?我应该如何处理Nginx配置?我添加了行(与它们完全相同),我应该重新启动吗?srv重新启动后仍然会发生:(请尝试重新启动nginx并将这两项服务联网。我试图理解-机器上没有负载,为什么转发代理会使用这么多打开的文件?我们试图根据我们所知道的进行猜测。在这种情况下,打开的文件限制增加通常是第一步。nginx抛出异常只是出于某种原因,请找出原因。)共享top、mpstat、iostat命令的输出。我在问题中添加了top、mpstat、iostat的输出,10倍
2015/07/09 14:17:27 [error] 15390#0: *7549 connect() failed (111: Connection refused) while connecting to upstream, client: 23.239.194.233, server: , request: "GET http://www.lgqfz.com/ HTTP/1.1", upstream: "http://127.0.0.3:80/", host: "www.lgqfz.com", referrer: "http://www.baidu.com"
2015/07/09 14:17:29 [error] 15390#0: *8121 connect() failed (111: Connection refused) while connecting to upstream, client: 204.44.65.119, server: , request: "GET http://www.lgqfz.com/ HTTP/1.1", upstream: "http://127.0.0.3:80/", host: "www.lgqfz.com", referrer: "http://www.baidu.com"
2015/07/09 14:17:32 [error] 15390#0: *8650 connect() failed (101: Network is unreachable) while connecting to upstream, client: 78.47.53.98, server: , request: "GET http://188.8.253.161/ HTTP/1.1", upstream: "http://188.8.253.161:80/", host: "188.8.253.161", referrer: "http://188.8.253.161/"