Tôi có nginx 1.0.5 + php-cgi (PHP 5.3.6) đang chạy. Tôi cần tải lên ~ 1GB tệp (phải tải lên 1-5 video song song). Tôi đang cố gắng tạo các tệp lớn tải lên qua tải lên ajax. Mọi thứ đều hoạt động nhưng PHP ăn nhiều bộ nhớ cho mỗi lần tải lên. Tôi đã thiết lập memory_limit = 200M, nhưng nó làm việc lên đến ~ 150MB kích thước của tập tin được tải lên. Nếu tệp lớn hơn - tải lên không thành công. Tôi có thể đặt memory_limit lớn hơn và lớn hơn, nhưng tôi nghĩ đó là cách sai, khiến PHP có thể ăn tất cả bộ nhớ. tôi sử dụng mã PHP này (nó đơn giản) để xử lý tải lên trên phía máy chủ:Giảm mức tiêu thụ bộ nhớ trong PHP trong khi xử lý tải lên bằng đầu vào php
$input = fopen('php://input', 'rb');
$file = fopen('/tmp/' . $_GET['file'] . microtime(), 'wb');
while (!feof($input)) {
fwrite($file, fread($input, 102400));
}
fclose($input);
fclose($file);
/etc/nginx/nginx.conf:
user www-data;
worker_processes 100;
pid /var/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;
client_max_body_size 2g;
# server_tokens off;
server_names_hash_max_size 2048;
server_names_hash_bucket_size 128;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
/etc/nginx/sites-enabled/srv .conf:
server {
listen 80;
server_name srv.project.loc;
# Define root
set $fs_webroot "/home/andser/public_html/project/srv";
root $fs_webroot;
index index.php;
# robots.txt
location = /robots.txt {
alias $fs_webroot/deny.robots.txt;
}
# Domain root
location/{
if ($request_method = OPTIONS) {
add_header Access-Control-Allow-Origin "http://project.loc";
add_header Access-Control-Allow-Methods "GET, OPTIONS, POST";
add_header Access-Control-Allow-Headers "Authorization,X-Requested-With,X-File-Name,Content-Type";
#add_header Access-Control-Allow-Headers "*";
add_header Access-Control-Allow-Credentials "true";
add_header Access-Control-Max-Age "10000";
add_header Content-Length 0;
add_header Content-Type text/plain;
return 200;
}
try_files $uri $uri/ /index.php?$query_string;
}
#error_page 404 /404.htm
location ~ index.php {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $fs_webroot/$fastcgi_script_name;
include fastcgi_params;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param PATH_INFO $fastcgi_script_name;
add_header Pragma no-cache;
add_header Cache-Control no-cache,must-revalidate;
add_header Access-Control-Allow-Origin *;
#add_header Access-Control-Allow-Headers "Content-Type, X-Requested-With, X-File-Name";
}
}
Bất kỳ ai biết cách giảm mức tiêu thụ bộ nhớ bằng PHP? Cảm ơn.
Tôi không hiểu kịch bản của bạn thực sự đang làm gì .. bạn đã thử sử dụng 'copy' chưa? Tại sao bạn cần phải sử dụng php cho điều này như trái ngược với chỉ ftping tập tin? –
> Tôi không hiểu kịch bản của bạn thực sự đang làm gì Lưu tệp đã tải lên. > bạn đã thử sử dụng bản sao chưa? Và làm thế nào nó có thể giúp đỡ? > Tại sao bạn cần phải sử dụng php cho điều này như trái ngược với chỉ ftping tập tin? Vì tôi cần tải lên web. Tôi không cần FTP. – andser
Có lẽ bạn có thể bỏ qua PHP hoàn toàn và sử dụng mô-đun Nginx Upload? (http://www.grid.net.ru/nginx/upload.en.html) – mobius