Search code examples
ruby-on-railsrubylinuxnginxpassenger

Configure nginx.conf for Rails Shows File Directory Not Index


Here is what I have in my nginx.conf:

#user  nobody;
worker_processes  1;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;
events {
    worker_connections  1024;
     }
http {
    passenger_root /usr/lib/ruby/gems/1.9.1/gems/passenger-3.0.11;
    passenger_ruby /usr/bin/ruby;
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;

    gzip  on;
    gzip_http_version 1.1;
    gzip_comp_level 2;
    gzip_types    text/plain text/html text/css
                  application/x-javascript text/xml
                  application/xml application/xml+rss
                  text/javascript;
    server {
        listen       80;
        server_name  domain.com www.domain.com;

        # autoindex on;
        passenger_enabled on;
        rails_env production;

        access_log  logs/mrfs.access.log;

        location / {
            root   /opt/nginx/html/mrfs/public; #line updated per suggestion below
        }

        }
  }

I can toggle the autoindex on and off and it will display a 403 error when it is commented out or a directory listing when it is on. When I have access to the directory listings, I can surf them and download various files. This seems to me to make it not a permissions issue. When I have auto index disabled I get a can not list directory error in the error log. I think what I need to do is tell nginx how to load my index.html.erb file? How do I do that? Is that what is wrong?

Update

I put an 'index.html' in my /opt/nginx/html/mrfs/public folder and it loads that. So what would cause rails/nginx/passenger to load index.html file in public, but not load the home.html.erb file in my routes? I can run 'rails s' and then do a wget http://localhost and it pulls down the right html file.


Solution

  • Here is what I had to change it to:

    server {
            listen       80;
            server_name  domain.com www.domain.com;
            rails_env production;
            passenger_use_global_queue on;
            access_log  logs/mrfs.access.log;
             root   /opt/nginx/html/mrfs/public;
            passenger_enabled on;
           error_page  404              /404.html;
           error_page   500 502 503 504  /50x.html;
        }
    
    }
    

    If you have a location / in your nginx.conf then you have to put passenger_enabled on; inside of that or else delete the location / block. I chose to delete it.