Home > database >  Cannot access assets - dockerized Rails app, nginx as a reverse proxy
Cannot access assets - dockerized Rails app, nginx as a reverse proxy

Time:10-08

I'm trying to run my test app on production. I have it dockerized using below Dockerfile:

FROM ruby:3.0.2

WORKDIR /app

COPY Gemfile Gemfile.lock ./
RUN bundle install

RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN apt-get update && apt-get install -y yarn

COPY . ./

RUN bin/rails assets:precompile

CMD ["rails", "server", "-b", "0.0.0.0"]

My sites-enabled config for this site looks like this:

server {
  server_name   mywebsite.com;

  location / {
    proxy_pass  http://localhost:3000;
  }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/mywebsite.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/mywebsite.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}
server {
    if ($host = mywebsite.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


  listen        80;
  server_name   mywebsite.com;
    return 404; # managed by Certbot
}

And I run it manually with this command:

sudo docker run -p 3000:3000 -e DATABASE_NAME=dbname -e DATABASE_USER=dbuser -e DATABASE_PASSWORD=dbpassword -e RAILS_ENV=production -e DATABASE_HOST=my_ip -d 72b09817c012

It's working but I get 404 on all asset files:

https://mywebsite.com/assets/application-04024382391bb910584145d8113cf35ef376b55d125bb4516cebeb14ce788597.css
https://mywebsite.com/packs/css/application-1f2a30e1.css
https://mywebsite.com/packs/js/application-98802c4d22f32af59b73.js

When I login into the running container and check public/assets and public/packs folders the files are there.

Any idea what I'm missing here?

CodePudding user response:

In production, Rails defaults to not serving static assets (under the assumption that you'll be hosting them on a CDN). You can flip that with a config setting: https://edgeguides.rubyonrails.org/configuring.html#config-public-file-server-enabled

# in: environments/production.rb

Rails.application.configure do
  # ...
  config.public_file_server.enabled = true
  # ...
end
  • Related