Ruby on Rails WEBrick SSL connection error - ruby-on-rails

Please help to implement ssl
rails version - 3.2.8
I edtited following files:
# Gemfile
gem 'rack-ssl'
# config/application.rb
require 'rack/ssl'
config.middleware.use Rack::SSL
I also tried to use
# config/application.rb
config.force_ssl = true
But it shows
SSL connection error
when I access mysite:3000/
But it shows normal page if going to https:mysite
Please help,
thanks,
D

According to this:
How to use deactivate Webrick's SSL
The issue is caused by config.force_ssl = true. Even if you remove that, which you may not want, you might still have issues with WEBrick giving you this error. You could try clearing cookies, but that still might not work.
A better alternative, if it's an option for you, would be to switch to using the thin server:
group :development do
gem "thin"
end
Then:
$ bundle
$ thin start --ssl
See also https://stackoverflow.com/a/11614213

Related

Heroku: Rails 5 ActiveCable configuration issue. Error during WebSocket handshake: 'Upgrade' header is missing

I`ve been implementing a simple chat app with Rails 5 and ActiveCable. Locally this app works great. But when I try to test it on heroku, I get in JS console the following:
'Error during WebSocket handshake: 'Upgrade' header is missing'
What I did:
I followed this guide.
I set the cable.yml
production:
adapter: redis
url: redis://rediscloud:url_here
I set the URL in production.rb
Rails.application.configure do
config.web_socket_server_url = "wss://my-app.herokuapp.com/cable"
I set the allowed hosts.
config.action_cable.allowed_request_origins =
['https://my-app.herokuapp.com',
'http://my-app.herokuapp.com']
But this doesn`t help. Any ideas why this error happens or how can I fix it?
Regards.
The problem was in the server selected as default.
The default server server was Thin. It was sending incorrect reply.
I`ve changed the server back to puma according to this guide and no code changes more needed to fix the issue.
After Struggling a lot with the same issue we even started thinking to change the default server from thin to Puma.
But we finally got the answer to this :)
ActionCable does work w/ Thin now w/ a few extra configs:
In your Gemfile:
gem 'faye-websocket'
gem 'thin'
Create a config/initializers/thin_action_cable.rb:
Rails.application.config.action_cable.use_faye = true
Faye::WebSocket.load_adapter 'thin'
this link helps me to fix that issue.
ActionCable not working with Thin #23696

Dokku Rails application missing static assets with 404 error

I deployed a rails application with Dokku. Everything went fine except that all my assets including images returns a 404 error.
I really don't know how to debug this.
For dokku it is simple to add:
dokku config:set <your-app> RAILS_SERVE_STATIC_FILES=true
I had exact same issue and adding the following gem solved it:
gem 'rails_12factor', group: :production
That's the official way of dealing with that issue on Heroku for Rails 3 and 4 (and that gem is made by Heroku team as well).
On rails 5, try making the changes in production.rb that are suggested on this page rails_12f
Finally I just added this :
config.serve_static_files = true
in environements/production.rb
Note: flag deprecated in Rails 5.0 in in favor of config.public_file_server.enabled

Why is Rails not writing to development.log

Suddenly my Rails 4.2 app is writing nothing to development.log.
Absolutely nothing!
I have tried checking the correct environment is being used, restarting the server, checking for gems that might interact with logger, checking permissions, logger.flush, and rake log:clean.
The log file is now completely empty, and nothing is being written.
Running rails s gives:
=> Booting WEBrick
=> Rails 4.2.0 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2015-03-14 18:49:57] INFO WEBrick 1.3.1
[2015-03-14 18:49:57] INFO ruby 2.1.5 (2014-11-13) [x86_64-darwin14.0]
[2015-03-14 18:49:57] INFO WEBrick::HTTPServer#start: pid=3963 port=3000
There are quite a few similar questions on Stackoverflow, but none provide an answer that has worked in my case.
I have no idea what would cause this or how to systematically debug.
What steps should I take?
OK, after much going round in circles, I finally found the culprit.
The rails_12factor is apparently overwriting config.logger.
See: https://github.com/heroku/rails_stdout_logging/blob/master/lib/rails_stdout_logging/rails3.rb#L7
I removed this gem from the development environment (I really only need it in the deployed environment) and everything is now working.
To put a gem in production only, it needs to be in a gem environment group like this:
# This is override development.log output and should only go in production.
group :production do
gem 'rails_12factor'
end
Thanks #maxd for helping to get me thinking in the right direction.
I think your problem can be caused by the following things:
You set to high log level. Check your application.rb and environments\*.rb files:
# Set to :debug to see everything in the log.
config.log_level = :error
You set custom logging. Check your application.rb, environments/*.rb and all files in initializers/*.rb:
# Use a different logger for distributed setups.
config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)
These two steps should help you to investigate problem and fix it.
Add the following line to config/environments/development.rb
config.log_level = :debug
restart server
If in AWS (or similar) you have to make sure that your log directory has write access.

production.log empty on Rails 4 / Capistrano / Passenger / Nginx server (digital ocean)

I have set up a rails 4 server on Ubuntu 12.04 using Capistrano, Nginx, Passenger, Postgres, Redis/Resque
Everything is working great, except that the production.log file is always empty.
I have tried a variety of configuration changes in production.rb to no avail.
It's definitely not a permissions issue, as the permissions on both the log dir and each of the logs are wide open (777)
Can anyone hep me figure out how to get basic logging working?
The culprit was Heroku's rails_12factor gem
Removing that gem from the Gemfile, now the logs are working as expected.
# group :production do
# gem 'rails_12factor'
# end
To clarify, the rails_12factor gem was responsible, but that's only because it includes rails_stdout_logging, which is the real culprit, however, due to it's intended behavior to "ensure that your logs will be sent to standard out."
Check with the log levels in production.rb file, config.log_level = :debug will display it's errors. Also make sure the server is running production mode, in case you have not made any changes any configuration files for rails env, production mode is by default.

How to change the default rails server in Rails 3?

I'm new to Rails and I'm wondering if there is an option to change the default rails server, i.e., webrick, for another one such as 'puma' or 'thin'. I know it is possible to specify which server to run with 'rails server' command, however I would like to use this command without specify the name of the server so it can run the default rails server. Is there a way to change the default rails server into a configuration file or something like this? Thanks in advance for your help!
Based on James Hebden's answer:
Add Puma to gemfile
# Gemfile
gem 'puma'
Bundle install it
bundle
Make it default, paste this code into script/rails above require 'rails/commands':
require 'rack/handler'
Rack::Handler::WEBrick = Rack::Handler.get(:puma)
So script/rails (in Rails 3.2.12) will look like:
#!/usr/bin/env ruby
# This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
APP_PATH = File.expand_path('../../config/application', __FILE__)
require File.expand_path('../../config/boot', __FILE__)
require 'rack/handler'
Rack::Handler::WEBrick = Rack::Handler.get(:puma)
require 'rails/commands'
Run server
rails s
=> Booting Puma
Rack (the interface between rails and a web server) has handlers for the default WEBrick, and also for Thin. If you place the following in your Gemfile in the root of your rails project
gem 'thin'
rails server will automatically use Thin. This has been the case since 3.2rc2.
This unfortunately only applies to Thin, as Rack does not have built-in support for Unicorn, and others.
For servers that have Rack handlers (again, sadly Unicorn does not), you can do a bit of a hack to get rails server to use them. In your scripts/rails file in the root of your rails project, you can add the below just above `require 'rails/commands'
require 'rack/handler'
Rack::Handler::WEBrick = Rack::Handler::<name of handler class>
This essentially resets the handler for WEBrick to point to the handler for the server you would like to use.
To get an understanding of the supported Rack handlers, take a look at the comments in the source: https://github.com/rkh/rack/blob/master/lib/rack/handler.rb
I think rails simply passes on the server option provided to rack. Rack has the following logic to determine what server to run:
https://github.com/rack/rack/blob/master/lib/rack/server.rb#L271-L273
def server
#_server ||= Rack::Handler.get(options[:server]) || Rack::Handler.default(options)
end
The first case is when a :server option was passed to the rails server command. The second is to determine the default. It looks like:
https://github.com/rack/rack/blob/master/lib/rack/handler.rb#L46-L59
def self.default(options = {})
# Guess.
if ENV.include?("PHP_FCGI_CHILDREN")
# We already speak FastCGI
options.delete :File
options.delete :Port
Rack::Handler::FastCGI
elsif ENV.include?("REQUEST_METHOD")
Rack::Handler::CGI
else
pick ['thin', 'puma', 'webrick']
end
end
Thin and Puma should be automatically picked up. The fallback is Webrick. Of course other web servers could override this behavior to make them the first in the chain.
If your Webserver is not picked up by default you could monkey-patch the default method to work like you want it. Of course this could break in future versions of rack.
Rack will now look at a RACK_HANDLER environment variable file to see if you've specified a default rack handler. You can add a line like this to your .env file to set the default if you're using dotenv, or specify the assignment from the command line.
`RACK_HANDLER=webrick`
This should work as of this pull request:
https://github.com/rack/rack/pull/590
I wouldn't get hung up on specifically using the rails server command. Just install whichever gem you want and alias the command (e.g. rails s Puma) to something simple like rs.
If you want unicorn/thin/etc, just add the gem to your gemfile
i.e. gem 'unicorn', gem 'thin', etc. then run bundle install at the command line.
As far as I can tell, adding either of these gems runs the appropriate server via rails server
UPDATE
Apparently this only works for Thin or Puma.
If you have thin in your Gemfile, you need to do this:
require 'rack/handler'
Rack::Handler::Thin = Rack::Handler.get(:puma)
If you use bash run: export RACK_HANDLER=webrick

Resources