php curl timeout Guzzlehttp請求超時
阿新 • • 發佈:2020-08-14
問題根源有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