Set HTTP Headers for Files in Public Folder - ruby-on-rails

I want to set Accept-Ranges none for video files that are requested from my public folder in Rails. The video file is not in my assets pipeline so the video simply lives at /public/videos/example.mp4. How can these HTTP headers be set in development mode? I tried editing the config.public_file_server.headers hash in development.rb but I don't believe this is the correct config.
config.public_file_server.headers = {
'Cache-Control' => "public, max-age=15768000",
"Accept-Ranges" => "none"
}

As far as I know in Rails 4, besides Cache-Control no other response header can be set on files. That’s a limitation.
However with changes in Rails 5, you can set any header you want and that is the correct place to do it for development: config.public_file_server.headers in development.rb
However, for changes to take effect, you'll have to create a development cache using rails dev:cache before startig the server.
Demo:
development.rb:
if Rails.root.join('tmp/caching-dev.txt').exist?
config.action_controller.perform_caching = true
config.cache_store = :dalli_store
config.public_file_server.headers = {
'Cache-Control' => 'public, max-age=172800',
'Accept-Ranges' => 'none'
}
else
config.action_controller.perform_caching = false
config.cache_store = :null_store
end
dev-cache and server
$ rails dev:cache
Development mode is now being cached.
$ rails s
Request:
$ curl -sI http://localhost:3000/car-images-silhouettes/back.png | grep Accept-Ranges
Accept-Ranges: none

Related

Webpack / Webpacker not serving styles with RSpec tests. webpack-dev-server not configured?

