How to connect to a localhost Rails site via mobile device locally? - ruby-on-rails

I have a Rails site that I'm developing on localhost Ubuntu and I have a mobile.
My site is running on http://localhost:3000.
I want to access this directly via my mobile browser not going through the internet.
Is there any way to access it via WiFi or some other way?

If your computer is accessible from internet, just enter in your mobile browser:
http://your.ip:3000/
You could also create a local network (via wifi for instance), connect to it with your cell phone and then do the same thing.
If you are using Rails 4.2+, start the server using:
rails server -b 0.0.0.0
(see http://edgeguides.rubyonrails.org/4_2_release_notes.html#default-host-for-rails-server).

1.) get your local ip by typing:
ifconfig |grep inet
into your shell/terminal. your ip usually looks like this: 192.xxx.xxx.xx
2.) then start the rails server with:
rails server -b 0.0.0.0
3.) your application can now be reached by typing:
192.xxx.xxx.xx:3000 into the browser.

Using ngrok
So basically, you run the command
./ngrok http 3000
And you will see the output like that
And all you need is copy the highlight URL and paste to the browser.

Execute
rails s -b IP_ADDRESS
Access mobile: IP_ADDRESS:3000

First find your Ip and note it down by typing ifconfig terminal(you should be connected to wifi or network)
ifconfig
than run the rails server by typing
rails server -b 0.0.0.0 (YOUR IP )
and press Enter
SERVER WILL BE RUNNING ON YOUR_IP:3000
2. Now connect your mobile with the same network and open url
https://YOUR_IP:3000
CONGRATS YOU ARE RUNNING LOCALHOST ON MOBILE

On rails-server machine:
Make sure you're running rails-server.
Get your IP address, I got "192.168.1.112" for example.
On mobile:
Access through browser by using:
http://<your_ip_address>:3000
http://192.168.1.112:3000
NOTE: Your mobiles need to have the same network with rails-server machine.
To get yourself IP address, run this command in your rails-server machine.
MacOS or Linux Terminal:
ipconfig getifaddr en0 || ipconfig getifaddr en1
# => 192.168.1.112
Description:
ipconfig getifaddr en0 is for wifi connections
ipconfig getifaddr en1 is for wired connections
Windows:
ipconfig
Then looking for IPv4 Address.
Running rails-server
On rails-server machine, it's okay to regularly run:
bin/rails s
If you found issue, you can try the following:
bin/rails s -b 0.0.0.0
Note: 0.0.0.0 is not an IP address, it's a shortcut for the system binding call to use all available IP addresses including 127.0.0.1, localhost.
Access through http://localhost:3000/

