2013-07-18 27 views
4

我跟着Railscast http://railscasts.com/episodes/293-nginx-unicorn?view=asciicast一起關於在Vagrant上設置Nginx和Unicorn,但有一個重要區別。 Ryan使用Rails 3(具有默認的/public/index.html Rails 4只能動態生成)的應用程序。在獲得Nginx的安裝並運行後,我們能夠在端口8080上看到默認頁面。然後,我們爲Nginx創建了一個基本配置文件,將其放入rails應用的config目錄中。nginx 403在Rails 4中沒有任何錯誤(沒有index.html文件)

server { 
listen 80 default; 
# server_name example.com; 
root /vagrant/public; 
} 

,然後取出在網站的默認頁面啓用和符號鏈接配置文件

[email protected]:/etc/nginx/sites-enabled$ sudo rm default 
[email protected]:/etc/nginx/sites-enabled$ sudo ln -s /vagrant/config/nginx.conf todo 

在此之後,瑞恩重啓nginx的,並能看到Rails的索引頁面localh OST:8080。但是,當我訪問localhost:8080時,我得到了一個403 Forbidden錯誤。

403 Forbidden 
nginx/1.1.19 

更新

因爲軌道4沒有公共/ index.html文件了,我覺得403錯誤可能是由引起的,因爲我是從這個博客帖子 http://www.nginxtips.com/403-forbidden-nginx/教訓。它說要在配置中將autoindex設置爲on(默認爲關閉),但我不確定如何設置它以使Rails主頁顯示。

當我做這個

server { 
listen 80 default; 

root /vagrant/public; 
location/{ 
       autoindex on; 
     } 
} 

它擺脫了403個權限錯誤(耶!),但是,這不是顯示默認的Rails主頁。而是顯示目錄結構,所以我想知道設置它的正確方法是什麼。 enter image description here

如果我嘗試將其設置爲位置/公共,我再次得到403錯誤。有任何想法嗎?

location /public { 
        autoindex on; 
      } 

更新

由於我使用的是流浪(虛框),應用程序是在/遊民,但位置設置位置/遊民也導致403錯誤

location /vagrant { 
       autoindex on; 
     } 
+0

每個想出了一個修復?我可以重現所有這一切! –

回答

2

您需要將請求從Nginx傳遞給Unicorn。您可以這樣做:

server { 
    listen *:80; 
    root /vagrant/public; 

    location/{ 
    # Serve static files if they exist, if not pass the request to rails 
    try_files $uri $uri/index.html $uri.html @rails; 
    } 

    location @rails { 
    proxy_redirect off; 
    proxy_set_header X-Forwarded-Proto $scheme; 
    proxy_set_header Host    $http_host; 
    proxy_set_header X-Real-IP   $remote_addr; 

    proxy_pass http://127.0.0.1:8080; 
    } 
} 

您可能必須更改proxy_pass網址。默認情況下,獨角獸會在127.0.0.1:8080上監聽,但是如果你改變了那個,那麼你需要指定那個端口。

相關問題