在使用Nginx代理时,不会从Rails应用程序提供PDF

时间:2012-02-06 10:59:20

标签: ruby-on-rails nginx ruby-on-rails-2 unicorn wicked-pdf

我希望有人能够对此有所了解。我们有一个Rails 2应用程序,使用wicked_pdf库生成PDF。我们在nginx代理的独角兽下运行应用程序。

麻烦的是,每当应用程序尝试提供PDF时,它只会超时并且nginx报告404.我已经通过独角兽端口检查了应用程序并且它工作正常所以它是unicorn和nginx之间发生的事情导致了这个问题失败。

日志文件没有报告任何有用的内容。应用程序日志只会正常报告所有内容,但不会输出Sending data行。 nginx日志报告*443 upstream prematurely closed connection while reading response header from upstream, client

独角兽错误日志报告连接超时且工作人员被杀死,但PDF生成不需要30秒:

worker=2 PID:14099 timeout (31s > 30s), killing
reaped #<Process::Status: pid=14099,signaled(SIGKILL=9)> worker=2
worker=2 ready

我觉得我们错过了网站的nginx配置,但我不知道从哪里开始寻找。

我在下面包含了nginx配置:

server {
    listen *:80;
    server_name server_name.example.com;
    root /var/apps/application/current/public;
    try_files /system/maintenance.html $uri $uri/index.html @app;
    location @app {
        proxy_pass http://unix:/tmp/application.sock;
        proxy_set_header Host $http_host;
    }
    error_page 404 400 500 502 /error.html;
    location = /error.html {
        root /var/www;
    }
}

2 个答案:

答案 0 :(得分:0)

尝试在环境文件中设置:

ActionController::Streaming::X_SENDFILE_HEADER = 'X-Accel-Redirect' 

注意:这适用于Nginx,而非Apache。

答案 1 :(得分:0)

好的,经过几个小时的探索,我终于解决了这个问题。

当我将超时设置得足够大时,PDF最终会通过,这表明它在其他地方超时。最终我发现我正在访问PDF的布局文件中的request.env ['HTTP_HOST']以创建图像的绝对路径。这导致wkhtmltopdf尝试在错误的主机名上连接回服务器并且它失败了。

解决方案是将其换成绝对文件系统路径。