Table of Contents
回复内容:
Home Backend Development PHP Tutorial php+nginx 上传大文件 502 Bad Gateway

php+nginx 上传大文件 502 Bad Gateway

Jun 06, 2016 pm 08:19 PM
nginx php

由于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,
最后没用,自己试试就知道了,不多说了,多谢好人!

<code>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;     
</code>
Copy after login

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

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

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

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

<code>upload_max_filesize=100M
post_max_size=128M
</code>
Copy after login
<code>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
<?php if(!empty($_FILES)){
    $uploaddir = '/png/www/example.com/public_html/app/yab/static/';
    $uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

    echo '<pre class="brush:php;toolbar:false">';
    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 "</code>
Copy after login
"; exit(); } ?>
Send this file:

输出:

<code>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
        )

)
</code>
Copy after login

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

<code>client_max_body_size 128M;
</code>
Copy after login

经过我测试,上传文件是可以上传成功的,但是我用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错?

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What's wrong with nginx running for a while? What's wrong with nginx running for a while? Apr 14, 2025 am 07:18 AM

The reasons why nginx hangs up after running for a period of time: 1. Memory leak; 2. Configuration error; 3. Insufficient resources; 4. External factors. Solution: 1. Diagnose memory leaks; 2. Fix configuration errors; 3. Provide more resources; 4. Exclude external factors.

How to implement nginx load balancing How to implement nginx load balancing Apr 14, 2025 am 07:21 AM

Nginx load balancing defines backend servers through the upstream module and uses the location block to proxy the request to these servers. Supports load balancing strategies such as polling, minimum number of connections, response time weighting, and ip_hash. Configuration examples include defining an upstream group and pointing to it using the proxy_pass directive.

nginx restart command nginx restart command Apr 14, 2025 am 07:27 AM

nginx restart command: sudo systemctl restart nginx. Other related commands include: 1. Start: sudo systemctl start nginx; 2. Stop: sudo systemctl stop nginx; 3. Check status: sudo systemctl status nginx.

Apr 14, 2025 am 07:36 AM

Nginx Autoindex is a function of generating directory listing HTML pages, which is used to browse files and view file information when requesting directories instead of files. It can be customized with configuration options such as displaying the exact file size, local time, and custom page format. Advantages include easy browsing, easy configuration and providing file information. Disadvantages include security risks, performance impact, and the inability to customize the appearance of the page.

What is the reason for nginx403 What is the reason for nginx403 Apr 14, 2025 am 07:39 AM

nginx 403 error indicates that the client does not have permission to access the resource. Factors that cause this problem may include: permission settings, nginx configuration, CGI script errors, .htaccess files, or other reasons. Troubleshooting steps include: checking permission settings, reviewing nginx configuration, testing CGI scripts, checking .htaccess files, excluding firewalls or security software, and checking servers and file systems.

How to configure virtual hosts in nginx How to configure virtual hosts in nginx Apr 14, 2025 am 08:15 AM

Configuring nginx virtual host allows multiple websites to be hosted on a single server, each with a separate domain name and root directory. The specific configuration steps include: creating a virtual host configuration file to configure the server block, specifying the server listening port, virtual host domain name and document root directory to enable the virtual host, and linking the configuration file to the enabled directory to reload nginx

The relationship between nginx and web server The relationship between nginx and web server Apr 14, 2025 am 07:09 AM

nginx is a lightweight, non-blocking web server and reverse proxy, commonly used for front-end proxy, load balancing, and caching. Its relationship with a web server is usually: Front-end proxy: nginx handles requests and forwards them to the back-end server. Load Balancer: nginx distributes requests to multiple backend servers. Caching: nginx caches frequently accessed files for performance.

How to redirect in nginx How to redirect in nginx Apr 14, 2025 am 08:42 AM

Methods for redirecting through Nginx are 301 permanent redirects (update links or mobile pages) and 302 temporary redirects (handling errors or temporary changes). Configuring redirection involves using location directives in server blocks, advanced features include regular expression matching, proxy redirection, and condition-based redirection. Common uses of redirects include updating URLs, handling errors, redirecting HTTP to HTTPS, and guiding users to a specific country or language version.

See all articles