I'm new to webpack and clearly missing something here. This seemed to be working until we upgraded our app to rails 6.1 and webpacker 5.4. yarn --version webpack outputs 1.22.10
Everything appears to get served correctly in the development environment. In the test environment some assets appear to get served(like some images and some react templates) but no styles are served and the app is totally unstyled.
One thing I noticed is that if I leave webpack-dev-server off in development mode, I get the same issue as test gets. This makes me think that the test env is not configured properly to work with webpack-dev-server but so far I cannot figure out how to connect them
The app has assets in two locations: app/assets and app/frontend
One interesting thing that I noticed is that the public/packs directory looks very different from the public/packs-test directory` although I can't see any reason why that should be true.
public/packs has a single file in it : manifest.json
public/packs-test has a ton of chunk files in it
Here is the relevant config
webpacker.yml
---
# Note: You must restart bin/webpack-dev-server for changes to take effect
default: &default
source_path: app/frontend
source_entry_path: packs
public_output_path: packs
cache_path: tmp/cache/webpacker
resolved_paths: ['app/assets', 'vendor/assets']
# Reload manifest.json on all requests so we reload latest compiled packs
cache_manifest: false
extensions:
- .erb
- .jsx
- .js
- .sass
- .scss
- .css
- .module.sass
- .module.scss
- .module.css
- .png
- .svg
- .gif
- .jpeg
- .jpg
- .json
development:
<<: *default
compile: true
# Reference: https://webpack.js.org/configuration/dev-server/
dev_server:
https: true
host: localhost
port: 3035
public: localhost:3035
hmr: true
# Inline should be set to true if using HMR
inline: true
overlay: true
compress: true
disable_host_check: true
use_local_ip: false
quiet: false
headers:
'Access-Control-Allow-Origin': '*'
watch_options:
ignored: /node_modules/
aggregateTimeout: 300
poll: 500
test:
<<: *default
compile: true
public_output_path: packs-test
production: &production
<<: *default
compile: false
cache_manifest: true
webstaging:
<<: *production
webtest:
<<: *production
webdev:
<<: *production
config/webpack/environment.js
const { environment } = require('#rails/webpacker')
const erbLoader = require('./loaders/erb')
const svgLoader = require('./loaders/svg')
const addCommonsChunkPlugins = require('./plugins/commons-chunk')
const injectIndividualStylesheets = require('./individual-stylesheets')
// Set shorter hashes
environment.config.set('output.filename', '[name]-[hash:6].js')
environment.config.set('output.chunkFilename', '[name]-[chunkhash:6].chunk.js')
// More readable classnames for css modules
const cssLoader = environment.loaders
.get('moduleCss')
.use.find(el => el.loader === 'css-loader')
cssLoader.options.localIdentName = '[local]_[hash:base64:5]'
// Manually add any ES6 targeted vendor dependencies
// converts /node_modules/ to /node_modules(?!\/my-es6-targeted-module)/
const babelLoader = environment.loaders.get('babel')
const newExcludePattern = new RegExp(`${babelLoader.exclude.source}(?!\/better-youtube-api)`)
babelLoader.exclude = newExcludePattern
// Add erb loader (for images.js.erb)
environment.loaders.append('erb', erbLoader)
// Load svg files differently from css and js files
environment.loaders.insert('svg', svgLoader, { after: 'file' })
// Exclude svg extension from file loader
const fileLoader = environment.loaders.get('file')
fileLoader.exclude = /\.(svg)$/i
// Add commons chunk plugin for extracting common shared modules
addCommonsChunkPlugins(environment)
// Add individual stylesheets to webpack entries
injectIndividualStylesheets(environment)
module.exports = environment
config/webpack/development.js
process.env.NODE_ENV = process.env.NODE_ENV || 'development'
const environment = require('./environment')
module.exports = environment.toWebpackConfig()
config/webpack/test.js
process.env.NODE_ENV = process.env.NODE_ENV || 'development'
const environment = require('./environment')
module.exports = environment.toWebpackConfig()
Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.
config.action_mailer.default_url_options = { host: "localhost", port: 3000 }
Rails.application.routes.default_url_options[:host] = 'localhost:3000'
config.force_ssl = true
# The test environment is used exclusively to run your application's
# test suite. You never need to work with it otherwise. Remember that
# your test database is "scratch space" for the test suite and is wiped
# and recreated between test runs. Don't rely on the data there!
config.cache_classes = true
# Do not eager load code on boot. This avoids loading your whole application
# just for the purpose of running a single test. If you are using a tool that
# preloads Rails for running tests, you may have to set it to true.
config.eager_load = false
# Configure public file server for tests with Cache-Control for performance.
config.public_file_server.enabled = true
config.public_file_server.headers = {
'Cache-Control' => "public, max-age=#{1.hour.seconds.to_i}"
}
# Separate file storage in the test environment
config.active_storage.service = :test
# Use inline job processing to make things happen immediately
config.active_job.queue_adapter = :inline
# Show full error reports and disable caching.
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
# Raise exceptions instead of rendering exception templates.
config.action_dispatch.show_exceptions = false
# Disable request forgery protection in test environment.
config.action_controller.allow_forgery_protection = true
config.action_mailer.perform_caching = false
# Tell Action Mailer not to deliver emails to the real world.
# The :test delivery method accumulates sent emails in the
# ActionMailer::Base.deliveries array.
config.action_mailer.delivery_method = :test
# Print deprecation notices to the stderr.
config.active_support.deprecation = :stderr
# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true
config.x.enable_gtm = false
end
I would be extremely grateful for any insite as I have been fighting this for a while
I still don't really know what was going on here or if it was resolvable but upgrading my webpack (not webpacker) version along with many sub dependencies solved this for me.

Rails Production log dont update

I'm using Rails 6.0.3
In production I try to run a tail -f on the production log and I don't receive updates when I do any action on the application. Any idea?
I tried to apply 0644 permissions to the production.log file, but that's not the problem, because there is data in the file.
I am using nginx and puma too.
Production Log:
Production log file permissions
-rw-rw-r-- 1 ubuntu ubuntu 733 Apr 25 11:44 production.log
production.rb
Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.
# Code is not reloaded between requests.
config.cache_classes = true
# Eager load code on boot. This eager loads most of Rails and
# your application in memory, allowing both threaded web servers
# and those relying on copy on write to perform better.
# Rake tasks automatically ignore this option for performance.
config.eager_load = true
# Full error reports are disabled and caching is turned on.
config.consider_all_requests_local = false
# Ensures that a master key has been made available in either ENV["RAILS_MASTER_KEY"]
# or in config/master.key. This key is used to decrypt credentials (and other encrypted files).
# config.require_master_key = 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?
# Store uploaded files on the local file system (see config/storage.yml for options).
config.active_storage.service = :local
config.cache_store = :redis_cache_store, { url: "redis://localhost:6379/0" }
# Mount Action Cable outside main process or domain.
.....
# 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
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
# Do not dump schema after migrations.
config.active_record.dump_schema_after_migration = false
end
Thanks in advance.
I solved my problem by adding these lines to my production file:
config.logger = Logger.new (Rails.root.join ("log / production.log"))
And restarting the cougar on the production server.

Rails low level cache not working in development mode

I've configured the development.rb file like this:
# Enable/disable caching. By default caching is disabled.
config.action_controller.perform_caching = true
config.cache_store = :memory_store
config.public_file_server.headers = {
'Cache-Control' => 'public, max-age=172800'
}
(erased the default config that checks for a file just to test)
Then I open a rails console to test this but it does not seem to be working:
[1] pry(main)> Rails.cache.write("asd", "asd")
=> true
[2] pry(main)> Rails.cache.read("asd")
=> nil
I've managed to make it work a couple of days ago, but now it is not working.
A I missing something?
I fixed it by restarting spring. Doing
> spring stop
Spring stopped.
> spring start
does the trick.
Note: when starting spring again, it may fail, but it'll work anyway.
In rails 6.1.4
run rails dev:cache to toggle caching in development

Images not showing after deployment with Passenger/Capistrano to Nginx server

Hi I'm deploying my first Rails app to Ubuntu 16 server using Capistrano everything went smooth except the images are not showing in the production environment.
On the production server the images are located in this path : /myapp/current/public/assets
But if I look at this in the browser my broken images links gives me this(see picture), this is a broken link for the header image.
the strange thing is that there is a .svgfile in the /myapp/current/public/assets which is showing up perfectly in the browser, In the picture below is the path shown
this is my Capfile
# Load DSL and set up stages
require "capistrano/setup"
# Include default deployment tasks
require "capistrano/deploy"
set :rbenv_type, :user # or :system, depends on your rbenv setup
set :rbenv_ruby, '2.3.1'
require 'capistrano/rbenv'
require 'capistrano/bundler'
require 'capistrano/rails'
# Load custom tasks from `lib/capistrano/tasks` if you have any defined
Dir.glob("lib/capistrano/tasks/*.rake").each { |r| import r }
This is the config/deploy.rb
# config valid only for current version of Capistrano
lock '3.6.1'
set :application, 'myapp'
set :repo_url, 'git#github.com:DadiHall/myapp.git'
# Default deploy_to directory is /var/www/my_app_name
set :deploy_to, '/home/deploy/myapp'
set :linked_files, %w{config/database.yml config/secrets.yml}
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
namespace :deploy do
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
execute :touch, release_path.join('tmp/restart.txt')
end
end
after :publishing, 'deploy:restart'
after :finishing, 'deploy:cleanup'
end
Here is the environments/production.rb
Rails.application.configure do
config.cache_classes = true
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?
config.assets.js_compressor = :uglifier
# Do not fallback to assets pipeline if a precompiled asset is missed.
config.assets.compile = false
config.assets.digest = true
config.assets.initialize_on_precompile = false
# `config.assets.precompile` and `config.assets.version` have moved to config/initializers/assets.rb
config.log_level = :debug
config.i18n.fallbacks = true
config.active_support.deprecation = :notify
config.log_formatter = ::Logger::Formatter.new
config.active_record.dump_schema_after_migration = false
Braintree::Configuration.environment = :sandbox
Braintree::Configuration.merchant_id = ENV['merchant_id']
Braintree::Configuration.public_key = ENV['public_key']
Braintree::Configuration.private_key = ENV['private_key']
end
In the /etc/nginx/sites-enabled/default I have following lines
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
server_name mydomain.com;
passenger_enabled on;
rails_env production;
root /home/deploy/myapp/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;
}
}
this is the nginx Error log
[ 2016-09-28 06:25:02.4500 1594/7f900ee89700 age/Sha/ApiServerUtils.h:794 ]: Log file reopened.
[ 2016-09-28 09:45:43.7508 1597/7f2326502700 age/Cor/CoreMain.cpp:819 ]: Checking whether to disconnect long-running connections for process 1978, application /home/deploy/hlinreykdal/current/public (production)
App 21337 stdout:
App 21405 stdout:
[ 2016-09-28 10:30:31.0631 1597/7f2326502700 age/Cor/CoreMain.cpp:819 ]: Checking whether to disconnect long-running connections for process 21405, application /home/deploy/hlinreykdal/current/public (production)
App 23240 stdout:
App 23308 stdout:
[ 2016-09-28 10:41:40.1769 1597/7f2326502700 age/Cor/CoreMain.cpp:819 ]: Checking whether to disconnect long-running connections for process 23308, application /home/deploy/hlinreykdal/current/public (production)
App 24329 stdout:
App 24397 stdout:
I have tried bundle exec rake assets precompile with out any luck.
I have deployed and restarted nginx again and again, with out any luck
I have tried almost every answer to similar questions here on stack overflow, but nothing seems to work.
Am I missing something here?
I'm sure this problem has something to do with the assets pipeline, but I'm not sure how to fix it.
Can anyone please take a look at this and advise me.
thanks in advance
Ok if anyone is experiencing a similar problem you might want to check out the config.assets.compile In my case I only had to change the config/environments/production.rb config.assets.compile from false, I changed it to true and now everything worked.... It only took me two days to figure it out :D
Note that public/assets is where the asset pipeline puts its stuff. If this is for static assets, I would put them in app/assets/images in order to use the asset pipeline, or choose another directory name.
I was using html's img tag to serve image assets as following
<img src="/assets/AdminLTELogo.png" alt="AdminLTE Logo" class="brand-image im">
Just had to change it to following and it worked.
<%= image_tag 'AdminLTELogo.png' , alt: "AdminLTE Logo", class: "brand-image im %>
Cheers.

Rails, Nginx, Passenger, SSL - File Uploads Not Working

I am not able to get uploads to work with Passenger-Nginx and SSL. Uploads work fine if I disable SSL.
Here is my Nginx site config:
server {
listen 80;
server_name www.mysite.in;
root /home/deploy/mysite/current/public;
passenger_enabled on;
passenger_ruby /home/deploy/.rvm/gems/ruby-2.1.4/wrappers/ruby;
passenger_user deploy;
}
server {
listen 443 ssl;
server_name www.mysite.in;
root /home/deploy/mysite/current/public;
passenger_enabled on;
passenger_ruby /home/deploy/.rvm/gems/ruby-2.1.4/wrappers/ruby;
passenger_user deploy;
ssl on;
ssl_certificate /etc/nginx/ssl/www_mysite_in.crt;
ssl_certificate_key /etc/nginx/ssl/myserver.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
#Disables all weak ciphers
ssl_ciphers 'AES256+EECDH:AES256+EDH';
# Specifies that server ciphers should be preferred over client (e.g. browser) ciphers when using SSL/TLS.
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.4.4 8.8.8.8 valid=300s;
resolver_timeout 10s;
passenger_set_cgi_param HTTP_X_FORWARDED_PROTO https;
}
Any config/environments/production.rb
Mysite::Application.configure do
# Settings specified here will take precedence over those in config/application.rb.
# Code is not reloaded between requests.
config.cache_classes = true
# Eager load code on boot. This eager loads most of Rails and
# your application in memory, allowing both threaded web servers
# and those relying on copy on write to perform better.
# Rake tasks automatically ignore this option for performance.
config.eager_load = true
# Full error reports are disabled and caching is turned on.
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
# Enable Rack::Cache to put a simple HTTP cache in front of your application
# Add `rack-cache` to your Gemfile before enabling this.
# For large-scale production use, consider using a caching reverse proxy like nginx, varnish or squid.
# config.action_dispatch.rack_cache = true
# Disable Rails's static asset server (Apache or nginx will already do this).
config.serve_static_assets = false
# 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
# Generate digests for assets URLs.
config.assets.digest = true
# Version of your assets, change this if you want to expire all your assets.
config.assets.version = '1.0'
# 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
# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
config.force_ssl = true
# Set to :debug to see everything in the log.
config.log_level = :info
# Prepend all log lines with the following tags.
# config.log_tags = [ :subdomain, :uuid ]
# Use a different logger for distributed setups.
# config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)
# Use a different cache store in production.
# config.cache_store = :mem_cache_store
# Enable serving of images, stylesheets, and JavaScripts from an asset server.
# config.action_controller.asset_host = "http://assets.example.com"
# Precompile additional assets.
# application.js, application.css, and all non-JS/CSS in app/assets folder are already added.
# config.assets.precompile += %w( search.js )
# 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
# Disable automatic flushing of the log to improve performance.
# config.autoflush_log = false
# Use default logging formatter so that PID and timestamp are not suppressed.
config.log_formatter = ::Logger::Formatter.new
# Do not dump schema after migrations.
config.active_record.dump_schema_after_migration = false
# config.action_mailer.delivery_method = :sendmail
end
I have tried disabling the config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' and it makes no difference under SSL or non-SSL.
I was never able to get this to work. So I just deleted the VPS image, and created a new one... and it works.
PS - I was using a setup script to install all the packages, so I am at a total loss as to why it didn't work with the original VPS image.

Resources