cURL cannot connect to localhost but browser can - ruby-on-rails

I'm running a Rails app locally (Thin server), and I can connect locally from the browser (localhost:3000), but when I try to use curl, I get:
curl -H 'id:1' -i 'http://localhost:3000/api/data' -v
* Hostname was NOT found in DNS cache
* Trying ::1...
* Adding handle: conn: 0x7fd121808200
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x7fd121808200) send_pipe: 1, recv_pipe: 0
* Connection failed
* connect to ::1 port 3000 failed: Connection refused
* Trying fe80::1...
* Connection failed
* connect to fe80::1 port 3000 failed: Connection refused
* Failed to connect to localhost port 3000: Connection refused
* Closing connection 0
curl: (7) Failed to connect to localhost port 3000: Connection refused
This used to work just fine, but I recently updated to Mavericks, which I suspect may have broken something. I can also curl successfully from the web.

This is a curl bug (a strange one), where curl fails to fall back to IPv4 if there's an IPv6 entry in /etc/hosts that doesn't respond.
You can force it to use IPv4 via the -4 option.

Instead of
curl localhost:3000
try
curl 0.0.0.0:3000
It works.
If it doesn't, check the output when you start your rails server:
=> Booting WEBrick
=> Rails 3.2.13 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2014-10-28 15:30:08] INFO WEBrick 1.3.1
[2014-10-28 15:30:08] INFO ruby 2.0.0 (2014-02-24) [x86_64-darwin12.5.0]
[2014-10-28 15:30:08] INFO WEBrick::HTTPServer#start: pid=4004 port=3000
Use the url at the end of line 2 instead of ENV['HOST']

Are you behind a proxy? Then use the below to bypass the proxy altogether.
curl -x "" "http://127.0.0.1:3000"

