我刚刚设置了一个Nginx和unicorn服务器fpr我的rails应用程序。直到现在一切顺利。两台服务器都在启动,我可以通过我的URL访问它们。 但我的问题是nginx总是从公共文件夹加载index.html作为起始页面。如果我删除index.html文件,那么请求会导致403 Forbidden错误。当我尝试其他路线时,它会导致404找不到错误。所以我假设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
我无法弄清楚我在哪里犯了错误。也许有人知道我忘了什么。
答案 0 :(得分:5)
最后我自己找到了解决方案。这是我做的:
其他位置块受到干扰,因此它始终加载公用文件夹。我删除这一行后:
location / {
gzip_static on;
}
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
nginx服务器连接到独角兽。