In this post, I would show how to solve 502 bad gateway error when using proxy_pass in Nginx.
3. The nginx.conf
We can define an upstream in nginx to reverse proxy local tomcat service like this:
The service layout:
When user nagivate to http://xxx.com/myservice, then it would be reverse proxied to localhost’s tomcat instances(8081 and 8082).
4. The problem
When your business grows, the concurrent http requests would burst in a short time, sometimes, your users would get a error like this when requesting your services:
5. How to solve this 502 bad gateway problem?
5.0 Why did this happen?
Because you did not use the keep-alive feature of http, every request create a new connection, then there are too many connections in your server.
Default http 1.0 connections and requests:
You can see that every request create a new connection,so your server would suffer from the surge requests.
The http 1.1 connections and requests:
You can see that there are only a few connections between the nginx and local service, many of them are reused between http requests.
5.1 Try to use http keep alive feature
Change your server settings in nginx.conf, try to activate http 1.1 keep-alive features like this:
- “proxy_http_version 1.1” means use http 1.1, because the keep-alive feature is a default feature in http 1.1 protocol
- “proxy_set_header Connection “”” means clear the client’s connection headers, use http 1.1 header
5.2 Set the maximum number of idle keep-alive connections in upstream
Change your upstream settings like this:
5.3 Decrease the keep-alive timeout
6. The final nginx.conf
You should always try to use the keep-alive feature of http, it would help you to obtain good user experience and good revenue too.