2014-02-09 38 views
1

我有一個運行在獨角獸/ nginx下的Ruby on Rails應用程序。問題在於nginx不會爲我的所有資產提供服務。 JS文件似乎已被加載,但圖像未提供。nginx不會提供我所有的資源(Ruby on Rails 4)

這裏是我的nginx的conf文件:

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

server { 
    listen 80 default deferred; 
    server_name exemple.com; 

    root /home/aiccrr/aiccrr/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; 
    } 

    location ~ \.(js|css|png|jpg|jpeg|gif|ico|html)$ { 
    gzip_static on; 
    expires max; 
    add_header Cache-Control public; 
    } 

    error_page 500 502 503 504 /500.html; 
    client_max_body_size 1G; 
    keepalive_timeout 10; 
} 

unicorn.rb文件:

root = "/home/aiccrr/aiccrr" 

working_directory root 
pid "#{root}/tmp/pids/unicorn.pid" 
stderr_path "#{root}/log/unicorn.log" 
stdout_path "#{root}/log/unicorn.log" 

listen "/tmp/unicorn.aiccrr.sock" 
worker_processes 2 
timeout 30 

我做耙資產:今天預編譯至少10倍,並加入這行來production.rb :

config.assets.precompile += %w[*.png *.jpg *.jpeg *.gif] 

我用盡了想法。你有什麼想法嗎?

預先感謝您。

+0

您是否運行過'rake assets:precompile',它應該將您的資產編譯到您的'public'文件夾中? – pduersteler

+0

嗨,謝謝你的回覆。是的,我確實運行過它。 – west

+0

爲什麼你給'$ uri/index.html'更高的優先級?你是否確定該文件不存在? –

回答

0

嘗試以下操作:

禁用Rails的資產服務器

# config/production.rb 
# Disable Rails's static asset server (Apache or nginx will already do this). 
    config.serve_static_assets = false 

嘗試以下更改`nginx.conf

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

server { 
    listen 80 default deferred; 
    server_name exemple.com; 

    location/{ 
    root /home/aiccrr/aiccrr/public; 

    location ~ \.(js|css|png|jpg|jpeg|gif|ico|html)$ { 
     expires max; 
     gzip_static on; 
     add_header Cache-Control public; 
     break; 
    } 

    # If the file exists as a static file serve it directly without 
    # running all the other rewrite tests on it 
    if (-f $request_filename) { 
     break; 
    } 

    if (!-f $request_filename) { 
     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 1G; 
    keepalive_timeout 10; 
} 

如果仍然不能正常工作,後回用日誌文件&的結果也是可以複製此問題的示例nginx的URL。謝謝

+0

你能幫我嗎? http://stackoverflow.com/questions/30829635/puma-nginx-not-showing-any-images-in-productionubuntu –