How to configure Jenkins with Nginx to avoid "Cannot allocate memory" issue

I have Jenkins running behind a Nginx proxy on my Ubuntu 16.04 server. I have connected it with GitHub through webhooks, and the connection works perfectly, however every build fails, and I keep getting the same error log:

Started by user admin
Building in workspace /var/lib/jenkins/workspace/
[WS-CLEANUP] Deleting project workspace...
Cloning the remote Git repository
Cloning repository
 > /usr/bin/git init /var/lib/jenkins/workspace/ # timeout=10
Fetching upstream changes from
 > /usr/bin/git --version # timeout=10
 > /usr/bin/git fetch --tags --progress +refs/heads/*:refs/remotes/origin/* # timeout=15
ERROR: Error cloning remote repo 'origin'
hudson.plugins.git.GitException: Command "/usr/bin/git fetch --tags --progress +refs/heads/*:refs/remotes/origin/*" returned status code 128:
stderr: error: cannot fork() for fetch-pack: Cannot allocate memory

	at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(
	at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandWithCredentials(
	at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.access$300(
	at org.jenkinsci.plugins.gitclient.CliGitAPIImpl$1.execute(
	at org.jenkinsci.plugins.gitclient.CliGitAPIImpl$2.execute(
	at hudson.plugins.git.GitSCM.retrieveChanges(
	at hudson.plugins.git.GitSCM.checkout(
	at hudson.scm.SCM.checkout(
	at hudson.model.AbstractProject.checkout(
	at hudson.model.AbstractBuild$AbstractBuildExecution.defaultCheckout(
	at jenkins.scm.SCMCheckoutStrategy.checkout(
	at hudson.model.AbstractBuild$
	at hudson.model.Run.execute(
	at hudson.model.ResourceController.execute(
ERROR: Error cloning remote repo 'origin'
Finished: FAILURE

I can’t figure out what is going on, because everything seems to work fine. The strange thing is that if I log in to my server as jenkins and I perform these steps manually, everything succeeds.

In addition, Jenkins breaks occasionally and from that point Nginx keeps giving me a 503 error. If I restart the Jenkins service, everything works again.

Submit an answer
Answer a question...

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Accepted Answer

Turned out it was a simple OOM (out-of-memory) problem, which I could easily resolve by creating a swap file. To do so, I have followed this guide:

I have also made some notes, if you are having similar issues you might find it helpful: