在apache2中动态设置upload_tmp_dir [英] dynamically set upload_tmp_dir in apache2

查看:94
本文介绍了在apache2中动态设置upload_tmp_dir的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了几个小时研究它,使我发疯!

I am working on this for hours and its driving me nuts!

所以我认为这是一个非常棘手的问题,因此需要详细的问题。

so i think its a really tough one hence the detailed question.

问题是:


  • 我有一台服务器,可以接受通过php脚本上传的文件

  • I have a server that accepts file post uploads by php script.

数据将分发到多张光盘中。

The data will be distributed to several discs.

我要保存

原因:


  • 文件以后需要移动,我不希望它花很长时间(从系统tmp目录复制到目标磁盘)

  • The file needs to be moved later, and i don't want it to take long (copy from the systems tmp directory to target disk)

系统驱动器是一个ssd,当在其中写入和删除大量数据时,它不会非常高兴。

The system drive is an ssd that won't be very happy when so much data is written and deleted on it.

此服务器配置更改了初始值。在上传过程中,可以看到临时文件并查看其文件大小如何增加:

This server config changes the init value. during an upload an can see the temp file and see how its filesize increases:

Alias /thundercloud /home/thundercloud
<Directory /home/thundercloud>
    php_admin_value upload_tmp_dir /home/thundercloud/locations/42/tmp
</Directory>

我的便笺位于 / home / thundercloud 中,其中有几个符号链接 / home / thundercloud / locations (命名为1-n)都指向相应的挂载点。

My sripts are in /home/thundercloud and there are several symlinks in /home/thundercloud/locations (named numerically 1-n) that all point to the according mountpoints.

现在我需要的是获取数字42(这是42号磁盘-服务器中没有14个磁盘,只供测试用)是动态的。

Now what i need is to get the number 42 (which is disk no. 42 - no there are only 14 disks in the server, dis his just for test) to be dynamic.

我有很多东西控制请求,因此我基本上不在乎请求是否由主机名,获取变量,子目录等决定。

I have a lot of control over the request so I basically don't care whether its determined by hostname, get variable, subdirectory etc.

所以我的第一次尝试是:

So my first attempt was this:

RewriteLock /var/lock/apache2/rewrite.lock
Alias /thundercloud /home/thundercloud
RewriteMap getuplaodlocation prg:/home/thundercloud/uploadloc.php
RewriteEngine On
<Directory /home/thundercloud>
    AllowOverride All
    php_admin_value upload_tmp_dir ${getuplaodlocation:%{THE_REQUEST}}
</Directory>

相应的重写映射为:

#!/usr/bin/php
<?
set_time_limit(0); # forever program!
$keyboard = fopen("php://stdin","r");
while (1) {
    $line = trim(fgets($keyboard)); // dont care for now
    echo '/home/thundercloud/locations/42/tmp'.PHP_EOL;
}

我将硬编码设置为42。如果可以,我将解析出正确的错误信息 THE_REQUEST 。那没起效。脚本是chmod 777等。

I jut hardcoded 42. if it worked i would have parsed the correct falue from THE_REQUEST . It did not work. the script was chmod 777 and everything.

我的下一个尝试是:

Alias /thundercloud /home/thundercloud
<Directory /home/thundercloud>
    SetEnvIfNoCase Host lionel\.2x\.to upload=/home/thundercloud/locations/42/tmp
    php_admin_value upload_tmp_dir upload
</Directory>

没有运气。

所以我的决赛尝试是:

Alias /thundercloud /home/thundercloud
<Directory /home/thundercloud>
    php_admin_value upload_tmp_dir /home/thundercloud/locations/%{REMOTE_HOST}/tmp
</Directory>

也没有运气。我打印了ini_get_all()并得到以下输出:

no luck either. i printed ini_get_all() and got the following output:

["upload_tmp_dir"]=>
  array(3) {
    ["global_value"]=>
    string(47) "/home/thundercloud/locations/%{REMOTE_HOST}/tmp"

所以很明显,这个常量在此范围内不可用

so obviously this "constant" isn't available like this in this scope

现在我没了主意

我必须管理我的方法确实是反复试验的。

i must admin my apporaches are really trial and error.

任何人都可以指出正确的方向,或者告诉我这不可能并证明这一点吗?

anyony can point me in the right direction or tell me its not possible and justify that?

ps:它显然在php运行时不起作用;)

ps: it obviously doesnt work at php runtime ;)

edit:

好几个小时后,马克(Marc BI)提出了一个有用的评论,提出了这个解决方案:

Ok after some more hours and a useful comment from Marc B I came up with this "solution":

<VirtualHost *:80>
    ServerName 42.thundercloud.lionel.2x.to
    DocumentRoot /home/thundercloud
    php_admin_value upload_tmp_dir /home/thundercloud/locations/42/tmp
