简体   繁体   English

将 yesod 应用程序作为(快速)CGI 服务

[英]Serving a yesod application as a (Fast)CGI

I'm trying to serve a yesod application as a CGI (or FastCGI) program but Apache shows me a 500 error:我正在尝试将 yesod 应用程序作为 CGI(或 FastCGI)程序提供服务,但 Apache 向我显示 500 错误:

[Mon Sep 21 17:35:41.425565 2020] [http:error] [pid 2758] [client 10.0.2.2:43872] AH02429: Response header name '21/Sep/2020' contains invalid characters, aborting request [Mon Sep 21 17:35:41.425565 2020] [http:error] [pid 2758] [client 10.0.2.2:43872] AH02429:响应 header 名称“21/Sep/2020”包含无效字符,中止请求

Starting with a new template with stack new project yesodweb/sqlite , I've tried to convert it to a CGI program by changing src/Application.hs .stack new project yesodweb/sqlite的新模板开始,我尝试通过更改src/Application.hs将其转换为 CGI 程序。

I've imported the Wai CGI library with我已经导入了 Wai CGI 库

import Network.Wai.Handler.CGI              (run)

and changed the last line of appMain to run app :并将appMain的最后一行更改为run app

-- | The @main@ function for an executable running this site.
appMain :: IO ()
appMain = do 
    -- Get the settings from all relevant sources
    settings <- loadYamlSettingsArgs
        -- fall back to compile-time values, set to [] to require values at runtime
        [configSettingsYmlValue]
 
        -- allow environment variables to override
        useEnv
 
    -- Generate the foundation from the settings
    foundation <- makeFoundation settings
 
    -- Generate a WAI Application from the foundation
    app <- makeApplication foundation
                                             
    -- Run the application with Warp
    --runSettings (warpSettings foundation) app
    run app

I thought this was the minimal change to serve it as a CGI program but apparently is not working.我认为这是将其用作 CGI 程序的最小更改,但显然不起作用。

I've already looked into yesod's book chapter on deploying your Webapp which has been handy to write the apropriate Apache configuration so the server could ran the app.我已经查看了 yesod 的书上关于部署 Web应用程序的章节,该章节可以方便地编写适当的 Apache 配置,以便服务器可以运行该应用程序。 It says nothing about the nedded changes to the app code which I presume is where the problem is.它没有说明我认为是问题所在的应用程序代码的必要更改。

I've also checked this stackoverflow question but it's from almost 8 years ago so it's outdated now.我还检查了这个stackoverflow 问题,但它是将近 8 年前的问题,所以现在已经过时了。

The problem is that the default makeFoundation logs to standard output, which is also where the CGI program is supposed to send its response, so you're getting intermingling of response headers with log output, and Apache tries to parse log lines as HTTP headers, etc.问题是默认的makeFoundation记录到标准 output,这也是 CGI 程序应该发送其响应的地方,因此您将响应标头与日志 output 混合在一起,而 Apache 试图将日志行解析为 HTTP 标头,等等

If you replace newStdoutLoggerSet with newStderrLoggerSet , it should work, and the log output will end up in Apache's "error.log" or equivalent.如果将newStdoutLoggerSet替换为newStderrLoggerSet ,它应该可以工作,并且日志 output 将以 Apache 的“error.log”或等效文件结束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM