When I'm on my development laptop, I want to go to the URL "mysite.loc" instead of "localhost:3000" for a particular project. How can I do this?
I am using Ruby on Rails 4, and the default WEBrick server.
I have tried adding "127.0.0.1 mysite.loc" to my /etc/hosts file.
Some people suggest installing Fiddler
Add your local sitename to the HOSTS file then add a custom rule to Fiddler.
static function OnBeforeRequest(oSession: Session) {
if (oSession.host.toLowerCase() == "mysite.loc") {
oSession.host = "mysite.loc:3000";
}
}
Then when you navigate to http://mysite.loc it should be proxied to http://mysite.loc:3000.
Related questions:
ServerFault
StackOverflow
You can add a definition to your /etc/hosts file, but you will still have to use port 3000 in the URL, unless you also change this to the HTTP default (80). You could, if you really wanted, just run on port 80
sudo rails s -p 80
Not that binding to port 80 generally requires su privilege - hence the use of sudo (if available).
If you want to get straight to the desired result, with the assistance of some programming magic there is
http://pow.cx/
It does some local DNS and port magic to let you do what you are wanting.
Related
In development, I would like to replace localhost:3000 to something like "domain.com:3000" or "domain.com". I can achieve this by adding alias to /etc/hosts file, for ex:
# /etc/hosts
127.0.0.1 domain.com
^^^ this one works, i can now view my site locally using domain.com.
The problem is that rails itself still uses localhost:3000, for example when generating urls via router methods. I feel that there should be some config for this.
You have to set:
Rails.application.routes.default_url_options[:host] = 'domain.com:3000'
in your development environment.
Custom domain name for your IP Address can be created.
First of all find your IP Address using ifconfig.
Then open /etc/hosts file with sudo.
/etc/hosts file is in readonly, so need to open with sudo
sudo vi /etc/hosts
There you will find at least two entries for 127.0.0.1 and 127.0.1.1, below this, create your own domain name
<IP_Address> domain.com
Save the file.
Then start your rails server and bind with your IP Address.
rails s -b <IP_Address>
And you are done.
In browser,
<IP_Address>:3000 will be same as domain.com:3000
We're using Passenger 4.0.59 behind Apache 2.2 (CentOS 6.latest) with Rails 3.2.
In /etc/httpd/conf/httpd.conf we have:
TraceEnable off
We have one virtual host configured in httpd.conf and a second virtual host configured in /etc/httpd/conf.d/ssl.conf that's configured with Passenger.
I'm using commands of this form to test:
curl -I -X {method} https://{host}/{resource}
...and seeing the following behavior:
When I TRACE a static image over http, i.e. http://host.domain.com/images/foo.png, I get a 405 response (as expected).
When I TRACE the same static image over https, meaning it goes through the virtual host configured with Passenger, I get 405 (as expected).
However, when I TRACE a Rails service in our app, e.g. https://host.domain.com/status.json, I get a 200 response w/ valid data.
I would expect Apache to shut down the request and return a 405 response before it even gets to Passenger/Rails, but that isn't happening.
What am I missing / misunderstanding?
What am I missing / misunderstanding?
TraceEnable off is the correct directive to use, but you may have another TraceEnable directive elsewhere in your configs.
You should check all of your apache config files to be sure there is no other TraceEnable directives.
Since the TraceEnable directive can be used within either the server config or the virtual host config, so you may just want to add it to both.
I cloned a repo and am trying to get it to work. The app is very heavily dependent on subdomains. For example, signin.app.com or company1.app.com.
However, since I am trying to get it to run on my local machine, I can't simply do signin.localhost:3000
So, how do I get these subdomains to work using localhost?
Thanks!
As Gene said,
/etc/hosts file:- (add)
127.0.0.1 subdomain1.localhost
And on Rails3 this is treated as a domain, so you need to just add this:
127.0.0.1 subdomain1.localhost.local
then try,
http://subdomain1.localhost:PORT
In linux
open command prompt
>sudo vi /etc/hosts
Add a line in file
127.0.0.1 subdomain.hostname.com
and press Esc and :wq (means save host file)
thats it you type subdomain.hostname.com in your browser. Subdomain will run in localhost.
You could add the domain names to your hosts file, routing them to localhost.
I am new to ruby, can any one help me in explaining "how to change http://www.myexample.com:3000 to http://wwww.myexample.com in ruby?"
require 'uri'
uri = URI.parse('http://www.myexample.com:3000')
uri.port = nil
uri.host.sub!('www', 'wwww')
uri.to_s # => http://wwww.myexample.com
Depends on what web server you're using, but in general, try adding a -p 80 option to the command line when starting up the web server.
For example, with the Thin web server, you'd run: thin -p 80 start.
Assuming your using webrick, pass --port 80 on the command line when running your server
port 3000 is the rails server default port. You can avoid this 3000 by getting your rails app behind apache/ passenger (normally the production setup)
By that way apache and passenger will do the routings and you can view your app at
http://www.example.com
Following will be helpful
passenger - http://www.modrails.com/
rails casts - http://railscasts.com/episodes/122-passenger-in-development
hope this helps
cheers
sameera
So I would like my Rails app instances to register themselves on a "I'm up" kind of thing I'm playing with, and I'd like it to be able to mention what local port it's running on. I can't seem to find how to do it - in fact just finding out its IP is tricky and needs a bit of a hack.
But no problem, I have the IP - but how can I find what port my mongrel/thin/webrick server is running on?
To be super explicit, if I start a rails app using script/server -p 3001, what can I do to pull that 3001 inside the app.
You can call Rails::Server.new.options[:Port] to get the port that your Rails server is running on. This will parse the -p 3001 args from your rails server command, or default to port 3000.
From inside any controller action, check the content of request.port, thus:
class SomeController < ApplicationController
def some_action
raise "I'm running on port #{request.port}."
end
end
Two ways.
If you're responding to a request in a controller or view, use the request object:
request.port
If you're in an initialiser and don't have access to the request object use the server options hash:
Rails::Server.new.options[:Port]
I played around with this a bit, and this might be the best solution for Rails 5.1:
Rails::Server::Options.new.parse!(ARGV)[:Port]
Building on the other answers (which saved my bacon!), I expanded this to give sane fallbacks:
In development:
port = Rails::Server::Options.new.parse!(ARGV)[:Port] || 3000 rescue 3000
In all other env's:
port = Rails::Server::Options.new.parse!(ARGV)[:Port] || 80 rescue 80
The rescue 80 covers you if you're running rails console. Otherwise, it raises NameError: uninitialized constant Rails::Server. (Maybe also early in initializers? I forget...)
The || 80 covers you if no -p option is given to server. Otherwise you get nil.
For Rails 5.1 development server.
if Rack::Server.new.options[:Port] != 9292 # rals s -p PORT
local_port = Rack::Server.new.options[:Port]
else
local_port = (ENV['PORT'] || '3000').to_i # ENV['PORT'] for foreman
end