php - 两个客户端之间的 CURL 请求因超时而失败
问题描述
我有两个应用程序,一个 API 及其在 Windows 上的客户端,带有 Nginx 和带有 Fastcgi 的 PHP 7.1。当我从我的 API 向我的客户端或从我的客户端向我的 API 发送 CURL 请求时,它可以正常工作。
另一方面,例如,如果 API 向客户端发送请求,客户端也向 API 发送请求,则情况不再如此。我的第一个请求将在 30 秒后超时,在此期间我无法向客户端并行发送请求。他们都将超时,直到第一个结束。
到目前为止我尝试了什么:
- 我将此添加到我的 Nginx 配置中
fastcgi_read_timeout 120s;
upstream php-cgi {
server 127.0.0.1:9000 max_conns=5 weight=1 max_fails=0 fail_timeout=30s;
server 127.0.0.1:9001 max_conns=5 weight=1 max_fails=0 fail_timeout=30s;
server 127.0.0.1:9002 max_conns=5 weight=1 max_fails=0 fail_timeout=30s;
server 127.0.0.1:9003 max_conns=5 weight=1 max_fails=0 fail_timeout=30s;
server 127.0.0.1:9004 max_conns=5 weight=1 max_fails=0 fail_timeout=30s;
server 127.0.0.1:9005 max_conns=5 weight=1 max_fails=0 fail_timeout=30s;
server 127.0.0.1:9006 max_conns=5 weight=1 max_fails=0 fail_timeout=30s;
server 127.0.0.1:9007 max_conns=5 weight=1 max_fails=0 fail_timeout=30s;
server 127.0.0.1:9008 max_conns=5 weight=1 max_fails=0 fail_timeout=30s;
server 127.0.0.1:9009 max_conns=5 weight=1 max_fails=0 fail_timeout=30s;
server 127.0.0.1:9012 max_conns=5 weight=1 max_fails=0 fail_timeout=30s;
}
以下是我使用的 CURL 选项:
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLINFO_HEADER_OUT => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_URL=> MY_URL,
CURLOPT_POSTFIELDS=> MY_DATA,
CURLOPT_CUSTOMREQUEST=> MY METHOD,
CURLOPT_HTTPHEADER=> MY HEADERS
这就是我启动服务器的方式:
c:\nginx\RunHiddenConsole.exe c:\nginx\php-7.1\php-cgi.exe -b 127.0.0.1:9005 -c c:\nginx\php-7.1\php.ini
c:\nginx\RunHiddenConsole.exe c:\nginx\php-7.1\php-cgi.exe -b 127.0.0.1:9007 -c c:\nginx\php-7.1\php.ini
c:\nginx\RunHiddenConsole.exe c:\nginx\nginx.exe
还有我的 Nginx 配置:
worker_processes 5;
error_log logs/error.log notice;
#pid logs/nginx.pid;
events {
worker_connections 64;
multi_accept on;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
#access_log off;
# Optimisation sur l'envoie des fichier sans passer par des buffer
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 600;
server_name_in_redirect off;
server_names_hash_bucket_size 64;
# Compression
gzip on;
gzip_comp_level 6;
gzip_min_length 1000;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
# Taille de headers HTTP en prereponse
proxy_connect_timeout 159s;
proxy_send_timeout 600;
proxy_read_timeout 600;
proxy_buffer_size 64k;
proxy_buffers 16 256k;
proxy_busy_buffers_size 256k;
fastcgi_buffer_size 256k;
fastcgi_buffers 8 256k;
fastcgi_busy_buffers_size 256k;
proxy_temp_file_write_size 256k;
client_max_body_size 200m;
######################################
######################################
# fastcgi_cache_path c:/nginx/cache/ levels=1:2 keys_zone=www_cache:10m inactive=1h max_size=1g;
server {
listen 443 ssl http2;
server_name my-api.com
root c:\www\my-api.com\web;
ssl on;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_certificate ../cert/server.crt;
ssl_certificate_key ../cert/server.key;
location / {
# try to serve file directly, fallback to app.php
try_files $uri /app_dev.php$is_args$args;
}
# DEV
# This rule should only be placed on your development environment
# In production, don't include this and don't deploy app_dev.php or config.php
location ~ ^/(app_dev|config)\.php(/|$) {
fastcgi_pass 127.0.0.1:9005;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
}
# return 404 for all other php files not matching the front controller
# this prevents access to other php files you don't want to be accessible.
location ~ \.php$ {
return 404;
}
}
server {
listen 443 ssl http2;
server_name my-client.com;
root c:\www\my-client.com\public;
ssl on;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_certificate ../cert/server.crt;
ssl_certificate_key ../cert/server.key;
location / {
# try to serve file directly, fallback to app.php
try_files $uri /index.php$is_args$args;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires 1h;
}
location ~ ^/index\.php(/|$) {
fastcgi_pass 127.0.0.1:9007;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
internal;
}
# return 404 for all other php files not matching the front controller
# this prevents access to other php files you don't want to be accessible.
location ~ \.php$ {
return 404;
}
}
}
解决方案
感谢这个链接(https://www.nginx.com/resources/wiki/start/topics/examples/fastcgiexample/),我能够理解有必要在我的启动脚本中定义变量 PHP_FCGI_MAX_REQUESTS 以便 API可以响应同时收到的多个请求。
SET PHP_FCGI_CHILDREN=5
SET PHP_FCGI_MAX_REQUESTS=0
c:\nginx\RunHiddenConsole.exe c:\nginx\php-7.1\php-cgi.exe -b 127.0.0.1:9005 -c c:\nginx\php-7.1\php.ini
c:\nginx\RunHiddenConsole.exe c:\nginx\php-7.1\php-cgi.exe -b 127.0.0.1:9007 -c c:\nginx\php-7.1\php.ini
c:\nginx\RunHiddenConsole.exe c:\nginx\nginx.exe
推荐阅读
- android - java.lang.IllegalStateException:pb 在 TopHeadlinesFragment 处不能为空?
- postgresql - 如何获取数据库中定义的外键约束?
- nested - 没有重新计算 HSL 的 CSS 变量
- jquery - 滚动到达部分末尾时更改元素的不透明度
- c - 如何将 c 中的整数位字段连接成 uint64_t 整数?
- typescript - 用于检查文本正文的 Eslint 插件
- junit - 如何在 PC JVM 上运行 espresso 仪器测试前提条件?
- python - Pandas MultiIndex 切片不排序
- python - python - 如何忽略接受python中多个参数的函数中的字符串?
- angular - 如何在 Angular 8 中获取我的 UI 组件