ruby-on-rails - rails 5,nginx + passenger,无法提供静态 Assets (404)

标签 ruby-on-rails nginx passenger

我在 / 部署了一个 wordpress 站点,并在 /h20-initiative 部署了一个 Rails 应用程序。 加载 Rails 应用程序的首页,但没有任何静态文件。我做错了什么?

这是我的 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

更新:

在我的浏览器中使用开发工具,这是一个未找到的示例 Assets :

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

那个确切的文件确实存在于我的公共(public)目录中(至少我认为是这样)

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

最佳答案

感谢@SeanHuber 的建议,我将我的 nginx 日志级别设置为 debug

为此,请转到 /etc/nginx/nginx.conf。找到行 error_log/var/log/nginx/error.log; 并将其更改为 error_log/var/log/nginx/error.log debug;

然后我在日志中搜索...

我搜索了一项未提供服务的 Assets 并找到了这些行:

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)$"

看起来 block "\.(css|gif|ico|jpeg|jpg|js|png)$" 是匹配的 block ,用于确定在哪里可以找到文件。问题是,那不是文件所在的位置!事实上,我不需要从根目录提供任何 Assets 。我删除了那个区 block ,允许预期的区 block 为我的 Assets 提供服务。

关于ruby-on-rails - rails 5,nginx + passenger,无法提供静态 Assets (404),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42521316/

相关文章:

mysql - 尝试部署 Rails 应用程序时,使用 MySql 时出现重复列名错误

ruby-on-rails - 利用 Nginx 的浏览器缓存问题

ruby - nginx 背后的 Unicorn vs Passenger Standalone

ruby-on-rails - modrails - 流氓 ruby​​ 进程消耗 100% cpu

ruby-on-rails - 通过 .htaccess 配置 Phusion 乘客

ruby-on-rails - Rails - 在索引 View 中显示相关数据

ruby-on-rails - 在 Rails 中自定义 "Password confirmation doesn' t 匹配密码

php - NGINX - 安装了 CURL 扩展,但系统说它丢失了

php - 动态 URL 段的 Nginx 位置 block

ruby-on-rails - 安装 net-ssh (3.0.2) 时出错,Bundler 无法继续