development

PHP set_time_limit ()를 사용하여 nginx 504 게이트웨이 시간 초과 방지

big-blog 2020. 8. 3. 17:19
반응형

PHP set_time_limit ()를 사용하여 nginx 504 게이트웨이 시간 초과 방지


PHP 스크립트가 평소보다 오래 실행될 때 nginx에서 504 시간 초과 메시지가 표시됩니다. set_time_limit(0)그것을 막지 않는 것 같습니다! nginx에서 php5-fpm을 실행할 때 작동하지 않습니까? 그렇다면 시간 제한을 설정하는 올바른 방법은 무엇입니까?

오류:

504 Gateway Time-out
nginx/1.2.7

php-fpm의 타임 아웃을 설정하는 방법에는 여러 가지가 있습니다. 에서 /etc/php5/fpm/pool.d/www.conf나는이 줄을 추가 :

request_terminate_timeout = 180

또한 /etc/nginx/sites-available/default문제의 서버의 위치 블록에 다음 줄을 추가했습니다.

fastcgi_read_timeout 180;

전체 위치 블록은 다음과 같습니다.

location ~ \.php$ {
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_read_timeout 180;
    include fastcgi_params;
} 

이제 php-fpm과 nginx를 다시 시작하면 180 초 미만의 요청에 대한 시간 초과가 없어야합니다.


링크를 사용해보십시오.이 문제를 해결하는 방법에 대한 더 나은 솔루션이 있습니다. 단계는 다음과 같습니다.

  1. 디렉토리 nginx.conf에있는 파일을여 십시오 /etc/nginx.
  2. 아래 http {섹션 아래에 이것을 추가 하십시오 :

    client_header_timeout 3000;
    client_body_timeout 3000;
    fastcgi_read_timeout 3000;
    client_max_body_size 32m;
    fastcgi_buffers 8 128k;
    fastcgi_buffer_size 128k;
    

    참고 : 이미있는 경우 값을 변경하십시오.

  3. Nginx 및 php5-fpm을 다시로드하십시오.

    $ service nginx reload
    $ service php5-fpm reload
    

    오류가 지속되면 값을 늘리십시오.


nginx가 발행 한 타임 아웃을 막기 위해 PHP를 사용할 수 없습니다.

더 많은 시간을 허용하도록 nginx를 구성하려면 proxy_read_timeout지시문을 참조하십시오 .


정답은 Nginx 구성에서 fastcgi_read_timeout늘리는 입니다.
그렇게 간단합니다!


 sudo nano /etc/nginx/nginx.conf

다음 변수를 nginx.conf 파일에 추가하십시오.

http {  
  # .....
  proxy_connect_timeout       600;
  proxy_send_timeout          600;
  proxy_read_timeout          600;
  send_timeout                600;
}

그런 다음 다시 시작하십시오.

service nginx reload

이러한 경우에 발생할 수있는 세 가지 유형의 시간 초과가 있습니다. 각 답변은 이러한 가능성 중 한 가지 측면에만 초점을두고 있음을 알 수 있습니다. 그래서 나는 나중에 여기를 방문하는 사람이 각 답변을 무작위로 확인하고 어느 것이 효과가 있었는지 알지 못하고 성공할 필요가 없도록 그것을 적어 놓았습니다.

  1. 요청자의 요청 시간 초과-시간 초과 헤더를 설정해야 함 (요청 라이브러리의 헤더 구성 참조)
  2. 요청하는 동안 (프록시 된 서버로 전달하기 전에) nginx 에서 시간이 초과되었습니다. 예 : 큰 파일이 업로드되고 있습니다.
  3. 프록시 서버로 전달한 후 시간이 초과 되면 서버는 nginx를 제 시간에 응답하지 않습니다. 예 : 서버에서 실행되는 시간이 많이 걸리는 스크립트

각 문제에 대한 수정 사항은 다음과 같습니다.

  1. 타임 아웃 헤더 설정 예 : 아약스에서

$.ajax({
    url: "test.html",
    error: function(){
        // will fire when timeout is reached
    },
    success: function(){
        //do something
    },
    timeout: 3000 // sets timeout to 3 seconds
});

  1. nginx 클라이언트 타임 아웃

    http{
         #in seconds
        fastcgi_read_timeout 600;
        client_header_timeout 600;
        client_body_timeout 600;
     }
    
  2. nginx proxied server timeout

    http{
      #Time to wait for the replying server
       proxy_read_timeout 600s;
    
    }
    

So use the one that you need. Maybe in some cases, you need all these configurations. I needed.


You need to add extra nginx directive (for ngx_http_proxy_module) in nginx.conf, e.g.:

proxy_read_timeout 300;

Basically the nginx proxy_read_timeout directive changes the proxy timeout, the FcgidIOTimeout is for scripts that are quiet too long, and FcgidBusyTimeout is for scripts that take too long to execute.

Also if you're using FastCGI application, increase these options as well:

FcgidBusyTimeout 300
FcgidIOTimeout 250

Then reload nginx and PHP5-FPM.

Plesk

In Plesk, you can add it in Web Server Settings under Additional nginx directives.

For FastCGI check in Web Server Settings under Additional directives for HTTP.

See: How to fix FastCGI timeout issues in Plesk?


Since you're using php-fpm you should take advantage of fastcgi_finish_request() for processing requests you know can take longer.


Using set_time_limit(0) is useless when using php-fpm or similar process manager.

Bottomline is not to use set_time_limit when using php-fpm, to increase your execution timeout, check this tutorial.


I solve this trouble with config APACHE ! All methods (in this topic) is incorrect for me... Then I try chanche apache config:

Timeout 3600

Then my script worked!

참고URL : https://stackoverflow.com/questions/16002268/prevent-nginx-504-gateway-timeout-using-php-set-time-limit

반응형