欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php+nginx 上传大文件 502 Bad Gateway

程序员文章站 2022-05-31 11:32:49
...
由于php每天要接收其它服务器post过来的文件
比如我在:192.168.1.147上用下面这条命令
wget http://192.168.1.148/upload.php --post-file=msg.log
这个msg.log 有50M,进行post msg.log这个文件到148服务器的php上存储,进行入库

首先说明:
不要问我怎么不直接复制文件,要用php接收?
我现在讨论的是这个问题,谢谢。

原因:
首次调用出现了:
HTTP request sent, awaiting response... 413 Request Entity Too Large
我按照网上的教程改nginx配置
改为:
client_max_body_size 100m;
满以为解决问题,再次执行报错。
HTTP request sent, awaiting response... 502 Bad Gateway

网上有的说php.ini要配置,但是我的php.ini 配置是64M
max_execution_time 300 300
max_file_uploads 20 20
max_input_nesting_level 64 64
max_input_time 60 60
max_input_vars 1000 1000
memory_limit 128M 128M
open_basedir no value no value
output_buffering 4096 4096
output_handler no value no value
post_max_size 64M 64M
upload_max_filesize 64M 64M

都找了好多,都是说配置php.ini, nginx.conf,还有php-fpm.conf的request_terminate_timeout=0,
最后没用,自己试试就知道了,不多说了,多谢好人!

回复内容:

由于php每天要接收其它服务器post过来的文件
比如我在:192.168.1.147上用下面这条命令
wget http://192.168.1.148/upload.php --post-file=msg.log
这个msg.log 有50M,进行post msg.log这个文件到148服务器的php上存储,进行入库

首先说明:
不要问我怎么不直接复制文件,要用php接收?
我现在讨论的是这个问题,谢谢。

原因:
首次调用出现了:
HTTP request sent, awaiting response... 413 Request Entity Too Large
我按照网上的教程改nginx配置
改为:
client_max_body_size 100m;
满以为解决问题,再次执行报错。
HTTP request sent, awaiting response... 502 Bad Gateway

网上有的说php.ini要配置,但是我的php.ini 配置是64M
max_execution_time 300 300
max_file_uploads 20 20
max_input_nesting_level 64 64
max_input_time 60 60
max_input_vars 1000 1000
memory_limit 128M 128M
open_basedir no value no value
output_buffering 4096 4096
output_handler no value no value
post_max_size 64M 64M
upload_max_filesize 64M 64M

都找了好多,都是说配置php.ini, nginx.conf,还有php-fpm.conf的request_terminate_timeout=0,
最后没用,自己试试就知道了,不多说了,多谢好人!

nginx配置:

    keepalive_timeout 600; 

    proxy_send_timeout 600;

    client_max_body_size 200M;  

    fastcgi_connect_timeout 600;

    fastcgi_send_timeout 600;

    fastcgi_read_timeout 600;

    fastcgi_buffer_size 1024k;

    fastcgi_buffers 8 1024k;

    fastcgi_busy_buffers_size 1024k;

    fastcgi_temp_file_write_size 1024k;

    proxy_ignore_client_abort on;

    tcp_nopush on;

    tcp_nodelay on;

    client_header_timeout 600; 

    client_body_timeout 600; 

    sendfile on; 

    send_timeout 600;     

php的配置或php-fpm上传大小配置及post大小也需要修改

根据你的实际情况修改上边的配置吧

具体原因不清楚, 不过502错误一般都是PHP那边出了问题
不是没响应,就是响应时间过长,等。
这里已经和nginx没什么关系了

我在Xubuntu 14.04上用测试PHP CLI Server,只配置了下面两条,其他都是默认配置,就能上传90多MB的ZIP包了.

upload_max_filesize=100M
post_max_size=128M
php -S 127.0.0.1:8080 -t /png/www/example.com/public_html/
// chmod 777 /png/www/example.com/public_html/app/yab/static/
// http://127.0.0.1:8080/app/yab/upload.php
';
    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
        echo "File is valid, and was successfully uploaded.\n";
    } else {
        echo "Possible file upload attack!\n";
    }

    echo 'Here is some more debugging info:';
    print_r($_FILES);

    print "
"; exit(); } ?>
Send this file:

输出:

File is valid, and was successfully uploaded.
Here is some more debugging info:Array
(
    [userfile] => Array
        (
            [name] => firefox.zip
            [type] => application/zip
            [tmp_name] => /tmp/phpG1PdtU
            [error] => 0
            [size] => 93938426
        )

)

同时本地测试了Nginx + PHP-FPM,Nginx只修改了下面这个配置:

client_max_body_size 128M;

经过我测试,上传文件是可以上传成功的,但是我用wget --post-file 就返回502错误
wget可能有限制大文件上传,
用curl命令搞定:
curl -T "msg.log" "http://192.168.1.148/upload.php"
谢谢大家 结贴!

nginx_module_upload

你把php的执行时间调大一点,另外上传超时也调大一点。
set_time_limit(),max_input_time之类的。你是不是第一次调完后再上传会等一会儿才报502错?

相关标签: php nginx