【问题标题】:Rails 3.2 Nginx Unicorn always try to load index.html (403) from public folderRails 3.2 Nginx Unicorn 总是尝试从公共文件夹加载 index.html (403)
【发布时间】:2013-06-20 07:58:37
【问题描述】:

我刚刚在我的 rails 应用程序中设置了 Nginx 和 unicorn 服务器。到现在为止一切顺利。两台服务器都在启动,我可以通过我的 URL 访问它们。 但我的问题是 nginx 总是从公用文件夹加载 index.html 作为起始页。如果我删除 index.html 文件,则请求会导致 403 Forbidden 错误。当我尝试其他路线时,会导致 404 not found 错误。所以我假设nginx和unicorn服务器之间没有连接。也许我错了。试图解决这个问题已经几个小时但没有成功。如果有人可以帮助我,那就太好了。

这是我的配置:

nginx.comfig:

/etc/nginx/conf.d/medinow.conf

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

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

  location / {
    gzip_static on;
  }

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

unicorn.conf.rb:

worker_processes 4

APP_PATH = "/var/www/medinow/current"
working_directory APP_PATH # available in 0.94.0+

listen "/tmp/unicorn.medinow.sock", :backlog => 64
listen 8080, :tcp_nopush => true


timeout 30

pid APP_PATH + "/tmp/pids/unicorn.pid"

stderr_path APP_PATH + "/log/unicorn.medinow.stderr.log"
stdout_path APP_PATH + "/log/unicorn.medinow.stdout.log"

preload_app true
GC.respond_to?(:copy_on_write_friendly=) and
  GC.copy_on_write_friendly = true

check_client_connection false

before_fork do |server, worker|

  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.connection.disconnect!

   old_pid = "#{server.config[:pid]}.oldbin"
   if old_pid != server.pid
     begin
       sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
       Process.kill(sig, File.read(old_pid).to_i)
     rescue Errno::ENOENT, Errno::ESRCH
     end
   end

 end

after_fork do |server, worker|
  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.establish_connection
end

before_exec do |server|
  ENV["BUNDLE_GEMFILE"] = "/var/www/medinow/current/Gemfile"
end

我只是不知道我在哪里做错了。也许你们当中有人知道我在哪里忘记了什么。

【问题讨论】:

  • 看起来你在 nginx 配置的 try_files 中指定了 index.html?
  • ye 但如果 index.html 不可用,它不应该加载然后作为独角兽服务器的 \@unicorn 块吗?此外,如果我从 try_files 中删除除 @unicorn 之外的所有文件,则会收到“参数数量错误”错误。

标签: ruby-on-rails nginx unicorn http-status-code-403


【解决方案1】:

最后我自己找到了解决方案。这是我所做的:

其他位置块受到干扰,因此它总是加载公用文件夹。在我删除这行之后:

location / {
    gzip_static on;
}

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

nginx服务器连接unicorn。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-19
    • 1970-01-01
    • 1970-01-01
    • 2020-03-02
    • 1970-01-01
    • 2012-06-19
    • 2018-11-30
    • 1970-01-01
    相关资源
    最近更新 更多