nginx http上传模块配置

nginx http上传模块配置

我最近将 nginx 放在 apache 前面作为反向代理。

到目前为止,Apache 直接处理请求和文件上传

现在,我需要配置 nginx 以便它将文件上传请求发送到 apache。

我有几个用于上传文件的端点,我正在尝试查看是否有一种快速的方法可以在 nginx 中为所有端点定义一个配置选项

目前,下面仅指一个文件上传条目

location /banner_upload {
    proxy_pass  http://backend:8080/banner/save;
   }




 location /banner/save {
            # Pass altered request body to this location
            upload_pass /banner_upload;

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

        # Allow uploaded files to be read only by user
        #upload_store_access user: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}_content_type" $upload_content_type;
        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}_md5" $upload_file_md5;
        upload_aggregate_form_field "${upload_field_name}_size" $upload_file_size;

        upload_pass_form_field "(.*)";
    }

但上述条目仅适用于 1 个端点

我有 7 个不同的

我是否需要为每个端点创建一个新条目,或者有没有办法对它们进行分组

谢谢

答案1

还没有测试过这个想法,但你应该能够设置一个命名位置并将请求重写到该位置。不过,你仍然需要为每个请求写一行

#Add one of these for each location
rewrite ^/upload/location1$ @upload last;
rewrite ^/upload/location2$ @upload last;
rewrite ^/upload/location3$ @upload last;
rewrite ^/upload/location4$ @upload last;

location = @upload {
        # Pass altered request body to this location
        upload_pass /banner_upload;

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

        # Allow uploaded files to be read only by user
        #upload_store_access user: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}_content_type" $upload_content_type;
        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}_md5" $upload_file_md5;
        upload_aggregate_form_field "${upload_field_name}_size" $upload_file_size;
        
        upload_pass_form_field "(.*)";
}

这是粗略的想法,我可能有一些语法错误,所以如果您遇到问题,请查阅 wiki。

编辑

根据你的其他配置,你可能更愿意将数据重写到目录中

#Add one of these for each location
rewrite ^/upload/location1$ /upload/ last;
rewrite ^/upload/location2$ /upload/ last;
rewrite ^/upload/location3$ /upload/ last;
rewrite ^/upload/location4$ /upload/ last;

location /upload/ {
        # Pass altered request body to this location
        upload_pass /banner_upload;

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

        # Allow uploaded files to be read only by user
        #upload_store_access user: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}_content_type" $upload_content_type;
        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}_md5" $upload_file_md5;
        upload_aggregate_form_field "${upload_field_name}_size" $upload_file_size;
        
        upload_pass_form_field "(.*)";
}

答案2

是的,有一种方法可以将所有端点分组为一个“别名”:

  upstream backend_pool {
      server 10.0.0.1:8080;
      server 10.0.0.2:8080;
      server 10.0.0.3:8080;
  }

  location /banner_upload {
      proxy_pass  http://backend_pool/banner/save;
  }

更多详情请参见此处:

http://wiki.nginx.org/LoadBalance示例

就是这样,它应该可以正常工作!

相关内容