You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ARM环境下lighttpd 1.4.55中AJAX请求返回CGI脚本内容而非执行结果的问题求助

ARM环境下lighttpd 1.4.55中AJAX请求返回CGI脚本内容而非执行结果的问题求助

我在ARM环境下使用lighttpd 1.4.55,做了一个带下载按钮的HTML页面:点击按钮会触发表单提交,调用一个CGI脚本,这个脚本需要接收表单输出并写入文件。但现在点击按钮后,XHR请求的响应文本居然是CGI脚本的内容,而不是脚本里printf输出的消息,而且CGI已经设置了执行权限。

我的目录结构

/mnt/userfs/lighttpd/
├── www
│   ├── scripts_files
│   │   └── json.cgi
│   ├── html_files
│   │   ├── css folder
│   │   ├── js folder
│   │   ├── upload_dir
│   │   └── index.html
│   ├── admin
│   │   ├── password_file
│   │   ├── main.html
│   │   └── file_upload.html
│   └── user
│       ├── password_file
│       ├── main.html
│       └── file_upload.html
├── lighttpd.conf
└── log
    └── error.log

触发请求的按钮代码

这个按钮会先调用JS函数,再下载CGI生成的文件:

<a href="/scripts_files/conf.json" download="data.json">
  <input type="button" value="DOWNLOAD" onclick="submit_form();">
</a>

AJAX请求函数

function submit_form()
{
  var div1 = document.getElementById("extern");
  var data = {};
  data = recursive_f(div1, 0, 0);
  output = JSON.stringify(data);
  var xhr_lv = new XMLHttpRequest();
  xhr_lv.onreadystatechange=function(){}
  xhr_lv.open("POST", "/scripts_files/json.cgi", true);
  xhr_lv.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
  xhr_lv.send(output);
}

生成CGI的C程序代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char* argv[])
{
  char* post_len_v = getenv("CONTENT_LENGTH");
  long post_len = strtol(post_len_v, NULL, 10);
  char* post_msg = (char*)malloc(post_len + 1);
  FILE *fp;

  if (!post_msg)
  {
    return 0;
  }

  fgets(post_msg, post_len + 1, stdin);
  fp = fopen("/mnt/userfs/lighttpd/www/scripts_files/conf.json", "w");
  fprintf(fp, "%s", post_msg);
  fclose(fp);

  printf("Content-type: text/html\n\n");
  printf("{'ret': 'OK'}");
  return 0;
}

lighttpd配置文件内容

server.modules              = (
    "mod_indexfile",
    "mod_access",
    "mod_redirect",
    "mod_alias",
    "mod_compress",
    "mod_dirlisting",
    "mod_staticfile",
    "mod_auth",
    "mod_authn_file",
    "mod_accesslog",
    "mod_cgi",
    #"mod_rewrite",
    #"mod_status"
    #"mod_fastcgi"
)

server.document-root        = "/mnt/userfs/lighttpd/www"
server.errorlog             = "/mnt/userfs/lighttpd/log/error.log"
server.breakagelog         = "/mnt/userfs/lighttpd/log/breakage.log"

index-file.names            = ("index.html", "main.html", "file_upload.html")

mimetype.assign = (
    ".class" => "application/java-vm",
    ".js" => "application/javascript",
    ".mjs" => "application/javascript",
    ".json" => "application/json",
    ".jsonld" => "application/ld+json",
    ".wmx" => "video/x-ms-wmx",
    ".wvx" => "video/x-ms-wvx",
    ".avi" => "video/x-msvideo",
    ".movie" => "video/x-sgi-movie",
    ".ice" => "x-conference/x-cooltalk",
    ".sisx" => "x-epoc/x-sisx-app",
    ".vrm" => "x-world/x-vrml",
    "README" => "text/plain; charset=utf-8",
    "Makefile" => "text/x-makefile; charset=utf-8",
    # enable caching for unknown mime types:
    #"" => "application/octet-stream"
)

mimetype.use-xattr        = "disable"

url.access-deny             = ( "~", ".inc" )

static-file.exclude-extensions = ( ".php", ".pl", ".fcgi" )

server.port                = 80
server.username            = "midac"
server.groupname           = "midac"

#compress.cache-dir          = "/var/cache/lighttpd/compress/"
compress.filetype           = ( "application/javascript", "text/css", "text/html", "text/plain" )

cgi.assign = ( ".cgi" => "" )

$HTTP["url"] =~ "/admin" {
    auth.backend = "htpasswd"
    auth.backend.htpasswd.userfile = "/mnt/userfs/lighttpd/www/admin/.htpasswd"
    auth.require = ( "/admin" => (
        "method" => "basic",
        "realm" => "main",
        "require" => "valid-user")
    )
}

$HTTP["url"] =~ "/user" {
    auth.backend = "htpasswd"
    auth.backend.htpasswd.userfile = "/mnt/userfs/lighttpd/www/user/.htpasswd"
    auth.require = ( "/user" => (
        "method" => "basic",
        "realm" => "main",
        "require" => "valid-user")
    )
}

$HTTP["url"] =~ "/user2" {
    auth.backend = "htpasswd"
    auth.backend.htpasswd.userfile = "/mnt/userfs/lighttpd/www/user2/.htpasswd"
    auth.require = ( "/user2" => (
        "method" => "basic",
        "realm" => "main",
        "require" => "valid-user")
    )
}

尝试过的其他测试

我还试了一个简单的shell脚本CGI,结果也是直接返回脚本内容:

#!/bin/sh
echo hello

而且POST请求的类型显示是octet-stream,看起来mod_cgi好像没正常工作,或者我在配置里漏了什么?有没有大佬能给点解决建议?

备注:内容来源于stack exchange,提问作者marco

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.23 09:59:07