if you get ipconfig command not found on ubuntu
Try:
ip route to get the IP address then you can run rails server as explained in other answers.
rails s -p 3000 -b 192.168.0.102 (Replace IP with your system's IP).
Now you should be able to access on you mobile browser by just entering
http://192.168.0.102:3000/

run your server on 0.0.0.0:port_no rather then 127.0.0.1 or localhost
then check your IP address by typing a command in your terminal
ipconfig
here you can check your host machine's IP address.
-Now we have to enable 3000 in your case port to be accessible in the network
write following commands
sudo ufw enable
sudo ufw enable port_no
sudo ufw status
then you will be able to see your port is enabled
-Now you can access your server in any device browser using IP address:port_no
Check out this picture where I am accessing Django Web App running locally on Ubuntu and I am able to access it on my windows laptop which is in the same network via ip_addess:port_no which in my case is 8000
You can access the same website at

Related

nginx proxy_pass connection refused for flask development server [duplicate]

I'm trying to run a simple web server on a Raspberry Pi with Flask. When I run my Flask app, it says:
running on http://127.0.0.1:5000/
But when I enter this address on my laptop's in Chrome, I get
ERR_CONNECTION_REFUSED
I can open 127.0.0.1:5000 on the Raspberry Pi's browser. What do I need to do to connect from another computer?
Run your app like this:
if __name__ == '__main__':
app.run(host='0.0.0.0')
It will make the server externally visible. If the IP address of the machine is 192.168.X.X then, from the same network you can access it in 5000 port. Like, http://192.168.X.X:5000
when you are running the server via flask run change it to flask run --host=0.0.0.0
to connect, find the IPV4 address of the server that your script is running on. On the same network, go to http://[IPV4 address]:5000
A reason could also be in firewall refusing incoming connections on port 5000. Try:
sudo ufw allow 5000
app.run(host='0.0.0.0',port=5000)
if you run your app in this way then your server will be visible externally.
Steps by Setp:
Run your app by using the following command
app.run(host='0.0.0.0',port=5000)
Go to the window cmd . Type ipconfig and get the get the IPV4 adress suppose your IPV4 address is 192.168.X.X
Go to the mobile browser and type the 192.168.X.X:5000
If you have debug = True inside your app.run(), then it will not be visible to other machines either. Specify host and port inside app.run() without the debug = True.
You will have to run the development server such that it listens to requests on all interfaces and not just the local one
Ask Flask to listen on 0.0.0.0:PORT_NUMBER
or any other port you may choose
On MacOS 12.4 (Monterey) I couldn't load localhost nor my local IP but it worked with both of these:
0.0.0.0
127.0.0.1
Just change the URL in the browser if it loads with "localhost".
Both devices must be connected to same network.
Use app.run(host='0.0.0.0',port=5000) and run with your own Ipv4
address like his http://[Your Ipv4 address]:5000
If you are connecting this with android app then don't forget to
put INTERNET permission in manifest file.
Contrary to popular believe 127.0.0.1 is not the same a localhost.
I solved the issue above by setting 127.0.0.1 explicitly on both ends.
Well i was also here to know answer but i guess i found out problem. The reason is that you may not activated or run flask that's why it is showing error. For that you have to start from terminal "flask run" and then surely your flask will run...
The issue may occur, if VPN is on, so try to switch it off.

Accessing mac localhost from an iPhone (iOS 10)

I've tried from all Stack Overflow's existing answer for this topic but they're not working for me. I've tried using http proxy with Squidman, created local network between my iPhone and my mac with static IP, then accessing it using my machine IP address. I've also tried ping my mac using my Windows pc, it can't ping to my mac but my Mac can ping my Windows, both firewall are disabled.
Any configuration that I missed?
if your desktop is (192.168.0.101), you need to run your service as 0.0.0.0:8080 (example port). Usually ports are bound to 127.0.0.1
This way your router can direct requests from 192.168.0.101:8080 to your application. Ensure you undo this in case you exposing sensitive data.
Assuming your mac ip is $ip, you can do the following to make port $port accessible from your local network:
$ ssh -R $ip:$port:localhost:$port localhost
It will relay all requests from $ip:$port to the localhost server.
Example:
$ ssh -R 172.16.0.111:3000:localhost:3000 localhost
(Posted on behalf of the OP)
The main problem that causes my Mac can't be accessed by other device is the firewall blocked ICMP. Then I disabled it using this command below:
pfctl -d
My firewall rules list:

Connecting rails server from local machine to network

I have made a rails application using 4.2.0 in my local Ubuntu machine. I want to deploy that application to local network of my campus. How can I do that?
I checked my ip from ifconfig, and deployed the webrick server via binding that ip and different ports too,
rails server -b my_ip -p port
the console says that the application is running in that ip and port but when I am trying to connect it via my browser, I get service timout error. Can anybody suggest me a method to do that?
Binding to 0.0.0.0 tells the service to bind to all IP addresses on your machine. Rails server used to do this by default, but with 4.2 changed to binding only to localhost.
Basically if it's only bound to localhost then it will only respond locally to either localhost or 127.0.0.1 which can't work through a DNS service because it's not a public IP address.
When you use 0.0.0.0 it will bind to localhost and to your routable IP address.
Use rails s -b 0.0.0.0

No access to a rails server running behind a firewall

I am trying to run a ror application at a friend's home and the application is not reachable from the outside. Here is what we have tried.
The application has been created with
$ rails new <application name>
and started with
$ rails server
The development server starts and is reachable on http://localhost:3000
Next thing we have activated port forwarding on the router he uses to connect to the internet: we have mapped the external port 3000 to port 3000 on the internal host on which the rails server is running. When I try to open the page from the outside (with firefox) I get an error page: unable to conenct to host, the host may be down, try to connect later.
Things we have tried:
Changed port forwarding 80 -> 80
Installed and started Apache: the default Apache page is reachable.
Stopped Apache and started the rails server on port 80 (with sudo, otherwise it can't use port 80): the rails server is not reachable.
I have also checked the firewall of the machine running the rails server. The OS is ubuntu 12.04: the firewall (at least ufw) is not active. I can also connect to that machine using port 22 / ssh.
Summarizing:
rails server is accessible on port 3000 on local host
port forwarding works for ports 22 and 80 and both ssh server and apache server are reachable
rails server is NOT reachable on both port 80 and 3000 from the outside
The only thing I can think of is that rails might have a mechanism similar to Django's ALLOWED_HOSTS and that it refuses external requests because they are for a different host than localhost. However, I haven't found anything on this topic. Also, the application's log files do not show any connection attempt that was refused by the rails server.
So we are out of ideas. What should we check next?
Prior to Rails 4.2, the default for rails server was to bind to all interfaces. In Rails 4.2 this changed to only bind to 127.0.0.1 by default - there's nothing in the application logs because the socket is simply not listening to connection on other network interfaces.
To allow connections from another machine you need to use the -b option to bind to extra ip addresses, eg
rails s -b 0.0.0.0
to bind to all available ip addresses. You can of course replace 0.0.0.0 with one of your machine's actual ip addresses although you would of course have to change that invocation whenever your machine's ip address changed.

Access Webrick Rails Server from local network

Hello i am trying to access my Webrick rails server that i started on my ubuntu to my windows 7 pc locally. I checked on ifconfig in my virtual box and used the inet:10.0.2.15 when i run my rails server and also used port 80 like this:
rails server -b 10.0.2.15 -p 80
It didn't gave me an error in the terminal but when i try to go to 10.0.2.15 on my windows browser, it says it can't connect. I tried disabling the firewall in windows but still wont work. Thank you.
Choose another network type for your VM ubuntu. I suggest the Bridged Network. Reboot vm, it should obtain ip in same network as your windows host (example 192.168.0.XXX). Then you will be able reach your rails server from network At any port 80 or 3000.
Can you share your ubuntu with the vm, via the apache server too? is that started? In macs you have to enable web sharing (or something similar to that wording). ON ubuntu what happens when you do localhost:80... if i recall it should say it works! or something like that.

Resources