【问题标题】:nginx 403 forbidden error in Rails 4 (with no index.html file)Rails 4中的nginx 403禁止错误(没有index.html文件)
【发布时间】:2013-07-17 17:30:26
【问题描述】:

我正在关注 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/。它说在配置中将自动索引设置为on(默认为关闭),但我不确定如何设置它以显示 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;
        }

【问题讨论】:

  • 每个人都想出一个修复?我可以重现这一切!

标签: ruby-on-rails nginx vagrant


【解决方案1】:

您需要将请求从 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 网址。默认情况下,unicorn 将侦听 127.0.0.1:8080,但是,如果您更改了它,则需要指定该端口。

【讨论】:

    猜你喜欢
    • 2011-05-31
    • 2017-12-05
    • 2015-02-02
    • 1970-01-01
    • 2012-08-23
    • 2013-11-20
    • 2017-07-15
    • 2013-07-19
    • 2016-03-26
    相关资源
    最近更新 更多