rails 5,nginx + passenger,不能提供静态资产(404)

时间:2017-03-01 00:45:22

标签: ruby-on-rails nginx passenger

我在/部署了一个wordpress网站,其中rails应用程序部署了一个子目录/h20-initiative。 rails app的首页加载,但没有任何静态文件。我做错了什么?

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

更新:

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

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

1 个答案:

答案 0 :(得分:0)

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

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

然后我在日志中喋喋不休......

我搜索了一个没有提供服务的资产,并找到了这些行:

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)$"是匹配的块,用于确定在何处查找文件。问题是,这不是文件的位置!实际上,我不需要从根目录提供任何资产。我删除了该块,允许预期的块为我的资产提供服务。