摘要
我试图映射一些文件扩展名,由nph (非解析头) CGI可执行文件执行。
假设我想访问一个URL http://server/file.ext并将"ext“文件扩展名映射到”触发“我的nph的执行(/var/www/ CGI /nph-test.sh)。
Apache配置
要做到这一点,我使用的是mod_actions和mod_cgid,这是我的相关配置信息:
<VirtualHost *:80>
DocumentRoot /var/www/htdocs
ScriptAlias /cgi-bin /var/www/cgi-bin
Action cgi-wrapper /cgi-bin/nph-test.sh
AddHandler cgi-wrapper .ext
</VirtualHost>NPH CGI脚本
这是我的nph-test.sh . is shell:
#!/bin/sh
echo "HTTP/1.0 200 OK"
echo "Server: Shell/1.0"
echo "Status: 200 \"OK\""
echo "Connection: close"
echo "Content-type: text/html"
echo ""
echo "<html><body><pre>"
echo "QUERY_STRING = "${QUERY_STRING}
echo "PATH_TRANSLATED = "${PATH_TRANSLATED}
echo "</pre></body></html>"问题
当我使用这个URL http://localhost/cgi-bin/nph-test.sh?Hello访问nph-test.sh时,我得到:
HTTP/1.0 200 OK
Server: Shell/1.0
Status: 200 "OK"
Connection: close
Content-type: text/html
<html><body><pre>
QUERY_STRING = Hello
PATH_TRANSLATED =
</pre></body></html>当使用像这个URL http://localhost/file.ext中的文件映射时出现问题,我得到一个双 http头(它将nph脚本处理为一个非 nph脚本):
HTTP/1.0 200 OK
Server: Shell/1.0
Status: 200 "OK"
Connection: close
Content-type: text/html
<html><body><pre>
QUERY_STRING =
PATH_TRANSLATED = /var/www/htdocs/file.ext
</pre></body></html>
HTTP/1.1 200 OK^M
Date: Tue, 18 Mar 2014 14:32:29 GMT^M
Server: Apache/2.4.6 (Ubuntu)^M
Content-Length: 0^M
Keep-Alive: timeout=5, max=100^M
Connection: Keep-Alive^M
Content-Type: text/x-sh^M
^M我的发现
搜索了几个小时后,我只找到了这个2003 apache bug报告。这也意味着一个理论上还没有被应用的补丁。
但是在搜索当前的cgid源代码时,我可以看到代码已经更改为能够捕获"nph-“cgi文件前缀(strrchr):
1372 if ((argv0 = strrchr(r->filename, '/')) != NULL) {
1373 argv0++;
1374 }
1375 else {
1376 argv0 = r->filename;
1377 }
1378
1379 nph = !(strncmp(argv0, "nph-", 4));问题
是否有一种使用apache2将文件扩展名映射到nph的方法?(不返回两个http头)
还有其他方法可以做到这一点或者其他的解决办法吗?
Apache和OS服务器信息
我在Ubuntu13.10中使用默认的apache2服务器:
# apachectl -V
Server version: Apache/2.4.6 (Ubuntu)
Server built: Dec 5 2013 18:32:22
Server's Module Magic Number: 20120211:23
Server loaded: APR 1.4.8, APR-UTIL 1.5.2
Compiled using: APR 1.4.8, APR-UTIL 1.5.2
Architecture: 64-bit
Server MPM: event
threaded: yes (fixed thread count)
forked: yes (variable process count)
Server compiled with....
-D APR_HAS_SENDFILE
-D APR_HAS_MMAP
-D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
-D APR_USE_SYSVSEM_SERIALIZE
-D APR_USE_PTHREAD_SERIALIZE
-D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
-D APR_HAS_OTHER_CHILD
-D AP_HAVE_RELIABLE_PIPED_LOGS
-D DYNAMIC_MODULE_LIMIT=256
-D HTTPD_ROOT="/etc/apache2"
-D SUEXEC_BIN="/usr/lib/apache2/suexec"
-D DEFAULT_PIDLOG="/var/run/apache2.pid"
-D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
-D DEFAULT_ERRORLOG="logs/error_log"
-D AP_TYPES_CONFIG_FILE="mime.types"
-D SERVER_CONFIG_FILE="apache2.conf"更新:解决方案
使用mod_rewrite模块解决问题的更改配置:
<VirtualHost *:80>
DocumentRoot /var/www/htdocs
ScriptAlias /cgi-bin /var/www/cgi-bin
RewriteEngine on
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^/?(.*\.ext)$ /cgi-bin/nph-test.sh?$1 [PT]
RewriteRule ^/?(.*\.ext)$ /cgi-bin/nph-test.sh?$1&%{QUERY_STRING} [PT]
</VirtualHost>发布于 2014-06-12 08:57:01
我认为在2.4.x源代码上(直接)是不可能的。正如您正确指出的那样,错误目前在重新打开的状态中。
拟议的修补程序包括两项主要修改:
我无法进行测试,但我认为使用mod_rewrite解决这个bug是可能的:查看mod_rewrite文档似乎可以很好地使用nph
您可以在mod_rewrite中找到完整的nph- + 文件页示例。
https://stackoverflow.com/questions/22483281
复制相似问题