Nginx 调用上传模块时返回“404 Not Found”

Nginx 调用上传模块时返回“404 Not Found”

我已经设置了 Nginx 和上传模块,如下所示Martin Fjordvald 的指南,但每当我向其提交文件时,我都会收到 Nginx 的通用“404 Not Found”页面。该文件未上传。

这是我的 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/*;
}

conf.d 中的文件(包含在 nginx.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;

}

默认配置文件

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

主机名配置文件

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

这是我用来提交文件上传的代码(我已经尝试使用模块主页上找到的通用示例,但是它仍然不起作用):

[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]

以下是 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

有人能解释一下这种情况吗?这真的让我很困惑。

提前致谢!

答案1

好吧,在对配置进行了一些调整后,我终于让它工作了。结果发现这是我犯的一个愚蠢的错误(通常都是这样的):

在我的 Nginx 配置中,我有以下内容:

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

当我真正需要这个的时候:

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

得到的经验教训是:一定要检查你的重写。

相关内容