rails部署使用nginx&独角兽:403禁止错误 [英] rails deployment using nginx & unicorn: 403 forbidden error

查看:201
本文介绍了rails部署使用nginx&独角兽:403禁止错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚设置了一个VPS(Centos 6.3),并使用capistrano部署我的应用程序。 VPS运行nginx&独角兽。我在访问服务器时收到403 Forbidden错误:此行显示在
/var/log/nginx/error.log中:

I have just setup a VPS (Centos 6.3) and deploy my app using capistrano. The VPS runs nginx & unicorn. I'm getting a 403 Forbidden error when visiting the server: this line appears in /var/log/nginx/error.log:

*5 directory index of "/var/www/current/public/" is forbidden, client: xxxxx,   server: xxx, request: "GET / HTTP/1.1", host: "xxxx"

但是,如果我将index.html添加到我的rails应用程序./public中,没有问题。

我还使用chmod -R 755设置了/ var / www的所有文件夹的权限*(可能不是长期的最好的想法,但想排除这是一个错误的来源)。有没有其他方法来调试这个更详细的(error.log文件不告诉我什么别的)?

I have also set the permissions for all folders of /var/www using chmod -R 755 * (probably not the best idea in the long run but wanted to exclude this as a source of error). Is there any other way to debug this in more detail (the error.log file does not tell me anything else)?

任何帮助是非常赞赏。

这是我的routes.rb文件(一切正在开发中工作)

This is my routes.rb file (everything's working locally in development)

MyTest::Application.routes.draw do

  root :to => 'welcome#index'
end

以下是我的nginx.conf文件:

The following is my nginx.conf file:

upstream unicorn {
  server unix:/tmp/unicorn.blog.sock fail_timeout=0;
}

server {
  listen 80 default deferred;
  # server_name example.com;
  root /var/www/current/public;

  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  try_files $uri/index.html $uri @unicorn;
  location @unicorn {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://unicorn;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 5;
}

这是unicorn_init.sh:

This is the unicorn_init.sh:

#!/bin/sh
set -e

# Feel free to change any of the following variables for your app:
TIMEOUT=${TIMEOUT-60}
APP_ROOT=/var/www/current
PID=$APP_ROOT/tmp/pids/unicorn.pid
CMD="cd $APP_ROOT; bundle exec unicorn -D -c $APP_ROOT/config/unicorn.rb -E production"
AS_USER=##########
set -u

OLD_PIN="$PID.oldbin"

sig () {
  test -s "$PID" && kill -$1 `cat $PID`
}

oldsig () {
  test -s $OLD_PIN && kill -$1 `cat $OLD_PIN`
}

run () {
  if [ "$(id -un)" = "$AS_USER" ]; then
    eval $1
  else
    su -c "$1" - $AS_USER
  fi
}

case "$1" in
start)
  sig 0 && echo >&2 "Already running" && exit 0
  run "$CMD"
  ;;
stop)
  sig QUIT && exit 0
  echo >&2 "Not running"
  ;;
force-stop)
  sig TERM && exit 0
  echo >&2 "Not running"
  ;;
restart|reload)
  sig HUP && echo reloaded OK && exit 0
  echo >&2 "Couldn't reload, starting '$CMD' instead"
  run "$CMD"
  ;;
upgrade)
  if sig USR2 && sleep 2 && sig 0 && oldsig QUIT
  then
    n=$TIMEOUT
    while test -s $OLD_PIN && test $n -ge 0
    do
      printf '.' && sleep 1 && n=$(( $n - 1 ))
    done
    echo

    if test $n -lt 0 && test -s $OLD_PIN
    then
      echo >&2 "$OLD_PIN still exists after $TIMEOUT seconds"
      exit 1
    fi
    exit 0
  fi
  echo >&2 "Couldn't upgrade, starting '$CMD' instead"
  run "$CMD"
  ;;
reopen-logs)
  sig USR1
  ;;
*)
  echo >&2 "Usage: $0 <start|stop|restart|upgrade|force-stop|reopen-logs>"
  exit 1
  ;;
esac

最后,我的Gemfile:

And finally, my Gemfile:

source 'https://rubygems.org'

gem 'rails', '3.2.12'

# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'

gem 'pg'
gem 'execjs', '~> 1.4.0'
gem 'therubyracer'
gem "less-rails"

# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'

  # See https://github.com/sstephenson/execjs#readme for more supported runtimes
  # gem 'therubyracer', :platforms => :ruby

  gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'

# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'

# To use Jbuilder templates for JSON
# gem 'jbuilder'

# Use unicorn as the app server
 gem 'unicorn'

# Deploy with Capistrano
 gem 'capistrano'

# To use debugger
# gem 'debugger'


gem "haml", "~> 4.0.0"
gem 'twitter-bootstrap-rails', '~> 2.2.6'
gem "simple_form", "~> 2.1.0"

这是我的unicorn.rb:

This is my unicorn.rb:

root = "/var/www/current"
working_directory root
pid "#{root}/tmp/pids/unicorn.pid"
stderr_path "#{root}/log/unicorn.log"
stdout_path "#{root}/log/unicorn.log"

listen "/tmp/unicorn.blog.sock"
worker_processes 4
timeout 30


推荐答案

我想出来了。在Centos 6.3上,标准配置存储在/etc/nginx/conf.d/
中删除此目录中的默认配置后,一切正常。注意,它可能是更好的符号链接自定义nginx配置文件到/etc/nginx/conf.d而不是/ etc / nginx / sites-enabled(我不得不在我的系统上创建这个目录,然后将其包括在我的/ etc /nginx/nginx.conf)。

I figured it out. On Centos 6.3 the standard configurations are stored in /etc/nginx/conf.d/ After deleting the default configuration in this directory everything worked. Note that it probably is better to symlink the custom nginx config file into /etc/nginx/conf.d instead of /etc/nginx/sites-enabled (I had to create this directory on my system and then include it in my /etc/nginx/nginx.conf).

这篇关于rails部署使用nginx&amp;独角兽:403禁止错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