Assuming that you have not touched /etc/hosts and scutil reports are positive:
$ scutil -r 127.0.0.1
Reachable,Local Address
$ scutil -r localhost
Reachable,Local Address
then my guess is that the Firewall is active and not accepting connections from curl.
Try adding curl to the list accepted applications under (I'm guessing at the language alternatives since my machine is set to use Swedish) Preference --> Security --> Firewall alternatives --> plus sign --> (serach for curl and add it)
Note: make sure that you add the curl that you are actually using in your shell.
$ type -a curl
curl is /opt/local/bin/curl
curl is /usr/bin/curl

Another cause of this issue can be a poorly configured system with a proxy address. Run
export http_proxy=
and rerun the curl command, to eliminate this as a contributing factor. It fixed this issue for me.

Restarting the computer can sometimes fix odd connection issues. I'm not sure why.

Related

Getting error curl: (56) Recv failure: Connection reset by peer

I know this error has been posted before on StackOverflow and many solutions/answers are available too. But, I've already gone through all those answers and couldn't find any viable solution for me.
I'm running a Hyperledger Fabric network with single peer and orderer. Their Docker containers have exposed the following ports. 7051:7051 & 7053:7053 on peer, 7050:7050 on orderer. I'm trying to configure Prometheus to analyse the metrics by following the official documentation .
As mentioned in the documentation, I'm exposing my local machine's 9443 port to port 9443 of peer docker container by mapping it as 9443:9443 in ports section of docker-compose.yaml. When I run curl 0.0.0.0:9443, I get curl: (56) Recv failure: Connection reset by peer error.
However, when I run the command curl 0.0.0.0:9443/logspec in my peer container I get the desired result which is {"spec":"info"}. The two commands mentioned above are different but I've also tried running the command curl 0.0.0.0:9443/logspec in my local machine to which I got the same error response.
Running the command curl -v 0.0.0.0:9443 results in following response.
* About to connect() to 0.0.0.0 port 9443 (#0)
* Trying 0.0.0.0...
* Connected to 0.0.0.0 (0.0.0.0) port 9443 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: 0.0.0.0:9443
> Accept: */*
>
* Recv failure: Connection reset by peer
* Closing connection 0
curl: (56) Recv failure: Connection reset by peer
I read in many of the answers that I might be a firewall issue. But even after disabling my firewall using the command systemctl disable firewalld, it's not working.
cat etc/os-release response
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"
curl --version response
curl 7.29.0 (x86_64-redhat-linux-gnu) libcurl/7.29.0 NSS/3.34 zlib/1.2.7 libidn/1.28 libssh2/1.4.3
I request you not to mark this question as duplicate as I've already gone through all the possible scenarios mentioned here.
First of all please check if there is firewall. Disable it.
Then remove the IP address provided in core.yaml file in operations tab. So that line will look like a below:
operations:
# host and port for the operations server
listenAddress: :9443
This change worked for me.

When I try to curl using internal ip it works but the same doesn't work with external ip on GCP?

I'm bit new to GCP, I am trying to run a curl command using internal ip this works but when tried with external ip it doesn't response
curl -vvvv http://10.128.0.3:50000
* Rebuilt URL to: http://10.128.0.3:50000/
* Trying 10.128.0.3...
* Connected to 10.128.0.3 (10.128.0.3) port 50000 (#0)
> GET / HTTP/1.1
> Host: 10.128.0.3:50000
> User-Agent: curl/7.47.0
> Accept: */*
>
* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< Content-Type: text/plain;charset=UTF-8
<
Jenkins-Agent-Protocols: JNLP4-connect, Ping
Jenkins-Version: 2.121.3
Jenkins-Session: 4c0a93f0
Client: 10.128.0.3
Server: 10.128.0.3
Remoting-Minimum-Version: 2.60
* Closing connection 0
curl -vvvv http://35.232.33.183:50000
* Rebuilt URL to: http://35.232.33.183:50000/
* Trying 35.232.33.183...
Confirm that the firewall is properly configured by running the ff:
telnet 35.232.33.183 50000
You should get a prompt (which exact prompt depends on the remote server's OS.
The other option is to expose your internal IP to external world using tools like ngrok. Use the following steps to install and run ngrok. (This example is for Ubuntnu 16.04 on GCP and should work on other platforms too)
The following instructions are for using ngrok to expose localhost application to
internet. This is tested on CGP, Ubuntu 16.04
Step 1: Download ngrok
Login into https://ngrok.com/ and download Linux(32-bit) zip file to your google
instance (you may choose the right version for your platform else you will get a segmentation fault while trying to execute)
Step 2: Unzip executable
mkdir ngrok
cd ngrok
unzip ngrok-stable-linux-386.zip
Step 3: Run your app
Run your app which is listening on port 5000 of localhost.
Python3 main.py
The output will look like this:
name#instance-1:~$ python3 main.py
Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
Restarting with stat
Debugger is active!
Debugger PIN: 279-195-470
Your app is now listening on port 5000 of the local machine
Step 4: Run ngrok
For exposing an app listening on port 5000, run the following command
./ngrok http 5000
The output will look like this:
ngrok by #inconshreveable (Ctrl+C to quit)
Session Status online
Session Expires 7 hours, 59 minutes
Version 2.2.8 Region United States (us)
Web Interface http://127.0.0.1:4040
Forwarding http://f985f882.ngrok.io -> localhost:5000
Forwarding https://f985f882.ngrok.io -> localhost:5000
Connections ttl opn rt1 rt5 p50 p90 0 0 0.00 0.00 0.00 0.00
Step 5: Test your app
Run this command:
curl http://f985f882.ngrok.io
The output will look like:
Mynames-MacBook-Pro:~ Myname$ curl http://f985f882.ngrok.io
I am running
You can also access the above url from any browser
In conclusion
ngrok is very simple and easy tool to use

Travis CI: start rails server

I need to start a rails server on Travis to run integration tests.
I've added this to the config file:
before_script:
- psql -c 'create database scalia_test;' -U postgres
- "bundle exec rails server -p 3000 &"
However, I still get an error from Cypress:
http://localhost:3000/users/sign_in
We attempted to make an http request to this URL but the request failed without a response.
We received this error at the network level:
> Error: connect ECONNREFUSED 127.0.0.1:3000
Common situations why this would fail:
- you don't have internet access
- you forgot to run / boot your web server
- your web server isn't accessible
- you have weird network configuration settings on your computer
The stack trace for this error is:
Error: connect ECONNREFUSED 127.0.0.1:3000
at Object.exports._errnoException (util.js:1024:11)
at exports._exceptionWithHostPort (util.js:1047:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1150:14)
Does anybody knows how to start a Rails server on Travis ?
You are using & to send the command to the background, then you are running your tests right?
By the time you are running the tests Travis is still booting up your Rails server in the background therefore it's erroring out saying it can't connect. In my opinion it doesn't have to do anything with port binding.
In order to fix that you should use the -d parameter to daemonize rails after it started:
before_script:
- psql -c 'create database scalia_test;' -U postgres
- "bundle exec rails server -p 3000 -d"
rails server will block until it listens on port 3000 then sends itself to the background and continue running your scripts.
Do note that after your tests have run you may kill the rails server again using:
kill -9 $(cat tmp/pids/server.pid)
Otherwise you'll get a timeout or another error from travis.
Try binding rails to the loopback IP in that port
bundle exec rails s -p 3000 -b 127.0.0.1
probably that port is in use. Type lsof -i :8000 in terminal to see if that port is beeing used
U can try to start server with difrent port like rails server -p 3050

Redirect 192.XX.XX to localhost - OSX Yosemite

I would like to redirect 192.168.199.128 to localhost on OSX Yosemite. I'm running a Rails server on port 3000 and I would like to access it using localhost:3000/home as well as 192.168.199.128:3000/home.
I understand that iptables was removed in OSX Yosemite and pf is the preferred method of doing port forwarding but I'm unable to get it working.
This appears in /etc/pf.anchors/com.analysis, with a newline below it
rdr pass on lo8 proto tcp from any to 192.168.199.128 port 3000 -> 127.0.0.1 port 3000
Additionally, I added the following line to the end of /etc/pf.conf
load anchor "com.analysis" from "/etc/pf.anchors/com.analysis"
Finally, I started pf with the following command:
sudo pfctl -ef /etc/pf.anchors/com.analysis
Unfortunately, I still cannot access my rails server at 192.168.199.128:3000/home.
Thanks in advance for your help.
rails server -b 192.xxx.xxx.xxx

After OSX 10.8 upgrade postgres can not connect to the server

After a recent upgrade to Mountain Lion, I've run into problems with my brewed postgres install.
$ rake db:create
>rake db:migratecould not connect to server: Connection refused
> Is the server running on host "localhost" (::1) and accepting
> TCP/IP connections on port 5432?
>could not connect to server: Connection refused
> Is the server running on host "localhost" (fe80::1) and accepting
> TCP/IP connections on port 5432?
>could not connect to server: Connection refused
> Is the server running on host "localhost" (127.0.0.1) and accepting
> TCP/IP connections on port 5432?
After searching around the interwebs, I haven't yet found a solution to this issue.
I have found a suggested steps to help identify the problem but after following these, I'm not sure how to understand the results or what to do next. Can anyone help?
$ pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
>pg_ctl: another server might be running; trying to start server anyway
>server starting
$ pg_ctl -D /usr/local/var/postgres stop -s -m fast
>pg_ctl: could not send stop signal (PID: 865): No such process
$ pg_ctl status
>pg_ctl: no server running
$ ps auxw | grep post
>myuser 19037 2.6 7.7 4388248 324520 ?? S 7:30AM 19:06.02 /Applications/Postbox.app/Contents/MacOS/postbox-bin -psn_0_917728
>myuser 54897 0.1 0.0 2432768 464 s000 R+ 1:47PM 0:00.01 grep post
$ cat /usr/local/var/postgres/server.log
>FATAL: lock file "postmaster.pid" already exists
>HINT: Is another postmaster (PID 821) running in data directory "/usr/local/var/postgres"?
Happy to provide any additional info that may be useful.
try removing:
/usr/local/var/postgres/postmaster.pid
This once worked for me.

Resources