最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Large File Request via API Returns Empty Response with 200 Status - Stack Overflow

programmeradmin5浏览0评论

I'm encountering an issue with PHP and Nginx when handling large file return via an API.

Issue:

Small files (under 2GB) are processed correctly. Large files result in an empty response with a 200 status code.

Configuration:

  • php.ini:
    • post_max_size = 800 -> 4000
    • upload_max_filesize = 2000 -> 4000
  • nginx.conf:
    • client_max_body_size = 300 -> 4000
    • memory_limit = -1

Restart procedure:

sudo php-fpm8.3 -t 
sudo service php8.3-fpm 
sudo systemctl reload nginx

Verified the configuration was applied (php -i).

Error logs:

Checked logs (/var/log/nginx/error.log), but found no errors.

What could be causing the issue?

  • Adjusted php.ini settings (post_max_size, upload_max_filesize, memory_limit).
  • Modified nginx.conf (client_max_body_size).
  • Restarted PHP-FPM and Nginx.
  • Checked logs (/var/log/nginx/error.log).

What I Expected:

Large files (over 2GB) should be processed and returned correctly.

I'm encountering an issue with PHP and Nginx when handling large file return via an API.

Issue:

Small files (under 2GB) are processed correctly. Large files result in an empty response with a 200 status code.

Configuration:

  • php.ini:
    • post_max_size = 800 -> 4000
    • upload_max_filesize = 2000 -> 4000
  • nginx.conf:
    • client_max_body_size = 300 -> 4000
    • memory_limit = -1

Restart procedure:

sudo php-fpm8.3 -t 
sudo service php8.3-fpm 
sudo systemctl reload nginx

Verified the configuration was applied (php -i).

Error logs:

Checked logs (/var/log/nginx/error.log), but found no errors.

What could be causing the issue?

  • Adjusted php.ini settings (post_max_size, upload_max_filesize, memory_limit).
  • Modified nginx.conf (client_max_body_size).
  • Restarted PHP-FPM and Nginx.
  • Checked logs (/var/log/nginx/error.log).

What I Expected:

Large files (over 2GB) should be processed and returned correctly.

Share Improve this question edited 13 hours ago VLAZ 29.1k9 gold badges62 silver badges84 bronze badges asked yesterday seeun choiseeun choi 11 New contributor seeun choi is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 6
  • I think it could be due to a timeout. Have you set all of these?max_input_time = 6000, max_execution_time = 6000, and memory_limit = -1 in the php.ini file. Also, make sure you have set the client_max_body_size in both the HTTP block and the server block in the nginx.conf file. – Kamyar Safari Commented yesterday
  • @KamyarSafari Same error occurred.. PHP Info: max_input_time = -1 max_execution_time = 0 memory_limit = -1 Nginx Config: HTTP Block: client_max_body_size 10G Server Block: client_max_body_size 4000M – seeun choi Commented yesterday
  • It may require adjusting buffer settings in Nginx to handle large responses: ` server {` ` client_max_body_size 10G;` ` # Increase buffer sizes` ` client_body_buffer_size 10M;` ` proxy_buffer_size 16k;` ` proxy_buffers 4 16k;` ` proxy_busy_buffers_size 32k;` ` sendfile on;` } AND Have you also restarted nginx and fpm? ` sudo service php8.3-fpm restart ` ` sudo service nginx restart ` – Kamyar Safari Commented yesterday
  • yes, I changed setting as below, and restarted nginx and fpm but still same.. sendfile on; tcp_nopush on; types_hash_max_size 2048; client_max_body_size 10G; client_body_timeout 2024; client_header_timeout 2024; large_client_header_buffers 8 64k; fastcgi_read_timeout 300s; fastcgi_send_timeout 300s; proxy_read_timeout 300s; client_body_buffer_size 10M; fastcgi_buffering off; proxy_buffer_size 16k; proxy_buffers 8 16k; proxy_busy_buffers_size 32k; – seeun choi Commented 23 hours ago
  • How did you implement sending large files with that API? This information is totally missing from your question. Some APIs, even on the binary level, do not have support for files of 2GB and greater (singed 32bit integers). Does this really needs to be tagged Nginx? – hakre Commented 20 hours ago
 |  Show 1 more comment

1 Answer 1

Reset to default -1

PHP/Nginx Large File Handling Configuration

This guide provides a comprehensive solution for handling large file uploads (>2GB) through PHP and Nginx APIs.

Common Issues

  • Empty response with 200 status code for files >2GB
  • Timeout during upload process
  • Memory exhaustion
  • Incomplete file transfers

Server Configuration

PHP Configuration (php.ini)

; File upload limits
post_max_size = 4000M
upload_max_filesize = 4000M
memory_limit = -1

; Timeouts
max_execution_time = 300
max_input_time = 300

; Buffer settings
output_buffering = Off
zlib.output_compression = Off

; Error reporting for debugging
error_reporting = E_ALL
display_errors = On
log_errors = On

Nginx Configuration (nginx.conf)

http {
    # Client body size
    client_max_body_size 4000M;
    
    # Timeouts
    fastcgi_read_timeout 300;
    fastcgi_send_timeout 300;
    proxy_read_timeout 300;
    
    # Buffer sizes
    client_body_buffer_size 128k;
    client_header_buffer_size 1k;
}

PHP-FPM Configuration

; Timeout settings
request_terminate_timeout = 300

; Slow log configuration
slowlog = /var/log/php-fpm/slow.log
request_slowlog_timeout = 60
发布评论

评论列表(0)

  1. 暂无评论