2013-06-23 24 views
2

我剛剛設置了一個Nginx和獨角獸服務器fpr我的Rails應用程序。直到現在一切順利。兩臺服務器都開始了,我可以通過我的URL訪問它們。 但我的問題是,nginx總是從公用文件夾加載index.html作爲起始頁面。如果我刪除index.html文件,則請求會導致403 Forbidden錯誤。當我嘗試其他路線時,它會導致404找不到錯誤。所以我認爲nginx和獨角獸服務器之間沒有關係。也許我錯了。試圖解決這個問題已經好幾個小時了,但沒有成功。如果有人能幫助我,那會非常好。Rails 3.2 Nginx Unicorn總是嘗試從公用文件夾加載index.html(403)

這裏是我的配置:

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

看起來像你在nginx配置的try_files中指定index.html? – house9

+0

咋但如果index.html不可用不應該加載然後\\ unicorn塊是獨角獸服務器?另外,如果我從@unicorn除外的try_files中刪除所有文件,那麼我會得到「錯誤的參數數量」錯誤。 – user1367922

回答

5

最後我自己找到了解決方案。這是我做的:

其他位置塊干擾,所以它總是加載公共文件夾。在我刪除這行後:

location/{ 
    gzip_static on; 
} 

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

nginx服務器連接到獨角獸。

+0

謝謝你。我也遇到這個問題。現在解決! – Abhi

相關問題