Nginx retornando “404 Not Found” ao invocar o módulo de upload

1

Eu configurei o Nginx e o módulo de upload seguindo o Guia de Martin Fjordvald , mas sempre que eu envio um arquivo para ele, recebo a página genérica "404 Not Found" do Nginx. O arquivo não está carregado.

Aqui está meu nginx.conf:

user              nginx nginx;
worker_processes  1;
worker_rlimit_nofile 20000;

pid               /var/run/nginx.pid;

events {
    worker_connections  2048;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    client_max_body_size 64M;
    sendfile        on;
    tcp_nopush      on;

    keepalive_timeout  3;

    gzip  on;
    gzip_comp_level 2;
    gzip_proxied any;
    gzip_types      text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    server_tokens off;

    # Allocate RAM for upload modules
    upload_progress uploads 5m;

    include /etc/nginx/conf.d/*;
}

Arquivos em conf.d (incluídos no nginx.conf):

bitload.biz.conf

server {
    server_name  bitload.biz;
    return       301 http://www.bitload.biz$request_uri;
}

server {
    server_name www.bitload.biz;
    root /var/www/bitload.biz;
    include php;
    access_log  /var/www/bitload.biz-access.log;
    error_log  /var/www/bitload.biz-error.log;
    index index.php index.html index.htm;

    # Drop requests to non-PHP requests
    location ~ \.(aspx|jsp|cgi)$ {
        return 404;
    }

        # Set up the upload handler
    # More info: http://blog.martinfjordvald.com/2010/08/file-uploading-with-php-and-nginx/
    location /upload {
      # Pass altered request body to this location
      upload_pass   @uploadhandler;

      # Store files to this directory
      # The directory is hashed, subdirectories 0 1 2 3 4 5 6 7 8 9 should exist
      upload_store /var/tmp/bitload_fuploads 1;

      # Allow uploaded files to be read only by user
      upload_store_access user:r group:r all:r;

      # Set specified fields in request body
      upload_set_form_field $upload_field_name.name "$upload_file_name";
      upload_set_form_field $upload_field_name.path "$upload_tmp_path";

      # Inform backend about hash and size of a file
      upload_aggregate_form_field "$upload_field_name.sha1" "$upload_file_sha1";
      upload_aggregate_form_field "$upload_field_name.size" "$upload_file_size";

      # This directive specifies any extra POST fields which should be passed along.
      #upload_pass_form_field "^fallback$|^login$|^usession$";

      upload_cleanup 400 404 499 500-505;

      track_uploads uploads 5s;
    }

    # Set URL to redirect to after a user's upload finishes
    location @uploadhandler {
      rewrite ^ /processes/upload.php last;
    }

        # Set up the upload progress handler
    # More info: http://blog.martinfjordvald.com/2010/08/file-uploading-with-php-and-nginx/
    location = /progress {
      report_uploads uploads;
    }

    # Nice Download URLs
    location / {
        rewrite ^/download/([\d]*)\-.*$ /download.php?file=$1 last;
    }

    # Redirect 404s to the homepage
    error_page 404 /index.php;

}

default.conf

server {
    listen 80 default;
    server_name _;
    root /var/www/html;
}

hostname.conf

server {
    server_name gigabyte.bitload.biz;
    root /var/www/html;
}

Aqui está o código que estou usando para enviar o upload do arquivo (tentei com o exemplo genérico encontrado na página inicial do módulo, no entanto, ele ainda não funcionou):

[snip]

  <form method="post" name="uploadForm" id="upload" action="/upload/" enctype="multipart/form-data">
    <div class="inputrow">


      <div class="rowElem">
        <label for="name">Choose Category:</label>
        <span class="inputField">



          <select name="file_category" id="file_category">
            <?php echo getCategoryOptions();?>
          </select>



        </span> </div>

      <div class="rowElem">

        <label for="contact_number">File Title:</label>
        <span class="inputField">
        <input name="file_title" id="file_title" class="required" type="text" title="Please enter a file title." />
        </span> </div>


      <div class="rowElem">
        <label for="payout_method_details">File Description:</label>
        <span class="inputField">
       <textarea name="file_description" rows="1" cols="1" class="tinymce" id="file_description" title="Please enter a file description." style="width:100px;"></textarea>
        </span> </div>


      <div class="rowElem">

        <label for="contact_number">File:</label>
        <span class="inputField">
            <input id="file_upload" name="file_upload" type="file" />
        </span> </div>

      <div class="rowElem">
        <label>&nbsp;</label>
        <input type="hidden" name="process" value="upload" />

        <input type="submit" name="submit_button" value="Upload" title="Upload" class="normal submitbutton" style="width:auto;" />
      </div>
    </div>
  </form>
</div>
<div id="uploadProgress">
    <div id="uploadProgressBar"></div>
    <span id="percent" style="font-weight:bold;"></span><br /><br />
    <strong>Uploaded:</strong> <span id="received"></span>&nbsp;&nbsp;&nbsp;<strong>Speed:</strong> <span id="speed"></span>
</div>
<script type="text/javascript">
    $(document).ready(function(){
            $("#upload").validate();

            [snip (TinyMCE init code)]

        <?php
            $row = $ado->fetch($ado->exec("SELECT max_upload_file from template"));
            if(!empty($row['max_upload_file'])){
                $file_size_mb = $row['max_upload_file']; // Get max file size
            } else {
                $file_size_mb = 10;
            }
            $file_size = $file_size_mb*1024*1024; // Convert max file size to bytes
        ?>

        // File Upload Handler (fired on form submit)
        // More information: http://blog.martinfjordvald.com/2010/08/file-uploading-with-php-and-nginx/
        $("#upload").submit(function(){
            var received = 0;
            var percent  = 0.0;
            var periodical;
            var uuid = Math.random().toString(36).substring(2); // Unique uploader ID
            var check = 20; // Milliseconds between each XHR request.
            var action = $("#upload").attr("action") + '?X-Progress-ID=' + uuid;

            //$("#upload").attr("action", action); // Assign ID to upload.

            $("#uploadProgress").dialog("open");
            periodical = setInterval(function(){
                $.ajax({
                   url: '/progress?X-Progress-ID=' + uuid, // Using same identifier!
                   dataType: 'json',
                   type: 'GET',
                   success: function(data) {
                        var json = JSON.decode(data);
                        if (json.state == 'uploading') {
                            var delta = json.received - received;
                            var bytes = delta / (check / 1000);
                            received = json.received;
                            percent = (json.received / json.size) * 100;

                            $("#uploadProgressBar").progressbar("option", "value", percent);
                            $('#percent').html = percent + '%';
                            $('#received').html = Math.round(json.received / 1024) + '/' + Math.round(json.size / 1024) + ' KB';
                            $('#speed').html = Math.round(bytes / 1024) + ' KB/s';

                            if (percent >= 100) {
                                clearInterval(periodical); // Upload done, stop polling Nginx.
                            }
                        }
                   }
                });
            }, check);
        });

        $("#uploadProgress").dialog({
            width: 500,
            modal: true,
            closeOnEscape: false,
            draggable: false,
            title: 'Uploading, please wait...',
            zIndex: 9999,
            resizable: false,
            open: function(event, ui) { $(".ui-dialog-titlebar-close").hide(); },
            autoOpen: false
        });

        $("#uploadProgressBar").progressbar();

    });
</script>

[snip]

Aqui está a saída do nginx -V:

nginx version: nginx/1.0.14
built by gcc 4.4.6 20110731 (Red Hat 4.4.6-3) (GCC)
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx/ --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-mail --with-mail_ssl_module --with-ipv6 --with-cc-opt='-O2 -g -march=i386 -mtune=i686' --add-module=../gnosek-nginx-upstream-fair-5f6a3b7 --add-module=../nginx_upload_module-2.2.0 --add-module=../masterzen-nginx-upload-progress-module-82b35fc

Alguém pode esclarecer essa situação? Isso realmente me deixou perplexo.

Obrigado antecipadamente!

    
por stevenmirabito 30.03.2012 / 03:56

1 resposta

2

Bom, depois de mexer na configuração, finalmente consegui que funcionasse. Acontece que foi um erro estúpido da minha parte (como geralmente é):

Na minha configuração do Nginx, eu tive isto:

# Set URL to redirect to after a user's upload finishes
location @uploadhandler {
  rewrite ^ /processes/upload.php last;
}

Quando eu realmente precisava disso:

# Set URL to redirect to after a user's upload finishes
location @uploadhandler {
  rewrite ^ /processes/upload/upload.php last;
}

Lição aprendida: verifique sempre suas reescritas.

    
por 30.03.2012 / 21:42