Thin Server - Virtual Path - ruby-on-rails

In production, we deploy our application to a virtual path:
https://hostname/my-virtual-path/
So the route '/users/' in development is actually accessed on https://hostname/my-virtual-path/ in production.
This means that routes are different between development (/users/) and production (/my-virtual-path/users/). Normally this is handled by setting the environment variable RAILS_RELATIVE_URL_ROOT or config.action_controller.relative_url_root so the paths Rails generates with its URL helpers is adjusted depending on the setting of these variables. Unfortunately, our assets are going to be pre-compiled BEFORE we know what this setting will be.
Is there a way to run a Rails server in development mode, using thin, that will serve files to a virtual path? For example, I want to type:
thin start --ssl -p 3000 --path whatever-i-want
and be able to access the root URL at https://localhost:3000/whatever-i-want in order to test my application.

I think you’re after the --prefix option. From the output to thin -h:
--prefix PATH Mount the app under PATH (start with /)
In your case something like this:
thin start --ssl -p 3000 --prefix /whatever-i-want
(Note you need to start the prefix with /.)

Related

How to change WEBrick :AccessLog option when running RedMine?

I'm running RedMine via WEBrick using the following command line (simplified):
bundle exec rails server webrick -e production -p 3012 -P '/var/lib/redmine/redmine.pid'
I don't like how WEBrick outputs the peer address at the beginning of its access log lines (because I'm running it behind nginx and the peer address is always 127.0.0.1), so I want to change the access log format.
I know that I need to tune the :AccessLog config option for WEBrick, but I don't know how to get my hands on it. WEBrick is run by the rails server command, via the rack abstraction, and I don't see an obvious way to pass the necessary configuration to WEBrick.
So, is there any way to do it? Some command line switch? -c is the only switch that accepts some kind of configuration file, but it references "rackup", and I have no idea how to work with it.
Maybe it can be done by changing configuration files? I tried to modify additional_environment.rb by adding config[:AccessLog] = [ [ $stderr, WEBrick::AccessLog::COMMON_LOG_FORMAT ] ], but it had no effect (although the file was executed), so I assume this file's config is not what is passed to WEBrick.
I'm pretty sure there is some way to configure this option without creating a new Rails application and invoking WEBrick manually, and hopefully even without changing RedMine files.

Rails - switch environment to production automatically

I am doing a project in rails, and i just run this project in console by following command:
$ rails server
But it runs in development mode. I want when i run "rails server", it runs in production mode, what script must i edit to set the environment ?
Easy:
rails server RAILS_ENV=production
Or:
rails s -e production
Or you meant without this extra thing? If that so, it depends on which server you use. You can install Puma for example, and add config file, in which you can specify the default environment.
This question could help in case of set rails env for ngnx or passenger.
All the possible operations on the rails server
-p port: Specify the port to run on
-b ip: Bind to a specific IP address
-e name: Use a specific Rails environment (like production)
-d: Run in daemon mode
-h: Display a help message with all command-line options

Different ENV['RAILS_RELATIVE_URL_ROOT'] per environment

For sake of a simple example, let's say I want to deploy my dev and test environments to different subfolders. For example http://www.example.com/dev and http://www.example.com/test.
I'm using Thin, so I can easily set the relative path prefix by starting Thin with the following:
thin start --prefix /dev -e development
thin start --prefix /test -e test
However, my path helpers still generate paths that do not include the path prefix. So I end up with something like /images/logo.jpg for both environments. I'd like to end up with /dev/images/logo.jpg and /test/images/logo.jpg.
I can hard-code something like this in my config/environment.rb
ENV['RAILS_RELATIVE_URL_ROOT'] = "/dev"
But that obviously doesn't dynamically associate the relative root with the environment.

phusion passenger not seeing environment variables?

We are running ubuntu servers with Nginx + Phusion Passenger for our rails 3.0x apps.
I have an environment variable set in /etc/environment on the test machines:
MC_TEST=true
If I run a console (bundle exec rails c) and output ENV["MC_TEST"] I see 'true'.
But, if I put that same code on a page ( <%= ENV["MC_TEST"] %> ) it doesn't see anything. That variable does not exist.
Which leads me to question:
1 - What is the proper way to get environment variables into passenger with nginx (not apache SetEnv)?
2 - Why does Passenger not have a proper environment?
Passenger fusion v4+ enables reading of environment variables directly from bashrc file. Make sure that bashrc lives in the home folder of the user under which passenger process is executed (in my case, it was ubuntu, for ec2 linux and nginx)
Here is the documentation which goes into details of bashrc
I've the same problem with you when use passenger with nginx and nginx init script on ubuntu.
The reason is that I use sudo service nginx restart(installed by init script) to start up nginx and
it was running by root and the root did not get your login user environment variable.
There are two solutions for this.
One is running nginx manually.
sudo service nginx stop
sudo -E /path/to/your/nginx
one is add env to your nginx init script
export MC_TEST=true
The latter solution is somehow ugly, But it works. And I think the better way is found a configuration to tell the init script to preserve the login user env.
I got another ugly solution.
env_file = '/etc/environment'
if File.exist?(env_file)
text = File.open(env_file).read
text.each_line do |line|
key, val = line.split('=', 2)
ENV[key] = val.strip
end
end
With nginx you can use the variable passenger_env_var to it. See an example below
passenger_env_var GEM_HOME /home/foo/.rbenv/rubygems;
passenger_env_var GEM_PATH /home/foo/.rbenv/rubygems/gems;
So for your case
passenger_env_var MC_TEST true;

how to map a Rails app to a certain URL path?

Hey, Guys
I'm now learning starting up the Rails on my VPS server, Now I can visit my app rails my thin server by a 3000 port number, something like this http://mydomain:3000,
But I want to map this app to the url like http://mydomain/railsapp1, so when I add a railsapp2 for testing purpose, it won't mess up my railsapp1.
Should I add something in the thin configuration file? or I should use nginx?
Are you open to using Passenger (ModRails)? You could then use Nginx and setup your Rails apps under different subdirectories.
General information for installing Passenger in Nginx can be found here: http://www.modrails.com/install.html
You can see more information here on setting up Rails in subdirectories: http://www.modrails.com/documentation/Users%20guide%20Nginx.html#deploying_rails_to_sub_uri
You could just start railsapp2 on port 3001 if you want to have both running at the same time
Rails 2
script/server -p 3001
Rails 3
rails server -p 3001

Resources