简体   繁体   English

Rails 4中的nginx 403禁止错误(无index.html文件)

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

I'm following along with a Railscast http://railscasts.com/episodes/293-nginx-unicorn?view=asciicast about setting up Nginx and Unicorn on Vagrant, with one important difference. 我跟着Railscast http://railscasts.com/episodes/293-nginx-unicorn?view=asciicast一起在Vagrant上设置Nginx和Unicorn,但有一个重要区别。 Ryan's making his application with Rails 3 (that has the default /public/index.html that Rails 4 only generates dynamically). Ryan使用Rails 3编写了自己的应用程序(它具有Rails 4仅动态生成的默认/public/index.html)。 After getting Nginx installed and running, we were able to see the default page on port 8080. We then created a basic config file for Nginx to put in the config directory of the rails application 安装并运行Nginx之后,我们可以在端口8080上看到默认页面。然后,我们为Nginx创建了一个基本配置文件,将其放置在rails应用程序的config目录中

/config/nginx.conf /config/nginx.conf

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

and then removed the default page in sites enabled and symlinked to the configuration file 然后在已启用并符号链接到配置文件的站点中删除默认页面

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

After this, Ryan restarted nginx and was able to see the Rails index page at localhost:8080. 此后,Ryan重新启动了nginx并能够在localhost:8080看到Rails索引页面。 However, when I visit localhost:8080, I'm getting a 403 Forbidden error. 但是,当我访问localhost:8080时,出现了403 Forbidden错误。

403 Forbidden
nginx/1.1.19

Update 更新资料

since Rails 4 doesn't have the public/index.html file anymore, I think the 403 error might be caused by that, as I learned from this blog post http://www.nginxtips.com/403-forbidden-nginx/ . 由于Rails 4不再具有public / index.html文件,所以我认为403错误可能是由该错误引起的,正如我从此博客文章http://www.nginxtips.com/403-forbidden-nginx/中了解到的那样。 It says to set autoindex to on (the default is off) in the config, but I'm not sure how to set it to get the Rails homepage to show. 它说在配置中将autoindex设置为on (默认为off),但是我不确定如何设置它以显示Rails主页。

When I did this 当我这样做时

server {
 listen 80 default;

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

it got rid of the 403 permissions error (yay!), however, it's not showing the default Rails home page. 它摆脱了403权限错误(是的!),但是,它没有显示默认的Rails主页。 Rather it's showing the directory structure so I'm wondering what the proper way to set it is. 而是显示目录结构,因此我想知道设置它的正确方法是什么。 在此处输入图片说明

If I try to set it to location/public, I get the 403 error again. 如果尝试将其设置为location / public,则会再次收到403错误。 Any ideas? 有任何想法吗?

location /public {
                   autoindex on;
            }

Update 更新资料

Since I'm using Vagrant (virtual box), the app is in /vagrant, however setting the location to location/vagrant also results in a 403 error 由于我使用的是Vagrant(虚拟框),因此该应用程序位于/ vagrant中,但是将位置设置为location / vagrant也会导致403错误

location /vagrant {
               autoindex on;
        }

You'll need to pass the request from Nginx to Unicorn. 您需要将请求从Nginx传递到Unicorn。 You can do this like so: 您可以这样做:

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

You may have to change the proxy_pass url. 您可能必须更改proxy_pass网址。 By default, unicorn will listen on 127.0.0.1:8080 but, if you have changed that, then you will need to specify that port. 默认情况下,独角兽会监听127.0.0.1:8080,但是,如果您更改了它,则需要指定该端口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM