我最近把nginx放在了apache之前,作为一个反向代理。
到目前为止,Apache直接处理请求和file upload
现在,我需要configurationnginx,以便它发送file upload请求到Apache。
我有几个端点上传文件,我想看看是否有一个快速的方法来定义一个configuration选项在nginx的所有我的端点
现在下面只是指一个file upload条目
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个不同的
我是否需要为自己的端点创build一个新的条目,或者我可以将它们分组
谢谢
还没有testing过这个想法,但你应该能够设置一个命名的位置,并重写请求。 不过每个请求仍然需要一行
#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。
根据你的configuration的其余部分,你可能有更好的运气重写到一个目录
#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 "(.*)"; }
是的,有一种方法可以将所有端点分组为一个“别名”:
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/LoadBalanceExample
就是这样,它应该工作得很好!