Debugging 500 Internal Server Error when dispatch.fcgi run?


#1

I’m trying to get my Ruby on Rails application to work under Apache. It
works fine when run as WEBrick (“ruby script/server”).

As suggested in
http://www.ocssolutions.com/support/ruby/troubleshooting-ruby-on-rails.php
, I tried running dispatch.fcgi on the command line. When I run it, no
matter where I run it, it outputs:

500 Internal Server Error

Both the #! (shebang) line and the config/database.yml file are correct.

I tried comparing the output of “ruby --debug dispatch.fcgi” and “ruby
–debug script/server”. They look extremely similar except one says
“500 Internal Server Error” and the other says “Rails application
started on http://0.0.0.0:3000”. Since I’m using Linux (Fedora 3,
actually), I also tried “strace ruby --debug” and nothing unusual
happens by the time the “500 Internal Server Error” message is emitting;
it doesn’t even look like it’s trying to connect to the database.

Is there a way to get a more detailed trace of what dispatch.fcgi is
doing? Is there a better way to debug this problem?

Thanks.


#2

Robert Br wrote:

Both the #! (shebang) line and the config/database.yml file are correct.
My app is working, but I also get that error. I think you always get
that error when runing the dispatch.fcgi from the command line, but if
you get errors that libraries are not found, then you have a different
problem.

Is there a way to get a more detailed trace of what dispatch.fcgi is
doing? Is there a better way to debug this problem?

I have also wished for this.

As others have said, try deleting your /tmp/ruby_sess* files, and
restarting the httpd.

Which fcgi method are you using, and what do your relevant config files
and your dispatch.fcgi look like?


#3

Jim N. wrote:

Robert Br wrote:

Both the #! (shebang) line and the config/database.yml file are correct.
My app is working, but I also get that error. I think you always get
that error when runing the dispatch.fcgi from the command line, but if
you get errors that libraries are not found, then you have a different
problem.

I discovered this as well. But, I tried running it on the command line
again, setting environment variables as described in
http://docs.rinet.ru:8080/CP7/ch13.htm . My reply is continued below.

Is there a way to get a more detailed trace of what dispatch.fcgi is
doing? Is there a better way to debug this problem?

I have also wished for this.

As others have said, try deleting your /tmp/ruby_sess* files, and
restarting the httpd.

Which fcgi method are you using, and what do your relevant config files
and your dispatch.fcgi look like?

Below are the configuration I added to httpd.conf (“rails.conf”),
fastcgi.conf (in the /etc/httpd/conf.d directory), dispatch.fcgi, and
.htaccess . Also below is the output from running dispatch.fcgi on
command line after setting:

REQUEST_METHOD=GET
QUERY_STRING=’/rails/performers/list?keywords=rolling+stones’

I’m not sure if these environment variables are accurate or not – I
don’t know what actually is passed to dispatch.fcgi via the .htaccess
file (via “dispatch.fcgi?$1”). I also tried running dispatch.cgi and
the results are identical.

Thanks for any further help.

----------------------------------rails.conf----------------------------------
#Alias /rails/ “/usr/lib/ruby/gems/1.8/gems/rails-1.0.0/html/”
Alias /rails/ “/usr/lib/ruby/gems/1.8/gems/rails-1.0.0/public/”
<Directory “/usr/lib/ruby/gems/1.8/gems/rails-1.0.0/public/”>
Options ExecCGI FollowSymLinks
AllowOverride all
Allow from all
Order allow,deny

---------------------------------fastcgi.conf---------------------------------
LoadModule fastcgi_module modules/mod_fastcgi.so

FastCgiIpcDir /tmp/fcgi_ipc/
AddHandler fastcgi-script .fcgi

--------------------------------dispatch.fcgi---------------------------------
#!/usr/bin/ruby
require File.dirname(FILE) + “/…/config/environment”
require ‘fcgi_handler’

RailsFCGIHandler.process!

--------------------------------.htaccess-------------------------------------

General Apache options

AddHandler fastcgi-script .fcgi
AddHandler cgi-script .cgi
Options +FollowSymLinks +ExecCGI

If you don’t want Rails to look in certain directories,

use the following rewrite rules so that Apache won’t rewrite certain

requests

Example:

RewriteCond %{REQUEST_URI} ^/notrails.*

RewriteRule .* - [L]

Redirect all requests not available on the filesystem to Rails

By default the cgi dispatcher is used which is very slow

For better performance replace the dispatcher with the fastcgi one

Example:

RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]

RewriteEngine On

If your Rails application is accessed via an Alias directive,

then you MUST also set the RewriteBase in this htaccess file.

Example:

Alias /performers
/usr/lib/ruby/gems/1.8/gems/rails-1.0.0/app/views/performers
RewriteBase /performers

RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.rhtml [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /dispatch.fcgi?$1 [QSA,L]

In case Rails experiences terminal errors

Instead of displaying this message you can supply a file here which

will be rendered instead

Example:

ErrorDocument 500 /500.html

ErrorDocument 500 “

Application error

Rails application failed to
start properly”

-------------------------------dispatch.fcgi.out-------------------------------
Content-Type: text/html
Set-Cookie: _session_id=ee5040fdcaaa7ea5fb8ac35b22c2fb65; path=/
Status: 500 Internal Error
Cache-Control: no-cache

Action Controller: Exception caught body { background-color: #fff; color: #333; }
body, p, ol, ul, td {
  font-family: verdana, arial, helvetica, sans-serif;
  font-size:   13px;
  line-height: 18px;
}

pre {
  background-color: #eee;
  padding: 10px;
  font-size: 11px;
}

a { color: #000; }
a:visited { color: #666; }
a:hover { color: #fff; background-color:#000; }

NoMethodError in <controller not set>#<action not set>

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.<<

RAILS_ROOT: public/../config/..

<a href="#" 

onclick=“document.getElementById(‘Framework-Trace’).style.display=‘none’;document.getElementById(‘Full-Trace’).style.display=‘none’;document.getElementById(‘Application-Trace’).style.display=‘block’;;
return false;”>Application Trace |

<a href="#" 

onclick=“document.getElementById(‘Application-Trace’).style.display=‘none’;document.getElementById(‘Full-Trace’).style.display=‘none’;document.getElementById(‘Framework-Trace’).style.display=‘block’;;
return false;”>Framework Trace |

<a href="#" 

onclick=“document.getElementById(‘Application-Trace’).style.display=‘none’;document.getElementById(‘Framework-Trace’).style.display=‘none’;document.getElementById(‘Full-Trace’).style.display=‘block’;;
return false;”>Full Trace

  <div id="Application-Trace" style="display: block;">
  <pre><code>/usr/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/request.rb:143:in 

request_uri' /usr/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/request.rb:161:inpath’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/routing.rb:453:in
recognize!' ./public/../config/../lib/dispatcher.rb:38:indispatch’
./public/…/config/…/lib/fcgi_handler.rb:141:in process_request' ./public/../config/../lib/fcgi_handler.rb:53:inprocess!’
./public/…/config/…/lib/fcgi_handler.rb:52:in each_cgi' ./public/../config/../lib/fcgi_handler.rb:52:inprocess!’
./public/…/config/…/lib/fcgi_handler.rb:22:in process!'</code></pre> </div> <div id="Framework-Trace" style="display: none;"> <pre><code>/usr/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/request.rb:143:inrequest_uri’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/request.rb:161:in
path' /usr/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/routing.rb:453:inrecognize!’
./public/…/config/…/lib/dispatcher.rb:38:in dispatch' ./public/dispatch.fcgi:24</code></pre> </div> <div id="Full-Trace" style="display: none;"> <pre><code>/usr/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/request.rb:143:inrequest_uri’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/request.rb:161:in
path' /usr/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/routing.rb:453:inrecognize!’
./public/…/config/…/lib/dispatcher.rb:38:in dispatch' ./public/../config/../lib/fcgi_handler.rb:141:inprocess_request’
./public/…/config/…/lib/fcgi_handler.rb:53:in process!' ./public/../config/../lib/fcgi_handler.rb:52:ineach_cgi’
./public/…/config/…/lib/fcgi_handler.rb:52:in process!' ./public/../config/../lib/fcgi_handler.rb:22:inprocess!’
./public/dispatch.fcgi:24

Request

Parameters: {"/rails/performers/list?keywords"=>"rolling stones"}

Show session dump

--- 
{}

Response

Headers: {"cookie"=>[], "Cache-Control"=>"no-cache"}
-------------------------------------------------------------------------------

#4

Robert Br wrote:

I’m trying to get my Ruby on Rails application to work under Apache. It
works fine when run as WEBrick (“ruby script/server”).

As suggested in
http://www.ocssolutions.com/support/ruby/troubleshooting-ruby-on-rails.php ,
I tried running dispatch.fcgi on the command line. When I run it, no
matter where I run it, it outputs:

500 Internal Server Error

You’ll find the answer in your Apache log file. On TextDrive, I had to
turn
on logging to get this, but after that, it was all clear.


#5

Robert,
The error message you are getting is from your rails app, suggesting
that it is being called. I would look for an error in your appname/log
directory, in the logfile for whatever mode you are running in.

Also, this may not be necessary, but you could try specifying
FastCgiServer in your httpd.conf or fcgi.conf (this can not go in a
virtual host directive):
FastCgiServer “/path/to/app/public/dispatch.fcgi” -idle-timeout 20
-processes 1
FastCgiConfig -maxClassProcesses 2 -maxProcesses 2 -minProcesses 1
-processSlack 1

see:
http://www.fastcgi.com/mod_fastcgi/docs/mod_fastcgi.html#FastCgiServer

Also, when you run it under Apache, is there anything in the httpd log
that suggests that the request is not coming in as you expect?
- Jim


#6

The problem was .htaccess. I turned on “LogLevel debug” in the
httpd.conf file, but it wasn’t particularly useful, except for point #1
below. Here’s what I did.

  1. According to the log file, “Alias” doesn’t work in .htaccess. Don’t
    know why the sample .htaccess deceptively mentions this.
  2. Added
    RewriteBase /rails/
    to .htaccess. This seems necessary because otherwise the accessed
    path is the full /usr/lib/ruby/… path, which is not a valid URL.
  3. Did two things:
    a) Created a symlink “app” in public directory which points to the
    actually Ruby on Rails ruby scripts.
    b) In .htaccess, changed
    RewriteRule ^(.)$ dispatch.fcgi?$1 [QSA,L}
    to
    RewriteRule ^(.
    )$ dispatch.fcgi?app/views/$1 [QSA,L]
  4. Left this in .htaccess
    RewriteRule ^([^.]+)$ $1.rhtml [QSA,L]