簡體   English   中英

Rails 4中的nginx 403禁止錯誤(無index.html文件)

[英]nginx 403 forbidden error in Rails 4 (with no index.html file)

我跟着Railscast http://railscasts.com/episodes/293-nginx-unicorn?view=asciicast一起在Vagrant上設置Nginx和Unicorn,但有一個重要區別。 Ryan使用Rails 3編寫了自己的應用程序(它具有Rails 4僅動態生成的默認/public/index.html)。 安裝並運行Nginx之后,我們可以在端口8080上看到默認頁面。然后,我們為Nginx創建了一個基本配置文件,將其放置在rails應用程序的config目錄中

/config/nginx.conf

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

然后在已啟用並符號鏈接到配置文件的站點中刪除默認頁面

vagrant@lucid32:/etc/nginx/sites-enabled$ sudo rm default 
vagrant@lucid32:/etc/nginx/sites-enabled$ sudo ln -s /vagrant/config/nginx.conf todo 

此后,Ryan重新啟動了nginx並能夠在localhost:8080看到Rails索引頁面。 但是,當我訪問localhost:8080時,出現了403 Forbidden錯誤。

403 Forbidden
nginx/1.1.19

更新資料

由於Rails 4不再具有public / index.html文件,所以我認為403錯誤可能是由該錯誤引起的,正如我從此博客文章http://www.nginxtips.com/403-forbidden-nginx/中了解到的那樣。 它說在配置中將autoindex設置為on (默認為off),但是我不確定如何設置它以顯示Rails主頁。

當我這樣做時

server {
 listen 80 default;

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

它擺脫了403權限錯誤(是的!),但是,它沒有顯示默認的Rails主頁。 而是顯示目錄結構,因此我想知道設置它的正確方法是什么。 在此處輸入圖片說明

如果嘗試將其設置為location / public,則會再次收到403錯誤。 有任何想法嗎?

location /public {
                   autoindex on;
            }

更新資料

由於我使用的是Vagrant(虛擬框),因此該應用程序位於/ vagrant中,但是將位置設置為location / vagrant也會導致403錯誤

location /vagrant {
               autoindex on;
        }

您需要將請求從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,但是,如果您更改了它,則需要指定該端口。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM