Rails 5 development server - access from the outside - ruby-on-rails

I have an android phone and I need to send some requests to a rails server.
I do the usual stuff by picking up the ip address from ifconfig then using that ip address on port 3000.
It does not work. Even if I run something like wget ip_address:3000 it does not.
Is it because the port 3000 is blocked ?

The Rails server only binds to localhost, you need to use the -b option, eg. rails s -b 0 This will bind to 0.0.0.0 (ie. all addresses). My answer here goes into more detail: https://stackoverflow.com/a/29084070/152786

Related

Make Rails Server port 3000 accessible via domain name (AWS EC2, Cloudflare)

I have a development environment that I want accessible via a specific domain name ie. "example.com". It's a legitimate development environment (the whole puma +sqlite 3 RoR starting kit) that I want to make to be able to access using the domain name.
Currently the elastic IP from AWS EC2 is accessible by appending :3000. IPAddress:3000 works (ec2-NUMBERSHERE.ap-southeast-1.compute.amazonaws.com:3000 via web browser). I start this by running screen bundle exec rails server -b 0.0.0.0 Yes, I am running my development environment in an AWS EC2. This is being done on purpose.
In my Cloudflare account I have mapped the Type A to the Name "example.com" and content to the elastic public ip. I have mapped Type CNAME to the Name "wwww" and the content to the public DNS.
How do I achieve the same as IPAddress:3000 with example.com?
**I am aware of the best practices that this is ignoring but the question is really just this.
I think you can't do anything at a DNS level. What you can do is change the rails server default port using the -p option:
$ screen bundle exec rails server -b 0.0.0.0 -p 80
But then, you'll need to run the rails server with root permissions in order to use the 80 port (and any other port below 1024).
A better option is to bind the rails server at localhost (which is the default behaviour) and add Nginx as a reverse proxy listening in port 80 using the proxy_pass feature, something like this:
server {
listen 80;
listen [::]:80;
server_name example.com;
location / {
proxy_pass http://localhost:3000/;
}
}
I was able to do it by forwarding port 80 traffic to 3000 using iptables.
sudo iptables -t nat -I PREROUTING -p tcp --dport 443 -j REDIRECT --to-ports 3000\
After that running screen bundle exec rails server -b 0.0.0.0 worked!

Ruby on Rails. How to run two servers (different apps) on Amazon Ec2 at same time?

