I’m having this strange error, CURL ERROR: Recv failure: Connection reset by peer
This is how it happens, if I did not connect to the server and all of a sudden trying to connect to the server via CURL in PHP I get the error. When I run the CURL script again the error disappears and then works well the whole time, if I leave the remote server idle for about 30mins or reboot the remote server and try to connect again, I get the error again. So it seems like the connection is idle and then all of sudden the server wakes up and then works and then sleeps again.
This is how my CURL script looks.
$url = Yii::app()->params['pdfUrl'];
$body = 'title='.urlencode($title).'&client_url='.Yii::app()->params['pdfClientURL'].'&client_id='.Yii::app()->params['pdfClientID'].'&content='.urlencode(htmlentities($content));
$c = curl_init ($url);
$body = array(
"client_url"=>Yii::app()->params['pdfClientURL'],
"client_id"=>Yii::app()->params['pdfClientID'],
"title"=>urlencode($title),
"content"=>urlencode($content)
);
foreach($body as $key=>$value) { $body_str .= $key.'='.$value.'&'; }
rtrim($body_str,'&');
curl_setopt ($c, CURLOPT_POST, true);
curl_setopt ($c, CURLOPT_POSTFIELDS, $body_str);
curl_setopt ($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($c, CURLOPT_CONNECTTIMEOUT , 0);
curl_setopt ($c, CURLOPT_TIMEOUT , 20);
$pdf = curl_exec ($c);
$errorCode = curl_getinfo($c, CURLINFO_HTTP_CODE);
$curlInfo = curl_getinfo($c);
$curlError = curl_error($c);
curl_close ($c);
I’m totally out of ideas and solutions, please help, I’ll appreciate it!!!
If I verbose the output to see what happens using
curl_setopt ($c, CURLOPT_VERBOSE, TRUE);
curl_setopt($c, CURLOPT_STDERR, $fp);
I get the following
* About to connect() to 196.41.139.168 port 80 (#0)
* Trying 196.x.x.x... * connected
* Connected to 196.x.x.x (196.x.x.x) port 80 (#0)
> POST /serve/?r=pdf/generatePdf HTTP/1.1
Host: 196.x.x.x
Accept: */*
Content-Length: 7115
Content-Type: application/x-www-form-urlencoded
Expect: 100-continue
* Recv failure: Connection reset by peer
* Closing connection #0
012 20:23:49 GMT
< Server: Apache/2.2.15 (CentOS)
< X-Powered-By: PHP/5.3.3
< Connection: close
< Transfer-Encoding: chunked
< Content-Type: text/html; charset=UTF-8
<
* Closing connection #0
I’ve added in the following toe remove the default header and still no luck:
curl_setopt ($c, CURLOPT_HTTPHEADER, array( 'Expect:' ) );
> Accept: */* Content-Length: 8414 Content-Type:
> application/x-www-form-urlencoded
>
> * Recv failure: Connection reset by peer
> * Closing connection #0 r: Apache/2.2.15 (CentOS) < X-Powered-By: PHP/5.3.3 < Connection: close < Transfer-Encoding: chunked <
> Content-Type: text/html; charset=UTF-8 <
> * Closing connection #0
Having trouble with cURL 56 recv failure connection reset by peer in the CentOS server?
The error occurs mainly due to failure in receiving network data.
At Bobcares, we often get requests to fix cURL errors as part of our Server Management Services.
Today, let’s get into the details on how our Support Engineers fix this error.
Why does cURL 56 recv failure connection reset by peer occurs?
cURL error 56 is a temporary error that happens due to different reasons like, idle server connection, firewall restrictions, whitespace in the URL, etc.
For instance, the error appears as follows.
Let’s detail how our Support Engineers fix this for our customers.
How we fix this error cURL error?
At Bobcares, we have more than a decade of expertise in managing servers, we see many customers facing errors. It happens mainly when fetching package updates, installing new software, etc.
Now, let’s see the major reasons and how our Support Engineers fix this error.
Idle server connection
Recently, one of the customers approached us with a cURL error. He was getting the following error while transferring the backup to the remote destination.
cURL error 56: TCP connection reset by peer
So we checked the backup log files and confirmed that the backups have been generated successfully.
On further checking, we found that the server was idle while transferring the backups to the remote destination. However, the backups were successfully exported to Google drive.
In addition, to avoid this situation, the tips we follow are:
- We always upgrade the user’s OS to avoid TCP/IP issues.
- Also, we use the recent versions of PHP and cURL.
- Set the Maximum Transmission Unit (MTU) or the size of packets traveling over the network to the default value, 1500 bytes.
- We ensure that the firewall is not blocking the user’s connection.
Bad repository
Often bad repository URL can also trigger curl 56 errors.
For instance, while trying to install PHP on CentOS7 server one of our customers received the error:
Loaded plugins: fastestmirror, langpacks
http://mirror.xxx.net/el7-x86_64/rpms/ ... repomd.xml: [Errno 14] curl#56 - "Recv failure: Connection reset by peer"
Trying other mirror.
Here, the customer was using an outdated repository and that caused the error. Therefore, we corrected the repo settings on the server and the package install went on fine.
Similarly, the repository may fail to load due to DNS resolution failures as well. In such scenarios, we make use of the /etc/hosts files to map the repo website to the correct IP address.
Whitespace in the URL
Another customer approached us with a similar problem.
On further investigation, we found whitespace in the URL. We then removed whitespace from the URL.
And in the PHP settings, we added the following.
curl_setopt($ch, CURLOPT_URL, trim($url));
In addition, If whitespace is a valid part of the URL, we use rawurlencode() function in PHP, to store the URL as encoded including whitespace.
[Need assistance in fixing cURL errors? – We can help you.]
Conclusion
In short, cURL 56 recv failure connection reset by peer in CentOS occurs when the server is idle due to running a long process or due to improper usage of URL. Today, we saw how our Support Engineers fixed the error.
PREVENT YOUR SERVER FROM CRASHING!
Never again lose customers to poor server speed! Let us help you.
Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.
GET STARTED
var google_conversion_label = «owonCMyG5nEQ0aD71QM»;
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and
privacy statement. We’ll occasionally send you account related emails.
Already on GitHub?
Sign in
to your account
Closed
hwypengsir opened this issue
Sep 16, 2016
· 6 comments
Comments
If you enable debug tracing with --trace-ascii dump.txt
, does it shed any more light? «Recv failure» is a really generic error so its hard for us to tell without more info. Can you attach such a debug trace here and we might be able to tell something more?
curl -x «vps_ip:80» —trace-ascii /tmp/dump.txt https://dl-ssl.google.com/linux/linux_signing_key.pub -o /tmp/key.pub
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 —:—:— —:—:— —:—:— 0curl: (56) Recv failure: Connection reset by peer
cat /tmp/dump.txt
== Info: Hostname was NOT found in DNS cache
== Info: Trying vps_ip…
== Info: Connected to vps_ip (vps_ip) port 80 (#0)
== Info: Establish HTTP proxy tunnel to dl-ssl.google.com:443
=> Send header, 126 bytes (0x7e)
0000: CONNECT dl-ssl.google.com:443 HTTP/1.1
0028: Host: dl-ssl.google.com:443
0045: User-Agent: curl/7.38.0
005e: Proxy-Connection: Keep-Alive
007c:
== Info: Recv failure: Connection reset by peer
== Info: Received HTTP code 0 from proxy after CONNECT
== Info: Closing connection 0
It looks like your proxy simply doesn’t respond properly here.
To close the firewall ,problem solved.
Ok thanks for the update.
close firewall is workless
lock
bot
locked as resolved and limited conversation to collaborators
May 6, 2018
I am trying to download one file from my local server using curl.i am using command
curl -L -O --retry 999 --retry-max-time 0 -C - http://192.168.1.74/gio-v2-9515.dd -u ajay:aj123
For experiment purpose i disconnected my Internet connection for two minutes but according to this will resume automatically .But in my case i am getting error «curl: (56) Recv failure: Connection reset by peer».At the same time if i reconnect the connection with in 3 sec or 5 secs its will resume automaticaaly.so whats the actual issue.Any help would be appreciated .My local sever is working in Windows and my local machine is Ubuntu
asked Jun 2, 2016 at 12:40
2
You need to use --retry-all-errors
Retry on any error. This option is used together with —retry.
Added in 7.71.0.
https://curl.se/docs/manpage.html
--retry-connrefused
will not work with a Connection reset by peer
error
answered Nov 5, 2021 at 8:34
csanchezcsanchez
2842 silver badges6 bronze badges
1. Purpose
In this post, I would demo how to solve the connection errors when connecting to docker container.
2. Environment
- Docker Server Version: 20.10.2
- Kernel Version: 4.15.0-128-generic
Operating System: Ubuntu 18.04.5 LTS
OSType: linux
Architecture: x86_64
3. The code and the problem
3.1 The RESTful web service inside the container
I am developing a simple flask web service(RESTful) for test purpose, this little app just do the following two things:
- Exporting a web service at localhost’s port 8080
- When user access the url ‘http://localhost:8080’, it should return a hello world json string
The core code is as follows:
app1.py
from flask import Flask
from flask_restful import Resource, Api
app = Flask(__name__)
api = Api(app)
class HelloWorld(Resource):
def get(self):
return {'hello': 'world'}
api.add_resource(HelloWorld, '/')
if __name__ == '__main__':
app.run(debug=True, port=8080)
If you don’t know flask-restful, here is the simple introduction:
Flask-RESTful is an extension for Flask that adds support for quickly building REST APIs. It is a lightweight abstraction that works with your existing ORM/libraries. Flask-RESTful encourages best practices with minimal setup. If you are familiar with Flask, Flask-RESTful should be easy to pick up.
Just install it as follows:
pip install flask-restful
More information about flask-restful can be found here.
When I run it as follows:
I got this result, it indicates that the app is running.
* Serving Flask app "app1" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://127.0.0.1:8080/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 717-204-539
Then we test the service via curl command:
curl http://localhost:8080/
we got this:
3.2 The Dockerfile
Now I want to package the app as a docker image, and then run it as a docker container, so I must write a dockerfile to construct the docker image.
First, we define a file named requirements.txt to define the dependency requirements for pip command , which would be used inside the container.
requirements.txt
Flask==1.1.1
flask-restful=0.3.8
Dockerfile
FROM python:3.6-buster
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY app1.py .
CMD ["python","app1.py"]
In the above Dockerfile, we have done these steps:
- This docker image is based on python:3.6-buster, which is an offical python image
- The ‘WORKDIR’ defines our working directory inside the docker container, docker would create a directory named /app inside the docker container, and all the files copied would be put to this directory
- Then we copy the requirements.txt and do the ‘pip install -r …’ to install the python dependencies for the app, the ‘-r’ option of pip is explained as follows:
-r, --requirement <file>¶
Install from the given requirements file. This option can be used multiple times.
- Then we copy the app1.py to our working directory
- At last, we execute the python command to start our application
3.3 Build and run the docker image
Run the ‘docker build ‘ command in the project’s root directory:
docker build -t app1 -f ./Dockerfile .
The above command build a docker image named ‘app1’.
Then we run the docker image :
docker run --name app1 -p 8080:8080 --restart no --rm --detach app1
The above docker command runs the image ‘app1’, create a container named ‘app1’, exported the network port 8080 to host port 8080.
3.4 The connection problem
After running the docker container, we can verify it’s running:
[email protected]:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ad7b82319916 app1 "python app1.py" 18 hours ago Up 18 hours 0.0.0.0:8080->8080/tcp app1
Check the logs of the service inside the container:
[email protected]:~/app-flask-restful# docker logs -f ad
* Serving Flask app "app1" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://127.0.0.1:8080/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 160-222-920
Now we use ‘curl’ to connect to the docker container:
curl http://localhost:8080/
We get this error or exception:
curl: (56) Recv failure: Connection reset by peer
Or if we try to telnet the service’s port, we get this :
[email protected]:~# curl http://127.0.0.1:8080/
curl: (56) Recv failure: Connection reset by peer
[email protected]:~# telnet 127.0.0.1 8080
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
Connection closed by foreign host.
[email protected]:~#
Why did this happen? I think it should run, but it does not work!
4. Debug
I am suspecting that the docker network caused the problem, so I’d like to change the docker network to ‘host’ , by default, docker use the ‘bridge’ network :
bridge
: The default network driver. If you don’t specify a driver, this is the type of network you are creating. Bridge networks are usually used when your applications run in standalone containers that need to communicate. See bridge networks.host
: For standalone containers, remove network isolation between the container and the Docker host, and use the host’s networking directly. See use the host network.
If we switch to ‘host’ network, the service is bound to the host’s network interface directly.
4.1 Stop the old container
We stop the old container as follows:
docker stop <container-id>
4.2 Start the docker container with network host
[email protected]:~# docker run --network host -d app1
11dacad2f2b9650151dcfbbee15884341f73fab988d043279c601fe672038650
4.3 Test the connection
It works, so the problem is identifed, the service is working on its own, but it can not be accessed from outside(docker network bridge), so ,we should change our app, it should bind to ‘0.0.0.0’ instead of ‘localhost’.
0.0.0.0
has a couple of different meanings, but in this context, when a server is told to listen on 0.0.0.0
that means “listen on every available network interface”. The loopback adapter with IP address 127.0.0.1
from the perspective of the server process looks just like any other network adapter on the machine, so a server told to listen on 0.0.0.0
will accept connections on that interface too.
5. The Solution
5.1 Change the network bound policy
Now we should bind to the network interface ‘0.0.0.0’.
if __name__ == '__main__':
app.run(debug=True, port=8080, host="0.0.0.0")
5.2 Repackage the docker image
docker build -t app1 -f ./Dockerfile .
Sending build context to Docker daemon 6.656kB
Step 1/6 : FROM python:3.6-buster
---> d2c8d8ff1eb5
Step 2/6 : WORKDIR /app
---> Running in 74301bc0025e
Removing intermediate container 74301bc0025e
---> 4cf5b17bd895
Step 3/6 : COPY requirements.txt .
---> b63a56f6b8ca
Step 4/6 : RUN pip install -r requirements.txt
---> Running in ca94aa9c0537
Collecting Flask==1.1.1
Downloading Flask-1.1.1-py2.py3-none-any.whl (94 kB)
Collecting flask-restful==0.3.8
Downloading Flask_RESTful-0.3.8-py2.py3-none-any.whl (25 kB)
Collecting itsdangerous>=0.24
Downloading itsdangerous-1.1.0-py2.py3-none-any.whl (16 kB)
Collecting Jinja2>=2.10.1
Downloading Jinja2-2.11.3-py2.py3-none-any.whl (125 kB)
Collecting Werkzeug>=0.15
Downloading Werkzeug-1.0.1-py2.py3-none-any.whl (298 kB)
Collecting click>=5.1
Downloading click-7.1.2-py2.py3-none-any.whl (82 kB)
Collecting six>=1.3.0
Downloading six-1.15.0-py2.py3-none-any.whl (10 kB)
Collecting pytz
Downloading pytz-2021.1-py2.py3-none-any.whl (510 kB)
Collecting aniso8601>=0.82
Downloading aniso8601-9.0.1-py2.py3-none-any.whl (52 kB)
Collecting MarkupSafe>=0.23
Downloading MarkupSafe-1.1.1-cp36-cp36m-manylinux2010_x86_64.whl (32 kB)
Installing collected packages: MarkupSafe, Werkzeug, Jinja2, itsdangerous, click, six, pytz, Flask, aniso8601, flask-restful
Successfully installed Flask-1.1.1 Jinja2-2.11.3 MarkupSafe-1.1.1 Werkzeug-1.0.1 aniso8601-9.0.1 click-7.1.2 flask-restful-0.3.8 itsdangerous-1.1.0 pytz-2021.1 six-1.15.0
Removing intermediate container ca94aa9c0537
---> 520bec4911cc
Step 5/6 : COPY app1.py .
---> bba5c08e863d
Step 6/6 : CMD ["python", "app1.py"]
---> Running in 95b36bb37990
Removing intermediate container 95b36bb37990
---> 9e322e9ea3e7
Successfully built 9e322e9ea3e7
Successfully tagged app1:latest
5.3 Rerun the docker container
[email protected]:~/app-flask-restful# docker run --name app1 -p 8080:8080 --restart no --rm --detach app1
efd04c269d87d78ea59d047b90daeda6e2351668f2dcfbf417887e112b2f9399
[email protected]:~/app-flask-restful#
[email protected]:~/app-flask-restful# docker logs efd
* Serving Flask app "app1" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 188-194-998
[email protected]:~/app-flask-restful#
5.4 Test the connection again
[email protected]:~/app-flask-restful# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
efd04c269d87 app1 "python app1.py" About a minute ago Up About a minute 0.0.0.0:8080->8080/tcp app1
[email protected]:~/app-flask-restful# curl http://127.0.0.1:8080
Now it works.
6. Summary
In this post, we demonstrated how to solve the connection problem when connecting to a docker container, the key point is that the container’s service is bound to localhost or 127.0.0.1, which can not be accessed from outside, you should change the code or configuration to bind to ip address ‘0.0.0.0’.
This article will inform about how to solve a specific error message. The error message appears upon cloning an instance exist in the private Git Repository. In this context, it is a simple task to clone a repository exist in the Gitlab Repository. Below is the complete error message :
user@hostname:~/docker/apps$ git clone http://srv.mygitlab.net/gitlab/project/myapp.git Cloning into 'myapp'... Username for 'http://srv.mygitlab.net': myself Password for 'http://myself@srv.mygitlab.net': remote: Enumerating objects: 4737, done. remote: Counting objects: 100% (4737/4737), done. remote: Compressing objects: 100% (2600/2600), done. error: RPC failed; curl 56 Recv failure: Connection reset by peer fatal: the remote end hung up unexpectedly fatal: early EOF fatal: index-pack failed user@hostname:~/docker/apps$
According to the output of the above error message, the process for cloning a Git-based repository doesn’t end well. The repository cloning itself stop in the middle of the process. So, there is no clone repository exist after the above command execution. It is because the result from executing the above command ends in failure. The reason behind the failure is actually the size of the repository for cloning process. The size is too large and it cause the process terminate in the middle of the cloning process. In order to solve the problem, there is a solution in order to achieve it. It is done by changing the HTTP Post buffer value of the Git command. The following is the steps for execution sequence of the solution for solving the problem :
1. Try to check the setting for the HTTP Post Buffer of the git command. Just execute the following command pattern :
git config --get http.postBuffer
The following is the execution of the above command pattern in the first place before configuring the value of it :
user@hostname:~/docker/app$ git config --get http.postBuffer user@hostname:~/docker/app$
2. Apparently, at first it doesn’t generate any output because of there is no definite value set before. Although it doesn’t appear any output in the form of number indicating the size of the HTTP Post Buffer, by default it has the size of 1 MB. So, in order to increase the size for the HTTP Post Buffer, just give the number as in the following example :
user@hostname:~/docker/app$ git config --global http.postBuffer 524288000 user@hostname:~/docker/app$
3. The setting above is for defining the HTTP Post Buffer into 500 MB. So, in order to make sure that the value definition is exactly correct, just execute the following command to retrieve the HTTP Post Buffer value :
user@hostname:~/docker/app$ git config --get http.postBuffer 524288000 use@hostname:~/docker/app$
Actually, the solution for the above problem exists in the following link. Just read it to get additional information on how to solve the problem. Finally, try to clone the repository again as it exist in the previous part.