Ubuntu

PHP + NGINX 配置的下載大小不一致

  • November 16, 2015

我正在使用 docman 建構一個 Joomla 網站,用於文件管理/下載。該站點建立在執行 Nginx 1.4.2 和 PHP5 (FPM) 的 Ubuntu 14.04 伺服器上。

我遇到的問題是文件下載不一致(就大小而言)。例如,我從網站下載了一個 PDF 文件 6 次,結果如下:

  • 72KB
  • 75KB
  • 146KB
  • 177KB(完整文件)
  • 72KB
  • 177KB(完整文件)

對於此測試,完整文件下載正確 6 次中的 2 次。其他 4 次文件不完整。

我的 nginx.conf 看起來像這樣

user       www-data;
worker_processes  1;
error_log  logs/error.log;
worker_rlimit_nofile 8192;

events {
       worker_connections  4096;
}

http {
       include    /etc/nginx/conf/mime.types;
       include    /etc/nginx/proxy.conf;
       include    /etc/nginx/fastcgi.conf;
       index    index.php app.php index.html index.htm;
       default_type application/octet-stream;
       log_format   main '$remote_addr - $remote_user [$time_local]  $status '
       '"$request" $body_bytes_sent "$http_referer" '
       '"$http_user_agent" "$http_x_forwarded_for"';
       access_log   logs/access.log  main;
       sendfile     on;
       tcp_nopush   on;
       server_names_hash_bucket_size 128; # this seems to be required for some vhosts

       keepalive_timeout 0;

       #GZIP#
       gzip on;
       gzip_http_version 1.1;
       gzip_comp_level 6;
       gzip_min_length 1100;
       gzip_buffers 4 8k;
       gzip_types text/plain application/xhtml+xml text/css application/xml application/xml+rss text/javascript application/javascript application/x-javascript
       gzip_proxied     any;
       gzip_disable     "MSIE [1-6]\.";

       include /etc/nginx/sites-enabled/*;

站點配置如下所示:

server {
       listen 80;
       server_name example.com;
       server_name_in_redirect off;

       access_log /var/log/nginx/localhost.access_log main;
       error_log /var/log/nginx/localhost.error_log info;

       root /www/sites/canmore.link/html;
       index index.php index.html index.htm default.html default.htm;
       # Support Clean (aka Search Engine Friendly) URLs
       location / {
               try_files $uri $uri/ /index.php?$args;
       }

       # deny running scripts inside writable directories
       location ~* /(images|cache|media|logs|tmp)/.*\.(php|pl|py|jsp|asp|sh|cgi)$ {
               return 403;
               error_page 403 /403_error.html;
       }

       location ~ \.php$ {
                       include fastcgi_params;
                       fastcgi_read_timeout 300;
                       fastcgi_intercept_errors on;
                       fastcgi_pass 127.0.0.1:9000;
       }


       # caching of files
       location ~* \.(ico|pdf|flv)$ {
               expires 1y;
       }

       location ~* \.(js|css|png|jpg|jpeg|gif|swf|xml|txt)$ {
               expires 14d;
       }

}

這是日誌文件http://pastebin.com/br0u36xp

2015/11/15 23:51:06 [info] 16075#0: *1 recv() failed (104: Connection reset by peer) while sending to client, client: 24.64.69.223, server: canmore.link, request: "$
2015/11/15 23:51:07 [crit] 16075#0: *3 open() "/var/lib/nginx/fastcgi/1/00/0000000001" failed (13: Permission denied) while reading upstream, client: 24.64.69.223, $
2015/11/16 00:24:09 [info] 16228#0: *68 recv() failed (104: Connection reset by peer) while sending to client, client: 24.64.69.223, server: canmore.link, request: $
2015/11/16 00:24:10 [crit] 16228#0: *70 open() "/var/lib/nginx/fastcgi/1/00/0000000001" failed (13: Permission denied) while reading upstream, client: 24.64.69.223,$
2015/11/16 00:31:46 [info] 16262#0: *1 recv() failed (104: Connection reset by peer) while sending to client, client: 24.64.69.223, server: canmore.link, request: "$
2015/11/16 00:31:53 [crit] 16262#0: *5 open() "/var/lib/nginx/fastcgi/1/00/0000000001" failed (13: Permission denied) while reading upstream, client: 24.64.69.223, $
2015/11/16 00:38:24 [error] 16318#0: *4 open() "/www/sites/canmore.link/html/favicon.ico" failed (2: No such file or directory), client: 24.64.69.223, server: canmo$
2015/11/16 00:38:24 [info] 16318#0: *1 client prematurely closed connection, so upstream connection is closed too while sending request to upstream, client: 24.64.6$
2015/11/16 00:38:51 [info] 16318#0: *17 client 24.64.69.223 closed keepalive connection (104: Connection reset by peer)
2015/11/16 00:38:52 [crit] 16318#0: *14 open() "/var/lib/nginx/fastcgi/3/00/0000000003" failed (13: Permission denied) while reading upstream, client: 24.64.69.223,$
2015/11/16 00:38:55 [crit] 16318#0: *13 open() "/var/lib/nginx/fastcgi/4/00/0000000004" failed (13: Permission denied) while reading upstream, client: 24.64.69.223,$
2015/11/16 00:39:23 [info] 16360#0: *1 recv() failed (104: Connection reset by peer) while sending to client, client: 24.64.69.223, server: canmore.link, request: "$
2015/11/16 00:39:23 [crit] 16360#0: *3 open() "/var/lib/nginx/fastcgi/1/00/0000000001" failed (13: Permission denied) while reading upstream, client: 24.64.69.223, $
2015/11/16 00:40:33 [info] 16388#0: *44 recv() failed (104: Connection reset by peer) while sending to client, client: 24.64.69.223, server: canmore.link, request: $
2015/11/16 00:40:34 [crit] 16388#0: *46 open() "/var/lib/nginx/fastcgi/1/00/0000000001" failed (13: Permission denied) while reading upstream, client: 24.64.69.223,$
2015/11/16 00:40:44 [crit] 16388#0: *48 open() "/var/lib/nginx/fastcgi/2/00/0000000002" failed (13: Permission denied) while reading upstream, client: 24.64.69.223,$

基於日誌文件解決的問題…

我更改了 /var/lib/nginx/fastcgi 文件夾的權限並且它起作用了。

chown -R www-data /var/lib/nginx/fastcgi

引用自:https://serverfault.com/questions/736588