2011-12-07 34 views
2

以下是我在我的nginx.conf:Rails的配置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 
     } 

     } 
    } 

我可以切換自動索引和關閉,並且當它是它會顯示一個403錯誤當它打開時被註釋掉或目錄列表。當我訪問目錄列表時,我可以瀏覽它們並下載各種文件。這在我看來使它不是一個權限問題。當我禁用了自動索引時,我在錯誤日誌中找不到目錄錯誤。我想我需要做的是告訴nginx如何加載我的index.html.erb文件?我怎麼做?那是什麼錯誤?

更新

我把一個 'index.html的' 在我的/ opt/nginx的/ HTML/MRFS /公用文件夾,它會加載。那麼會導致rails/nginx/passenger公開加載index.html文件,但不會在我的路由中加載home.html.erb文件?我可以運行'rails s',然後做一個wget http://localhost並且它會拉下正確的html文件。

回答

1

這裏是我不得不改變它什麼:

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; 
    } 

} 

如果你有你的nginx.conf那麼你必須把passenger_enabled上location /;在那裏面或者刪除location /塊。我選擇刪除它。

3

您需要的root設置爲public目錄中的項目:

server { 
    ... 
    passenger_enabled on; 
    rails_env production; 
    root /opt/nginx/html/mrfs/public; 
} 
+0

公衆是否有特殊含義?我意識到這樣做可能是常規做法,我會添加一個公共目錄並將其放在那裏。但是我所做的只是在github上的git pull我的rails回購。 –

+1

您的rails應用程序應該有一個名爲'public'的目錄,它是由'rails new'生成的。它基本上是所有rails應用程序的web根目錄。 – halfdan

+0

謝謝。我在想像public_html。 –

1

除了對方的回答的「公共」文件夾修正,這聽起來好像有一個與權限的問題/包含您應用的文件夾的所有權。應該有在nginx.conf像一條線......

user websrv; 

它定義了nginx的自身運行下的用戶。您的Rails應用程序的文件夾應該由該用戶擁有,並且應該適當地設置權限。

+0

我已將用戶線從我的配置中添加到上面的原始問題中。默認值是'nobody',但root是所有者。我可以使用'autoindex on'設置訪問文件。如果權限設置錯誤,我能做到嗎? –

+1

好的,所以我的理解是你說的是nginx是以一個名爲'nobody'的用戶身份運行的,這很好,但是這些文件夾是由root擁有的,這並不好。糾正我,如果我誤解了。否則,糾正所有權。我不知道在這些條件下autoindex會或不會做什麼。 –

+0

我將整個/ html目錄的所有權更改爲'nobody',並通過在html目錄,mrfs目錄和公用文件夾上運行ls -l來確認這一點。我重新啓動了nginx服務器,仍然收到403禁止的錯誤。 –