首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >两个客户端之间的CURL请求由于超时而失败。

两个客户端之间的CURL请求由于超时而失败。
EN

Stack Overflow用户
提问于 2019-07-17 07:28:27
回答 1查看 783关注 0票数 1

我有两个应用程序,一个API及其在Windows上的客户端,一个是Nginx,另一个是PHP7.1和Fastcgi。当我从API发送卷曲请求到我的客户端或从我的客户端发送到我的API时,它正常工作。

另一方面,如果API向客户端发送请求,客户机也向API发送请求,则不再是这种情况。我的第一个请求将超时30秒后,在此期间,我不能发送一个请求并行到客户端。他们都会继续超时直到第一次结束。

到目前为止我尝试过的:

  • 我把这个添加到我的Nginx配置中
代码语言:javascript
复制
     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选项:

代码语言:javascript
复制
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

我就是这样启动服务器的:

代码语言:javascript
复制
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配置:

代码语言:javascript
复制
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;
        }
    }   

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-20 09:44:02

由于这个链接(https://www.nginx.com/resources/wiki/start/topics/examples/fastcgiexample/),我能够理解在我的启动脚本中定义变量PHP_FCGI_MAX_REQUESTS是必要的,这样API就可以同时响应多个请求。

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57070647

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档