簡體   English   中英

兩個客戶端之間的 CURL 請求因超時而失敗

[英]CURL request between two clients fails with a timeout

我有兩個應用程序,一個 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM