Fork me on GitHub

php curl timeout Guzzlehttp请求超时

问题根源有2个:

1,windows上测试

2,使用了nginx

今天在搭建一个laravel项目的时候,遇到了一个问题,Apache可以正常使用,换成nginx就请求超时。排查到使用Guzzlehttp请求自己的时候出错了。
以前也遇到过类似的,当时也是在nginx上,做了一次curl,然后一直超时。

问题原因:

我猜测是php只启动了一个进程,nginx又不维护进程池,单个请求还好,一旦出现自己请求自己,就麻瓜了。因为自己请求自己,不就是递归吗,但是提供递归的服务是单线程,发请求占用了唯一的线程,一直hang到超时。

解决方法:

总共分三步

第一步
本地起10个php-cgi进程,分别监听不同的端口。找到自己php所在的目录,CMD下输入

D:
cd D:\www\php7.1
php-cgi.exe -b 127.0.0.1:9071 -c D:\www\php7.1
php-cgi.exe -b 127.0.0.1:9072 -c D:\www\php7.1
php-cgi.exe -b 127.0.0.1:9073 -c D:\www\php7.1
php-cgi.exe -b 127.0.0.1:9074 -c D:\www\php7.1
php-cgi.exe -b 127.0.0.1:9075 -c D:\www\php7.1
php-cgi.exe -b 127.0.0.1:9076 -c D:\www\php7.1
php-cgi.exe -b 127.0.0.1:9077 -c D:\www\php7.1
php-cgi.exe -b 127.0.0.1:9078 -c D:\www\php7.1
php-cgi.exe -b 127.0.0.1:9079 -c D:\www\php7.1
php-cgi.exe -b 127.0.0.1:9070 -c D:\www\php7.1
pause

  

第二步

修改nginx.conf配置,对这些进程,做负载均衡处理

events {
    worker_connections  1024;
}
worker_processes auto;
error_log D:/www/nginx/logs/error.log;
pid D:/www/nginx/logs/nginx.pid;
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    tcp_nopush     on;
    tcp_nodelay         on;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    types_hash_max_size 2048;
    #gzip  on;
    include vhost/*.conf;
####注意,下面这一块儿就是对上面进程的管理
    upstream php_farm {
        server 127.0.0.1:9070 weight=1; 
        server 127.0.0.1:9071 weight=1;
        server 127.0.0.1:9072 weight=1;
        server 127.0.0.1:9073 weight=1;
        server 127.0.0.1:9074 weight=1;
        server 127.0.0.1:9075 weight=1;
        server 127.0.0.1:9076 weight=1;
        server 127.0.0.1:9077 weight=1;
        server 127.0.0.1:9078 weight=1;
        server 127.0.0.1:9079 weight=1;
    }

}

  

第三步

实际项目中,增加监听(因为是laravel项目遇到的,所以部分conf是兼容laravel的路由逻辑,不是laravel项目的同学只需要注意fast_cgi的配置那里就行)

server {
    listen  987;
    server_name www.a.com;
    root D:/project/laravel/public;
    index index.php index.html index.htm;
    try_files $uri $uri/ @rewrite;
    location @rewrite {
        rewrite ^/(.*)$ /index.php?_url=/$1;
    }
    location ~ \.php {
        add_header Access-Control-Allow-Origin http://localhost;
        add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
        add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization,lang,access-token';
        if ($request_method = 'OPTIONS') {
            return 204;
        }

        fastcgi_pass php_farm;
        fastcgi_index /index.php;
        fastcgi_split_path_info       ^(.+\.php)(/.+)$;
        fastcgi_param PATH_INFO       $fastcgi_path_info;
        fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include                       fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}

  重启服务,搞定,enjoy it!

 
作者:怀老师
链接:https://www.jianshu.com/p/615395ebf078
posted @ 2020-08-14 09:36  君乐豹  阅读(926)  评论(0编辑  收藏  举报
Live2D