</VirtualHost>
<VirtualHost *:80>
    ServerName 43.thundercloud.lionel.2x.to
    DocumentRoot /home/thundercloud
    php_admin_value upload_tmp_dir /home/thundercloud/locations/43/tmp
</VirtualHost>

只是蛮力为每个磁盘使用一个自己的VirtualHost容器...这很丑陋,我希望避免这种情况步骤并具有通用的Web服务器配置,但似乎唯一可行的方法-至少可以正常工作。

Just brute force an own VirtualHost container for every disk... its ugly and I hoped to avoid that step and have a generic webserver config, but it seems like the only way to go - atleast it works.

我仍然想知道是否还有更好的方法,我认为它非常深入了解Web服务器配置很有趣。 Apache非常灵活,当您将其推向极限时(至少我认为这是非常有趣的),它变得非常有趣。

I still wonder whether theres a nicer way and i think its very interesting to go so deep into webserver configurations. Apache is incredibly flexible and its getting really interesting when you push it to the limits (thats at least what i think).

这就是为什么我将这个问题留待讨论

Thats why I will leave this question open and put a bounty on it.

编辑:

经过更多研究后,我认为可能会有解决方案此处的技巧:
http://httpd.apache.org/docs/ 2.0 / vhosts / mass.html

After some more research I think there might be a solution applying the techniques here: http://httpd.apache.org/docs/2.0/vhosts/mass.html

类似的东西:

<VirtualHost *> 
Use CanonicalName off
VirtualDocumentRoot /home/thundercloud/locations/%0/tmp/
php_admin_value open_basedir VIRTUAL_DOCUMENT_ROOT
<VirtualHost>

这只是一个想法,尚未经过测试。仍在设法弄清楚。在这种情况下,路径不正确,但是可能存在符号链接的解决方法。

This is just an idea, not tested yet. still trying to figure it out. The paths are not correct in this case but there might be a workaround with symlinks. Would be nice if some1 could point me in the right direction.

推荐答案

没有办法在<$ c $中使用变量替换c> php_admin_value 设置。这仅仅是因为apache模块只能解析它们自己的config指令。 mod_rewrite支持使用环境变量,因为这样做是内置在mod_rewrite本身中的。

There's no way to use variable substitution in php_admin_value settings. This is just because apache modules can only parse their own config directives. mod_rewrite supports using environment variables because doing so is built into mod_rewrite itself.

由于不能使用变量,因此必须使用< Location>之类的东西来设置所有可能的值。。例如:

Because you can't use variables, you'll have to set up all of the possible values using something like <Location>'s. For example:

RewriteEngine on
RewriteRule /upload(\d+)$       /upload.php

php_admin_value upload_tmp_dir /tmp

<Location /upload1>
    php_admin_value upload_tmp_dir /tmp/1
</Location>
<Location /upload2>
    php_admin_value upload_tmp_dir /tmp/2
</Location>
....
<Location /upload100>
    php_admin_value upload_tmp_dir /tmp/100
</Location>

然后您将其发布到 / upload {$ id} 。显然,它不如可能进行替换时动态地好。有两种明显的方法可以使其更整洁:

Then you just post to /upload{$id}. This is obviously not as nicely dynamic as it would be if substitution were possible. There are 2 obvious alternatives to make it neater:

1)有一个单独的生成脚本,该脚本生成一个文件以放入 / etc / httpd / conf.d 以及每台服务器的相关行

1) Have a separate build script which generates a file to put in /etc/httpd/conf.d with the relevant lines for each server

2)创建一个通用配置文件,其中仅包含例如100个位置规则。然后,在每台服务器上,您只需要设置所需的符号链接,并让生成表格的PHP决定将上传指向哪个卷-您最终会得到一堆 / uploadX 不能在每台服务器上使用的URL,但是无论如何都不会在那发布。

2) Create a universal config file which simply has e.g. the 100 location rules. On each server, you'd then just need to set up the symlinks you wanted and have the PHP which generates the form decide which volume to point the upload at - you'd end up with a bunch of /uploadX urls that wouldn't work on each server, but nothing would post there anyway.

另一种方法是使用mod_macro,这基本上可以您可以执行与上述相同的操作,但是使用较小的配置文件,自动生成扩展的配置,但本质上仍只是生成带有大量虚拟主机/位置块的大型配置文件。要按照您的原始要求很好地完成此操作,您需要修改mod_php。

The alternative is to use mod_macro, which basically lets you do the same as above but with a smaller config file, generating the expanded config automatically, but still essentially just generating a big config file with lots of vhost/location blocks. To do it as nicely as you original asked, you've need to modify mod_php.

这篇关于在apache2中动态设置upload_tmp_dir的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