简体   繁体   English

Rails 3.2 Nginx Unicorn总是尝试从公用文件夹加载index.html(403)

[英]Rails 3.2 Nginx Unicorn always try to load index.html (403) from public folder

I just setup a Nginx and unicorn server fpr my rails app. 我只是在我的rails应用程序上设置了Nginx和独角兽服务器。 Till now all went well. 到现在为止一切顺利。 Both servers are starting and i can access them through my URL. 两台服务器都在启动,我可以通过我的URL访问它们。 But my problem is that nginx always loads the index.html from the public folder as the start page. 但是我的问题是,nginx总是从公用文件夹中将index.html加载为起始页。 If I delete the index.html file then the request results in 403 Forbidden error. 如果我删除index.html文件,那么请求将导致403 Forbidden错误。 When I try other routes then it results in 404 not found error. 当我尝试其他路线时,它会导致404 not found错误。 So I assume that there is no connection between the nginx and unicorn server. 因此,我假设Nginx和独角兽服务器之间没有连接。 Maybe I am wrong. 也许我错了。 Trying to solve this already for hours but had no success. 试图解决这个问题已经花费了数小时,但没有成功。 If someone could help me , would be really nice. 如果有人可以帮助我,那就太好了。

Here are my configurations: 这是我的配置:

nginx.comfig: nginx.comfig:

/etc/nginx/conf.d/medinow.conf /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: 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

I just can't figure out where I did the mistake. 我只是不知道我在哪里做错了。 Maybe someone of you know where I forgot something. 也许有人知道我在哪里忘记了什么。

Finally i found the solution myself. 最后,我自己找到了解决方案。 Here is what i did: 这是我所做的:

The other location-blocks interfered so it always loaded the public folder. 其他位置块受到干扰,因此它总是加载公用文件夹。 After I deleted this lines : 在我删除以下行后:

location / {
    gzip_static on;
}

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

the nginx server connects to unicorn. Nginx服务器连接到独角兽。

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

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