0

In my local machine when I do:

RAILS_ENV=production bundle exec rake assets:precompile
RAILS_ENV=production bundle exec puma -e production

Everything works fine. However I have the exact same app in a docker container for production that runs with nginx. I can see the application.css & application.js files in the assets folder with the chrome dev tools and they're not empty. But I have a page with no css/js, it should have something to do with nginx but I'm really confused.

/var/etc/nginx/nginx.conf:

user app sudo;

http{

  upstream app {
    server unix:/home/app/puma.sock fail_timeout=0;
  }

  server {
    listen 80 default;
    root /home/app/app/public;
    try_files $uri/index.html $uri @app;

    location @app {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect off;
      proxy_pass http://app;
    }
  }

  }

  events {worker_connections 1024;}

config/environments/production.rb:

Rails.application.configure do
  ...
  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true
  config.serve_static_files = true
  config.assets.js_compressor = :uglifier
  config.assets.compile = false
  config.assets.digest = true
  ...
  end

Please do not hesitate to suggest anything. :)

update: I've just realised that I have these 2 errors in my console:

new:11 Resource interpreted as Stylesheet but transferred with MIME type text/plain: "http://localhost/assets/application-5574b338d88d13681ef38b9b0800bc47.css".
new:12 Resource interpreted as Script but transferred with MIME type text/plain: "http://localhost/assets/application-ea59e17aff7f15a316e3e03d49f3daf4.js".
Badr Tazi
  • 749
  • 1
  • 6
  • 20

1 Answers1

0

If you want nginx to serve your static assets, you'll need to set config.serve_static_files = false so Rails doesn't try to do it for you.

eddiezane
  • 896
  • 7
  • 13
  • Thanks for your response, I've changed config.serve_static_files to false, but when I change root to /home/app/app/public/assets the assets don't even show in the chrome tools. I have updated my question, can you please take a look? – Badr Tazi Jul 19 '15 at 20:10
  • I was totally wrong on the `js` and `css` folder. Adding `include /etc/nginx/mime.types;` in your config will get rid of those errors. – eddiezane Jul 19 '15 at 20:17
  • How are you including your stylesheet and js files in your view? – eddiezane Jul 19 '15 at 20:20
  • I didn't do anything special. I have a stylesheet_link_tag and a javascript_include_tag in my application layout head. I'm using sass-rails for css, I import the css.scss files in the application.css.css file and I add the css files in the vendor folder with *= require css_file. I do have some js scripts in my views. But I think that this issue comes from my nginx.conf file. – Badr Tazi Jul 19 '15 at 20:32
  • The application.css and application.js are there but the browser does not read them. – Badr Tazi Jul 19 '15 at 20:33