Assets not loading in newly deployed Rails app - ruby-on-rails

Just deployed my rails app for the first time and none of my assets are loading. I don;t use the Asset pipeline but have left it switched on for any gems that may use it. I have a Gulp workflow configured which outputs all assets to the public/assets directory.
My my layout I call:
<link rel="stylesheet" href="<%= gulp_asset_path('stylesheets/application.css') %>">
which gives me:
<link rel="stylesheet" href="/assets/stylesheets/application.css">
Any ideas?
Edit:
My conf file:
server {
listen 80 default_server;
server_name dev.myapp.co.uk;
passenger_enabled on;
rails_env production;
root /home/deploy/myapp/current/public;
location /assets {
gzip_static on;
expires max;
add_header Cache-Control public;
}
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

Related

Nginx + Puma + Sidekiq web interface not showing css styles

I have an angularJS web app running in a Nginx server that sends request to a Rails API running in a Puma server. I have integrated Sidekiq 5.2.8 and everything works great but the Sidekiq web interface.
In my Nginx config file, I have a rule to pass request to the API. Please find the whole nginx.conf document:
events {
worker_connections 1024;
}
http {
upstream api.development {
# Path to Puma SOCK file, as defined previously
server unix:/tmp/puma.sock fail_timeout=0;
}
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
# set client body size to 10M #
client_max_body_size 10M;
gzip on;
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
#charset koi8-r;
#access_log logs/host.access.log main;
root /Users/Rober/Projects/domain/dev/domain/app;
index index.html index.htm;
try_files $uri $uri/ /index.html =404;
# Proxy requests to the backoffice Rails API
location /api {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
#proxy_set_header X-Forwarded-Proto https;
proxy_redirect off;
rewrite ^/api(.*) /$1 break;
proxy_pass http://api.development;
}
# Rule to proxy the sidekiq web UI
location /sidekiq {
proxy_pass http://api.development;
}
# Expire rules for static content
# RCM: WPO
# Images
location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
root /Users/Rober/Projects/domain/dev/domain/app;
expires 1w;
add_header Cache-Control "public";
}
# This rule is the root cause of the problems with the sidekiq css
# I have commented it for testing purposes
# CSS and Javascript
#location ~* \.(?:css|js)$ {
# root /Users/Rober/Projects/domain/dev/domain/app;
# expires 1w;
# add_header Cache-Control "public";
#}
# I have replaced the previous location above for this as suggested by #Beena Shetty.
location ~* \.(?:css|js)$ {
add_header X-debug-message "Into the location css" always;
if ($uri !~* "^/sidekiq/\w*(.*)+$") {
add_header X-debug-message "Into the location css if" always;
root /Users/Rober/Projects/domain/dev/domain/app;
expires 1w;
add_header Cache-Control "public";
}
}
# cache.appcache, your document html and data
location ~* \.(?:manifest|appcache|html?|xml|json)$ {
root /Users/Rober/Projects/domain/dev/domain/app;
expires -1;
}
}
include servers/*;
}
In Rails:
routes:
require 'sidekiq/web'
mount Sidekiq::Web => '/sidekiq'
I have included next rule in Nginx config file and now when I request http://localhost/sidekiq I can see the web interface and navigate, but still cannot see the styles.
location /sidekiq {
proxy_pass http://api.development;
}
See screenshot.
The dev tools shows that when I load sideqik is trying to get bootstrap.css and some other css and javascript in http://localhost/sidekiq/stylesheets/bootstrap.css
What am I missing?
UPDATE:
I have found out the root cause of the problem in my nginx.conf. I have next rule that set a cache expiration time for performance purposes. If I comment this code, everything works. But how can I have both things living together?
CSS and Javascript
location ~* \.(?:css|js)$ {
root /Users/Rober/Projects/domain/dev/domain/app;
expires 1w;
add_header Cache-Control "public";
}
UPDATE 2: Just in case the problem comes from another point, I have included my whole nginx conf.
Now, with the provided config, the expiration rules in my web app are still working, but the css in the sidekiq webapp do not.
I have included two headers as debug. One when the server is accessing the location rule and the second when the server is accessing inside the if condition. When I request my home page with localhost and I check the request for my own css, such as app.css, I can see the header X-debug-message: Into the location css if, which is right.
If I request sidekiq with localhost/sidekiq I still get 404 error for css, let´s say http://localhost/sidekiq/stylesheets/bootstrap.css and I can see the header X-debug-message: Into the location css.
Current conclusions:
As soon as I include the location ~* .(?:css|js)$ rule, sidekiq css stops working. Even if the rule is empty, like:
location ~* \.(?:css|js)$ {
}
As soon as I delete or comment the whole rule, the sidekiq css works perfectly, but unfortunately this is not compatible with the expires rules that we need to include for performance purposes.
Try this:
location ~* \.(?:css|js)$ {
if ($uri !~* "^/sidekiq/\w*(.*)+$"){
root /Users/Rober/Projects/domain/dev/yanpy/app;
expires 1w;
add_header Cache-Control "public";
}
}
I wasn't able to find a fix for this, so I hacked it with the following method: I copied sidekiq's assets to the public folder and it started working ( since they're referenced by the UI ).
|- images
|-- favicon.io
|-- logo.png
|-- status.png
|- javascripts
|-- application.js
|-- dashboard.js
|- stylesheets
|-- application.css
|-- application-rtl.css
|-- application-dark.css
|-- application-rtl.min.css
|-- bootstrap.css
Mainly, these files: https://github.com/mperham/sidekiq/tree/master/web/assets

Spree not loading images on Nginx

I'm trying to make images load on Spree 3.1.0 + Passenger 5.0.30 + Nginx + Capistrano 3.5 + Digital Ocean, I have followed Go Rails Tutorial and Spree documentation but no luck at the moment.
Weird... Is loading my css files but not loading my image-background
my /etc/nginx/sites-available/default file:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
server_name rodeobest.com;
passenger_enabled on;
rails_env production;
root /home/deployer/RMG_rodeobest/current/public;
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# if the request is for a static resource, nginx should serve it dir$
# and add a far future expires header to it, making the browser
# cache the resource and navigate faster over the website
location ~ ^/(spree|system|assets)/ {
root /home/deployer/RMG_rodeobest/current/public;
expires max;
break;
}
location ~* ^.+\.(jpg|jpeg|gif|png|ico)$ {
root /home/deployer/RMG_rodeobest/current/public;
}
}
As documentation says, I've changed location ~ ^/(system|assets)/ to location ~ ^/(spree|system|assets)/ and restart the server, but as I said, no luck. Any idea? thanks in advance
References:
Reference 1
Reference 2

rails app on nginx+passenger not showing custom error pages

I have a Rails app running on nginx 1.2.0 and passenger 3.0.7. I would like to have the custom error pages in the rails app (e.g. /rail_app/public/500.html) be displayed when the appropriate http error occurs within the app.
Here is my current nginx config file:
http {
passenger_root /usr/lib/ruby/gems/1.8/gems/passenger-3.0.7;
passenger_ruby /usr/bin/ruby;
include mime.types;
default_type application/octet-stream;
#access_log /opt/nginx/logs/access.log main;
sendfile on;
#tcp_nopush on;
server {
listen 80;
server_name localhost;
root /var/www/dashboard/current/public;
passenger_enabled on;
passenger_min_instances 1;
# listen 443;
# ssl on;
# ssl_certificate /opt/nginx/conf/server.crt;
# ssl_certificate_key /opt/nginx/conf/server.key;
error_page 500 502 503 504 /500.html;
location = /500.html {
root /var/www/dashboard/current/public/;
}
}
}
This configuration does not show the rails app customer error page rather just sends the http error status code to the client.
Anyone know what it takes to have nginx/passenger send the rails app custom error page to the client with the http error status code?
Please try the following:
# We use the x just because it is for all 5xx errors.
error_page 500 502 503 504 /5xx.html;
location = /5xx.html {
alias /var/www/dashboard/current/public/;
}
Reconfiguring the root directive makes no sense, as it is already set to the path you specified before. The alias ensures that the specific location is internally matched to a different location on the file system. All incoming request parameters should be passed along and if your Rails app is taking care of things at this point it should answer. Just make sure that your Rails app isn't answering with a 500 status again (I don’t know what would happen then).
Related Links
alias
You're probably missing passenger_intercept_errors on; in your nginx config
see the passenger docs for this directive for more info
The config I use:
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

Rails 3 running in subdirectory, assets not found

I'm serving two Rails sites using nginx. Rails1 does not use the asset pipeline but Rails2 does. Rails2 also uses a prefix to differentiate it from Rails1. For example:
http://myhost -> Rails1
http://myhost/abc -> Rails2
Both sites are running, however any reference to assets on the Rails2 site are not found.
Here's what my pseudo nginx.conf looks like:
http {
upstream rails1 {
server 127.0.0.1:3000;
}
upstream rails2 {
server 127.0.0.1:3030;
}
server {
location ~ ^/assets/ {
expires max;
add_header Cache-Control public;
access_log off;
}
location /abc {
proxy_pass http://rails2;
}
location / {
proxy_pass http://rails1;
}
}
}
Also, the routes.rb in my Rails 2 app:
Rails2App::Application.routes.draw do
scope '/abc' do
resources :projects
root :to => 'home#index'
end
end
Browsing to http://myhost/abc/ for the Rails2 app, brings up the page with no css, and the following error:
GET http://myhost/assets/application-asdasd.css 404 (Not Found)
I've tried using config.assets.prefix = '/abc' in the production.rb file but it didn't work. I've also tried different variations in the ngnix.conf file to no avail either.
Anyone know what I'm doing wrong, or missing?
UPDATE
I'm not quite sure why, but I was able to get this to (incorrectly) work using an #location instead of an upstream. But I had to move the assets folder from the Rails2 app to the Rails1 app. Not exactly ideal.
Changes to the server section:
location ~ ^/(assets)/ {
expires max;
add_header Cache-Control public;
access_log off;
}
location ~ ^/(abc)/ {
root /rails2/public;
try_files $uri/index.html $uri.html $uri #rails2;
error_page 404 /404.html;
error_page 422 /422.html;
error_page 500 502 503 504 /500.html;
error_page 403 /403.html;
}
location / {
root /rails1/public;
try_files $uri/index.html $uri.html $uri #rails1;
error_page 404 /404.html;
error_page 422 /422.html;
error_page 500 502 503 504 /500.html;
error_page 403 /403.html;
}
location #rails1 {
proxy_pass http://127.0.0.1:3000;
}
location #rails2 {
proxy_pass http://127.0.0.1:3030;
}
From a similar question: https://stackoverflow.com/a/3355589/417872
Have you tried this?
config.action_controller.relative_url_root = '/rails_app'
I would recommend googleing for how to correctly serve a rails app from a subdirectory. That's the real issue you're dealing with, and a quick search returned several pages of useful looking links.
I had a similar problem running Rails 4 on Nginx in production environment. The solution I found was to specify the root path for the asset location in nginx.conf:
location ^~ /assets/ {
root /home/rails/myapp/public;
gzip_static on;
expires max;
add_header Cache-Control public;
}
Hope this helps

Maintenance page with nginx for one domain_name among others

I have a rail app that serves multiple domain_name and is deployed by nginx & passenger. I need to put one domain under maintenance mode while the other still work as usual. Here is my config:
server {
listen 80;
server_name domain1.com domain2.com domain3.com domain4.com;
error_page 503 http://$host/maintenance.html;
location /maintenance.html {
# Allow requests
}
location / {
root /var/www/myapp/public; # <--- be sure to point to 'public'!
error_page 503 http://$host/maintenance.html;
passenger_enabled on;
rails_env development;
passenger_use_global_queue on;
if (-f /var/www/myapp/public/maintenance.html) {
return 503;
}
}
}
The above config would cause all domains under maintenance. However, I want to put domain1.com is under maintenance mode. How would I achieve this?
you can add another server entry for server "domain1.com" which serve request for this domain only. like:
server {
listen 80;
server_name domain1.com
error_page 503 http://$host/maintenance.html;
root /your/root/directory/
if (-f $document_root/maintenance.html){
rewrite ^(.*)$ /maintenance.html last;
break;
}
location /maintenance.html {
# Allow requests
}}
you need to ensure following
The "domain1.com" should be removed from previous server
entry
maintenance.html page should be
present in /your/root/directory/

Resources