php - Nginx 在下载时损坏文件

标签 php nginx laravel-5

我在使用 php 和 Laravel 框架制作的应用程序时遇到问题。

问题在于文件上传/下载。

当我将文件提交到服务器时,它会很好地存储它们,但是当我尝试下载大于 100KB 的上传文件时,它只会下载其中的一部分,使其损坏。

通过调整php.ini设置,nginx设置尝试了很多选项,仍然无法解决。

这是我当前的 nginx 配置:

nginx.conf

user developer;
worker_processes 4;
pid /run/nginx.pid;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
     # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

现在这是我的 nginx 站点配置:

server {

    listen 8002 default_server;
    server_name localhost 172.20.74.229 cadeco.dev;

    root /var/www/current/cadeco/public;
    index index.php index.html index.htm;

    access_log /var/log/nginx/cadeco.dev-access.log;
    error_log /var/log/nginx/cadeco.dev-error.log error;

    charset utf-8;

    include h5bp/basic.conf;

    location = /favicon.ico { log_not_found off; access_log off; }
    location = /robots.txt { log_not_found off; access_log off; }

    location ~ /\. {
        deny all;
        access_log off;
        log_not_found off;
    }


    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    sendfile off;

    client_max_body_size 100m;

    location ~ ^/index\.php(/|$) {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;

        include fastcgi.conf;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

下面是(/etc/php5/fpm/php.ini)中php.ini的相关设置:

max_execution_time = 30
max_input_time = 60

memory_limit = 512M

upload_max_filesize = 50M
max_file_uploads = 20

最后这是我的文件下载 php 脚本:

public function downloadFile($file)
{
    $filePath = storage_path('app/uploads/').$file;

    if (Storage::exists($file))
    {
        return response()->download($filePath);
    }

    Flash::error('File does not exists!');

    return redirect()->back();
}

提前感谢您的帮助! :D

最佳答案

我想通了!

我检查了这个 nginx 站点的错误日志,发现了这个错误:

*10 open() "/var/lib/nginx/fastcgi/4/00/0000000004" failed (13: Permission denied) while reading upstream, client: 172.20.73.101, server: localhost, request: XXXXX

这个错误是因为前段时间我们把用户改成了www-data来启动php-fpm等服务,但是nginx忘记改了。

将用户更改为 www-data,现在一切正常!

谢谢!

关于php - Nginx 在下载时损坏文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29760332/

相关文章:

php - 如何在 laravel 9 中使用 diffForHumans 创建 "today"、 "yesterday"、 "3 days ago"?

nginx - 带有 Nginx 反向代理和解析器的 Jenkins

php - Laravel 在工厂测试的嵌套关系

PHP fatal error : Constant expression contains invalid operations

php - sql查询分割句子并搜索每个单词的结果

python - 将静态文件服务从 flask 更改为 nginx?

PHP-FPM 和 Nginx : "Resource temporarily unavailable" errors in proxy_error_log

php - 为什么我的创建表单请求在 Laravel5 中抛出 404 异常?

javascript - Laravel:通过 min.js 文件 "Uncaught ReferenceError: HelloWeek is not defined"包含 javascript 代码

php - 如何使用 PHP 忽略给定数组中的某些集合