Linux – 504 bad gateway with Nginx

504linuxnetworkingnginxvps

I have a J2EE application hosted on a linux VPS server. I use nginx as the web server and tomcat6 as the application server. At times, the website keeps waiting to load or sometimes even throws a 504 bad gateway nginx error. During this downtime of a few minutes(typically 3-5 minutes), if I try to access the server via SSH, I get a connection timeout error. When the website starts working after a few minutes, I noticed that tomcat, nginx are still running on the same process id as before. I do not need to restart tomcat or nginx, it starts working by itself. Also once the site is running again, I also notice that the SSH connection to the server also succeeds.

I am not sure where exactly the issue lies? Is it a connectivity issue with the server or is it some error in my config? Any insights would be appreciated.

Best Answer

You're using a Xen VPS with 1GB of RAM and 2GB of swap, and your VPS provider has the same ratio of RAM to swap for all other customers, including 256MB of swap for the 128MB of RAM for customers on the lowest tier.

That would seem like a very high swap to RAM ratio for a VPS hoster, especially if they offer low tiers like 128MB of RAM with 256MB of swap, which invariably run out of RAM all the time when used by unsuspecting novice users.

It would sound like the Bad Gateway error you're receiving is caused by the overly long delay in the reply of your J2EE app, which might be caused by all the swapping going on.

Your best bet is test the performance of the server. You should probably start by running dd to test disc throughput, then followed up by some kind of UNIX benchmarking tool.