stoebelj
stoebelj

Reputation: 1598

rails 5, nginx + passenger, can't serve static assets (404)

I have a wordpress site deployed at / with a rails app deployed a sub directory, /h20-initiative. The front page of the rails app loads, but without any static files. What am I doing wrong?

Here is my nginx config file.

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;
  }
}

And here is /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:

Using dev tools in my browser, here is an example asset that is not being found:

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

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

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

Upvotes: 3

Views: 2871

Answers (1)

stoebelj
stoebelj

Reputation: 1598

Thanks to a suggestion from @SeanHuber, I set my nginx log level to debug

To do that, go to /etc/nginx/nginx.conf. Find the line error_log /var/log/nginx/error.log; and change it to error_log /var/log/nginx/error.log debug;

Then I greped through the log...

I searched for one of the assets that wasn't being served and found these lines:

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

It looks like the block "\.(css|gif|ico|jpeg|jpg|js|png)$" was the matched block and used to determine where to find the file. The problem is, that's not where the file is! In fact, I don't need to serve any assets from the root directory. I deleted that block, allowing for the intended block to serve my assets.

Upvotes: 3

Related Questions