简体   繁体   中英

CURL request between two clients fails with a timeout

I have two applications, an API and its client on Windows, with Nginx and PHP 7.1 with Fastcgi. When I send a CURL request from my API to my client or from my client to my API it works properly.

On the other hand, this is no longer the case if, for example, the API sends a request to the client which also sends a request to the API. My first request will timeout after 30 seconds and during this time I can not send a request parallel to the client. They will all go on timeout until the first one ends.

What I tried so far:

  • I added this to my Nginx configuration
     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;
        }

Here are the CURL options I use:

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

This is how I start my servers :

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

And my Nginx configuration :

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;
        }
    }   

}

Thanks to this link ( https://www.nginx.com/resources/wiki/start/topics/examples/fastcgiexample/ ), I was able to understand that it was necessary to define the variable PHP_FCGI_MAX_REQUESTS in my startup script so the API can respond to several requests received at the same time.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM