2017-03-01 123 views
0

我有一个wordpress站点部署在/,rails应用程序部署了一个子目录/h20-initiative。 应用程序的首页加载,但没有任何静态文件。我究竟做错了什么?rails 5,nginx + passenger,无法提供静态资产(404)

这是我的nginx配置文件。

server { 
    listen 80 default_server; 
    listen [::]:80 default_server; 

    root /var/sites/unify_wordpress; 
    index index.php index.html index.htm index.nginx-debian.html; 

    passenger_enabled on; 
    passenger_app_env production; 
    server_name 10.0.15.11; 

    location/{ 
     #try_files $uri $uri/ =404; 
     try_files $uri $uri/ /index.php$is_args$args; 
    } 

    location ~ \.php$ { 
     include snippets/fastcgi-php.conf; 
     fastcgi_pass unix:/run/php/php7.0-fpm.sock; 
    } 

    location ~ /\.ht { 
     deny all; 
    } 

    location = /favicon.ico { log_not_found off; access_log off; } 
    location = /robots.txt { log_not_found off; access_log off; allow all; } 
    location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ { 
     expires max; 
     log_not_found off; 
    } 

    location ~ ^/h20-initiative(/.*|$) { 
     alias /home/vagrant/unify/public$1; # <-- be sure to point to 'public'! 
     passenger_base_uri /h20-initiative; 
     passenger_app_root /home/vagrant/unify; 
     passenger_document_root /home/vagrant/unify/public; 
     passenger_enabled on; 

    } 
    location ~* ^/assets/ { 
    expires 1y; 
    add_header Cache-Control public; 

    add_header Last-Modified ""; 
    add_header ETag ""; 
    break; 
    } 
} 

这里是/config/enviornments/production.rb

Rails.application.configure do 

    config.cache_classes = true 

    config.eager_load = true 

    config.consider_all_requests_local  = false 
    config.action_controller.perform_caching = true 

    # Disable serving static files from the `/public` folder by default since 
    # Apache or NGINX already handles this. 
    config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present? 

    # Compress JavaScripts and CSS. 
    config.assets.js_compressor = :uglifier 
    # config.assets.css_compressor = :sass 

    # Do not fallback to assets pipeline if a precompiled asset is missed. 
    config.assets.compile = false 

    # `config.assets.precompile` and `config.assets.version` have moved to config/initializers/assets.rb 

    # Enable serving of images, stylesheets, and JavaScripts from an asset server. 
    # config.action_controller.asset_host = 'http://assets.example.com' 

    # Specifies the header that your server uses for sending files. 
    # config.action_dispatch.x_sendfile_header = 'X-Sendfile' # for Apache 
    # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX 

    # Mount Action Cable outside main process or domain 
    # config.action_cable.mount_path = nil 
    # config.action_cable.url = 'wss://example.com/cable' 
    # config.action_cable.allowed_request_origins = [ 'http://example.com', /http:\/\/example.*/ ] 

    # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. 
    # config.force_ssl = true 

    # Use the lowest log level to ensure availability of diagnostic information 
    # when problems arise. 
    config.log_level = :debug 

    # Prepend all log lines with the following tags. 
    config.log_tags = [ :request_id ] 

    # Use a different cache store in production. 
    # config.cache_store = :mem_cache_store 

    # Use a real queuing backend for Active Job (and separate queues per environment) 
    # config.active_job.queue_adapter  = :resque 
    # config.active_job.queue_name_prefix = "spam_#{Rails.env}" 
    config.action_mailer.perform_caching = false 

    # Ignore bad email addresses and do not raise email delivery errors. 
    # Set this to true and configure the email server for immediate delivery to raise delivery errors. 
    # config.action_mailer.raise_delivery_errors = false 

    # Enable locale fallbacks for I18n (makes lookups for any locale fall back to 
    # the I18n.default_locale when a translation cannot be found). 
    config.i18n.fallbacks = true 

    # Send deprecation notices to registered listeners. 
    config.active_support.deprecation = :notify 

    # Use default logging formatter so that PID and timestamp are not suppressed. 
    config.log_formatter = ::Logger::Formatter.new 

    # Use a different logger for distributed setups. 
    # require 'syslog/logger' 
    # config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name') 

    if ENV["RAILS_LOG_TO_STDOUT"].present? 
    logger   = ActiveSupport::Logger.new(STDOUT) 
    logger.formatter = config.log_formatter 
    config.logger = ActiveSupport::TaggedLogging.new(logger) 
    end 

    # Do not dump schema after migrations. 
    config.active_record.dump_schema_after_migration = false 
end 

UPDATE:

在浏览器中使用开发工具,这里是一个没有被发现的例子资产:

http://localhost:8080/h20-initiative/assets/application-eca7aa380d6901462faf375c03979ed8fd1292bda70e3321f82c272e050290d3.css

确切文件确实在我的公共目录中(至少我认为)

That exact file _does_ exist in my public directory (at least I think) 

app_root $ find public/ -name "application-eca7aa380d6901462faf375c03979ed8fd1292bda70e3321f82c272e050290d3.css" 
public/assets/application-eca7aa380d6901462faf375c03979ed8fd1292bda70e3321f82c272e050290d3.css 
+0

确保你有'config.relative_url_root'设置:http://guides.rubyonrails.org/configuring.html#deploy-to-a-subdirectory-relative-url-root –

+0

@SeanHuber感谢这一点。我添加了这个配置并重新启动了nginx。没有任何变化 – stoebelj

+0

您还需要在进行更改后重新编译资产。 –

回答

0

感谢来自@SeanHuber一个建议,我把我的nginx的日志级别debug

要做到这一点,去/etc/nginx/nginx.conf。找到行error_log /var/log/nginx/error.log;并将其更改为error_log /var/log/nginx/error.log debug;

然后,我通过日志greped ...

我搜索的结果没有被服务的资产之一,发现这些行:

2017/03/01 02:02:31 [debug] 28274#28274: *231 test location: "/" 
2017/03/01 02:02:31 [debug] 28274#28274: *231 test location: "robots.txt" 
2017/03/01 02:02:31 [debug] 28274#28274: *231 test location: "favicon.ico" 
2017/03/01 02:02:31 [debug] 28274#28274: *231 test location: ~ "\.php$" 
2017/03/01 02:02:31 [debug] 28274#28274: *231 test location: ~ "/\.ht" 
2017/03/01 02:02:31 [debug] 28274#28274: *231 test location: ~ "\.(css|gif|ico|jpeg|jpg|js|png)$" 
2017/03/01 02:02:31 [debug] 28274#28274: *231 using configuration "\.(css|gif|ico|jpeg|jpg|js|png)$" 

它看起来像块"\.(css|gif|ico|jpeg|jpg|js|png)$"是匹配的块,并用于确定在哪里找到文件。问题是,这不是文件的位置!实际上,我不需要从根目录提供任何资产。我删除了该块,以允许预定块为我的资产提供服务。

相关问题