I am triyn to deploy two different rails app in one Ec2 i can run one each time and work ok, but i need the 2 app running at same time and that can be accessed from anywhere not only localhost,I enable (add rule) two tcp port 3000 and 3001, this is my try:
/path/app1$ rails s -d
/path/app2$ rails s -b0.0.0.0 -p 3001 -d
this is the ps -ef command output
dev+ 3028 1 0 17:10 ? 00:00:00 puma 3.11.2 (tcp://localhost:3000) [/]
dev+ 3160 1 0 17:14 ? 00:00:00 puma 3.11.3 (tcp://0.0.0.0:3001) [/]
also try to run app1 with -b0.0.0.0 so it can listen from anywhere, but same result: only 1 app is listening on 0.0.0.0.
What I am missing? How can I run two servers at same time and listen both on 0.0.0.0.
thanks
By default, according to the Rails documentation, the server will only listen on the localhost / loopback interface. This is actually confirmed in the output snippet that you posted.
In the first command for app1, you aren't telling it to listen on 0.0.0.0, so you'd need to change your first command to:
/path/app1$ rails s -b0.0.0.0 -p 3000 -d
Both applications can listen on 0.0.0.0, but they can't share the same port. You have already configured app1 to listen on port 3000 (Rails default), and app2 to listen on port 3001, so they should both coexist peacefully, once you make the change above.
See also: What does binding a Rails Server to 0.0.0.0 buy you?

How to access the localhost ruby on rails server in internet?

I m using railsinstaller
http://localhost:3000/wage
Next page
http://localhost:3000/wage/results?hours=23
I try to access
myip:3000/wage
But it shows ERR_CONNECTION_REFUSED
I get the myip from ipconfig-->ipv4
Is the url wrong or configuration problem?
You've started well, but for local LAN only.
For intranet only, you can use your LAN ip.
Test it from another local machine (same LAN). Hope it works.
If your firewall and machine settings allow you to view on intranet, than go to next step:
For internet,
first make sure you have your public internet address, by visiting http://api.ipify.org/ > you will see your public IP address. Use it to access your website http://<public_address>:3000/
If it works on intranet, but not on internet, you need to open the port 3000 in your router and forward it to your local machine. (If you have no access to router settings, ask your administrator to help you with this).
Give me more details for a more complete solution.
What OS are you using?
EDIT:
For Windows10 you have to make sure to open the port 3000 in your firewall.
Navigate to Control Panel, System and Security and Windows Firewall.
Select Advanced settings and highlight Inbound Rules in the left pane.
Right click Inbound Rules and select New Rule.
Add the port you need to open and click Next.
Add the protocol (TCP or UDP) and the port number into the next window and click Next.
On the "Profile" step select all 3 (Domain, Private, Public)
Select Allow the connection in the next window and hit Next.
Select the network type as you see fit and click Next.
Name the rule something meaningful (like: Allow incoming connections on port 3000) and click Finish.
To change the default 3000 port use:
rails server -p 8080 to change port
rails server -b 0.0.0.0 to bind 0.0.0.0 address
UPDATE:
When server starts will output to the console, the address:port it listens. example:
Listening on tcp://0.0.0.0:3000 as a development server it can be set to listen on localhost requests only.
To override that settings use:
rails server -p <3030> -b 0.0.0.0 this will listen to all incoming connections on port 3030
For more details on the -p, and -b consult the help:
$ rails server -h
Usage: rails server [mongrel, thin etc] [options]
-p, --port=port Runs Rails on the specified port.
Default: 3000
-b, --binding=IP Binds Rails to the specified IP.
Default: localhost
-c, --config=file Uses a custom rackup configuration.
-d, --daemon Runs server as a Daemon.
-e, --environment=name Specifies the environment to run this server under (test/development/production).
Default: development
-P, --pid=pid Specifies the PID file.
Default: tmp/pids/server.pid
-C, --[no-]dev-caching Specifies whether to perform caching in development.
true or false
-h, --help Shows this help message.

Ruby Rails callback URL in development

I am integrating Plivo in to my application(Ruby/Rails) and I would like to setup the router to point to my local dev environment for the callback. I have setup my controller method: plivo/receive_sms and the associated route in routes.rb
My question is: How do I setup my router(NETGEAR) to receive the callback for localhost:3000/plivo/receive_sms from Plivo?
I have tried forwarding port 80 to my local IP (192.168.1.20) But that did not work.. So I added ports 3000 - 8080 with no success.
That should work:
Make sure you are forwarding external port 80 to your local port 3000.
Make sure the Plivo is configured with your OUTGOING public IP address as the call back URL hostname. (google for "What is my ip"? and google will tell you)
If that doesn't work, perhaps your computer is firewalled? Or your router / ISP firewalls port 80?
If #3 is the case, then try forwarding port 3000 to your local port 3000, but then you have to be able to change the callback URL (#2) to use port 3000.
Or - try:
localtunnel.me
localtunnel gem
PageKite
ShowOff
https://pagekite.net/2011-04-20/PageKite_meets_Showoff
http://devblog.avdi.org/2012/04/27/http-forwarding-services-for-local-facebook-development/
Or if you have access to a server thats sits somewhere on the internet, there are gems similar to local tunnel that will use your own server to do the proxying.
Or you can do it manually with SSH using the -R or -L option (I forget which).

What is the difference between starting a server binding to 0.0.0.0 vs 127.0.0.1?

It seems that Rails and Django can both start up the server binding to an IP, either
0.0.0.0, 127.0.0.1, or 192.168.1.2 <-- the last one is my local IP
are there other choices? It seems that 0.0.0.0 and 192.168.1.2 can let a Virtual PC on the same machine access this website, while 127.0.0.1 cannot.
However, if it is just the same notebook, using localhost:3000 (for Rails), then it doesn't matter. But either case, 0.0.0.0:3000 won't work.
How does it work? What are the meanings of using 0.0.0.0 vs 127.0.0.1 vs 192.168.1.2?
Binding to 0.0.0.0 means to listen to all interfaces.
Binding to 127.0.0.1 means to listen to the loopback interface.
0.0.0.0 binds to all IP addresses. Any other address binds to that particular interface.
If you start the server on 127.0.0.1, it is only listening on localhost; if you start it on 0.0.0.0, it is listening on any IP (eg your local IP).
For local dev, this is fine - but otherwise, no one but the local machine will be able to see it.

Resources