【问题标题】:Linux - client_body_in_file_only - how to set file permissions for the temp file?Linux - client_body_in_file_only - 如何为临时文件设置文件权限?
【发布时间】:2019-07-09 12:52:23
【问题描述】:

我们在 nginx 中使用 client_body_in_file_only 选项,以允许通过 Ajax 上传文件。配置如下所示:

location ~ ^(\/path1|\path2)$ {

  limit_except POST { deny all; }
  client_body_temp_path      /path/to/app/tmp;
  client_body_in_file_only   on;
  client_body_buffer_size    128K;
  client_max_body_size       1000M;

  #this option is a quick hack to make sure files get saved on (ie this type of request goes to) on a specific server
  proxy_pass                 http://admin;
  proxy_pass_request_headers on;
  proxy_set_header           X-FILE $request_body_file;
  proxy_set_body             off;
  proxy_redirect             off;

  # might not need?
  proxy_read_timeout         3m;
}

这可行,但处理请求的 Web 服务器进程 (Mongrel) 必须先 sudo headers['X-FILE'] 中的临时文件,然后才能对其执行任何操作。这是因为临时文件带有600 权限。

我对这种方法不满意,它要求我们编辑/etc/sudoers 文件以允许Web 服务器用户在没有密码的情况下执行sudo chmod。感觉很不安全。

有没有办法通过 nginx 配置更改创建的临时文件的权限,例如更改为 775?

编辑:我只是尝试在 nginx 初始化配置中更改 umask 选项的值,然后重新启动 nginx,但它没有帮助。原来是0022,我把它改成了0002。在这两种情况下,它都有 600 个权限。

EDIT2:我还尝试在 nginx 配置中的 proxy_redirect 行下添加此行。

proxy_store_access user:rw group:rw all:r;

但是,它没有任何区别 - 它仍然只有 user:rw

【问题讨论】:

    标签: file nginx temp


    【解决方案1】:

    查看nginx源,看来修改临时文件权限的唯一机制是请求的request_body_file_group_access属性,在ngx_http_write_request_body()中查阅:

    if (r->request_body_file_group_access) {
        tf->access = 0660;
    }
    

    但即便如此,您也只能使用 0660,而且它似乎不是用户可设置的属性,仅由 ngx_http_dav 模块使用。

    权限最终设置在ngx_open_tempfile(),默认为0600

    fd = open((const char *) name, O_CREAT|O_EXCL|O_RDWR, access ? access : 0600);
    

    所以目前似乎没有基于配置的解决方案。如果您愿意/能够从源代码构建nginx,一种可能性是应用一个简单的补丁来将权限设置为您想要在ngx_http_write_request_body() 中的任何内容:

    +    tf->access = 0644;
    +
         if (r->request_body_file_group_access) {
             tf->access = 0660;
         }
    
         rb->temp_file = tf;
    

    我对此进行了测试,得到了以下内容,第一个文件未经修改上传,第二个文件有修改:

    $ ls -al /tmp/upload/
    total 984
    drwxr-xr-x  2 nobody root     12288 Feb 18 13:42 .
    drwxrwxrwt 16 root   root     12288 Feb 18 14:24 ..
    -rw-------  1 nobody nogroup 490667 Feb 18 13:40 0000000001
    -rw-r--r--  1 nobody nogroup 490667 Feb 18 13:42 0063184684
    

    【讨论】:

    • 我将其标记为正确,因为似乎唯一的解决方案是重建 nginx。
    【解决方案2】:

    现在好像不能配置文件权限,但是有official feature request

    文件权限始终为 0600,使应用程序根本无法读取文件。 [...]这是目前不受支持的场景:[Nginx] 使用默认权限 [...] 0600 创建临时文件(除非设置了request_body_file_group_access - 但不幸的是,该属性不可设置)。

    该票于 2018 年 10 月以次要优先级开放。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-17
      • 2017-08-24
      • 2017-02-27
      • 2011-03-04
      • 2023-02-11
      • 2014-02-10
      • 2011-12-18
      • 2018-02-26
      相关资源
      最近更新 更多