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

AWS EC2上Hunchentoot无法加载图片的问题求助

问题分析与解决方案

核心原因

你遇到的404问题本质是静态文件路径解析错误:handle-static-file使用的相对路径是基于Lisp进程的启动工作目录,而非server.lisp文件所在的目录。在EC2实例上启动服务器时,工作目录大概率和server.lisp所在目录不一致,导致无法定位到static/下的图片文件。

具体修复方案

修改server.lisp中两个图片handler的handle-static-file调用,用*load-truename*获取当前文件的绝对目录,再拼接静态文件路径,确保路径解析不受工作目录影响:

(define-easy-handler (lisp-handler :uri "/lispy") ()
  (setf (hunchentoot:content-type*) "image/png")
  (setf (header-out "Access-Control-Allow-Methods") "GET, OPTIONS")
  (setf (header-out "Access-Control-Allow-Headers") "Content-Type")
  (setf (header-out "Access-Control-Allow-Origin") "http://myhomepage.info, http://localhost:4243")
  ;; 替换为基于当前文件目录的绝对路径
  (handle-static-file (merge-pathnames #p"static/lisp.png" (directory-namestring *load-truename*))))

(define-easy-handler (sicp-handler :uri "/wizard") ()
  (setf (hunchentoot:content-type*) "image/jpg")
  (setf (header-out "Access-Control-Allow-Methods") "GET, OPTIONS")
  (setf (header-out "Access-Control-Allow-Headers") "Content-Type")
  (setf (header-out "Access-Control-Allow-Origin") "http://myhomepage.info, http://localhost:4243")
  ;; 替换为基于当前文件目录的绝对路径
  (handle-static-file (merge-pathnames #p"static/sicp.jpg" (directory-namestring *load-truename*))))

额外验证建议

  1. 重启Hunchentoot服务器,重新加载修改后的server.lisp
  2. 若问题仍存在,可在代码中添加调试语句,打印当前工作目录和解析后的文件路径:
    ;; 在任意handler中添加
    (format t "当前工作目录: ~a~%" (uiop:getcwd))
    (format t "解析后的图片路径: ~a~%" (merge-pathnames #p"static/lisp.png" (directory-namestring *load-truename*)))
    
    启动服务器后查看输出,确认路径是否正确指向EC2实例上的图片文件。

内容的提问来源于stack exchange,提问作者chez93

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 22:03:26