# 502 Error Issue

**URL:** <https://www.ruby-forum.com/t/502-error-issue/242443>\
**Category:** NGINX\
**Created:** [June 24, 2015, 12:46pm UTC](https://www.ruby-forum.com/t/502-error-issue/242443 "2015-06-24T12:46:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![musicdenotation](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ea666f/32.png) [@musicdenotation](https://www.ruby-forum.com/u/musicdenotation)\
**Post date:** [June 24, 2015, 12:46pm UTC](https://www.ruby-forum.com/t/502-error-issue/242443/1 "2015-06-24T12:46:15Z")

</div>

Hi All,

We recently migrated from apache to nginx.

OS - CetnOS 6  
Nginx - 1.62 (4 CPU 8 GB RAM)  
PHP-FPM (php 5.4.37) (4 CPU 8 GB RAM)  
APC (Code Cache APC 3.1.13 beta)  
Memcache (data cache)

I have upstream of 4 php servers for php-fpm service.

I am facing two issues:

1. I am getting 502 status code in access log means nginx generating 502  
bad  
gateway error

2. Randomly connections got pile up on nginx up to 2500 and on php-fpm  
servers we do not see any load in top only 1-2 process seems to be  
running  
during that time and without taking any restart after 2-3 minutes it  
starts  
working normally and connections goes down to normal without taking any  
restart.

Please check nginx configuration in-line

user nguser;  
worker\_processes 4;

pid /var/run/nginx.pid;  
worker\_rlimit\_nofile 70000;

events {  
worker\_connections 1024;  
multi\_accept on;  
use epoll;  
}

http {  
##  
# Basic Settings  
##

```
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_requests 1000;
keepalive_timeout 65;
send_timeout 15;
types_hash_max_size 2048;
server_tokens off;
client_max_body_size 50M;
client_body_buffer_size 1m;
client_body_timeout 15;
client_header_timeout 15;
server_names_hash_bucket_size 64;
server_name_in_redirect off;

include /etc/nginx/mime.types;
default_type application/octet-stream;

##
# Logging Settings

```

## 

```
#access_log on;

access_log /var/log/nginx/access.log combined;
#error_log /dev/null crit;
error_log /var/log/nginx/error.log error;

#gzip on;
gzip on;
gzip_static on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_min_length 512;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/css text/javascript text/xml text/plain

```

text/x-component  
application/javascript application/x-javascript application/json  
application/xml application/rss+xml font/truetype  
application/x-font-ttf  
font/opentype application/vnd.ms-fontobject image/svg+xml;

```
open_file_cache max=2000 inactive=20s;
open_file_cache_valid 60s;
open_file_cache_min_uses 5;
open_file_cache_errors off;

fastcgi_buffers 256 16k;
fastcgi_buffer_size 128k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_intercept_errors on;

```

reset\_timedout\_connection on;

```
upstream fastcgiservers {
    least_conn;
    server xxx.xxx.xx.xx:9000;
    server xxx.xxx.xx.xx:9000;
    server xxx.xxx.xx.xx:9000;
    server xxx.xxx.xx.xx:9000;
   }

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;

```

}

location ~ .php$ {  
try\_files $uri =404;  
include fastcgi\_params;  
fastcgi\_pass fastcgiservers;  
fastcgi\_index index.php;  
fastcgi\_param SCRIPT\_FILENAME  
$document\_root$fastcgi\_script\_name;  
}

## ---------------------------------------------------------PHP-FPM Settings

pm.max\_children = 150  
pm.start\_servers = 90  
pm.min\_spare\_servers = 70  
pm.max\_spare\_servers = 100  
pm.max\_requests = 1500

Any help or suggestion would be greatly appreciated.

Thanks

Posted at Nginx Forum:

> **[502 Error Issue](https://forum.nginx.org/read.php?2,259833,259833#msg-259833)**
>
> Hi All, We recently migrated from apache to nginx. OS - CetnOS 6 Nginx - 1.62 (4 CPU 8 GB RAM) PHP-FPM (php 5.4.37) (4 CPU 8 GB RAM) APC (Code Cache APC 3.1.13 beta) Memcache (data cache) I have upstream of 4 php servers for p

---

<div class="post-metadata">

**Author:** ![mudgil.gaurav](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f05b48/32.png) [@mudgil.gaurav](https://www.ruby-forum.com/u/mudgil.gaurav)\
**Post date:** [June 24, 2015, 2:19pm UTC](https://www.ruby-forum.com/t/502-error-issue/242443/2 "2015-06-24T14:19:43Z")

</div>

On June 24, 2015 11:45:38 AM GMT+01:00, “mudgil.gaurav”  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have upstream of 4 php servers for php-fpm service.  
> during that time and without taking any restart after 2-3 minutes it  
> pid /var/run/nginx.pid;
> 
> # Basic Settings
> 
> client\_max\_body\_size 50M;
> 
> # Logging Settings
> 
> gzip\_static on;  
> application/xml application/rss+xml font/truetype  
> fastcgi\_busy\_buffers\_size 256k;  
> server xxx.xxx.xx.xx:9000;  
> include fastcgi\_params;  
> pm.start\_servers = 90  
> pm.min\_spare\_servers = 70  
> pm.max\_spare\_servers = 100  
> pm.max\_requests = 1500

php logs would be useful but I can notice at least that the value for  
pm.max\_requests is too low. I’ve made the same mistake in the first  
times I’ve setup php-fpm servers, IMHO you should defiantly set a value,  
but such a low values means php respaws a lot in a very short period of  
time… I would recommend something higher that 10000…

Also I’ve been having better results with ondemand but that might be  
related to a lot of different things, but you might also give it a try  
if you like.

hope this helps

> [mailman.nginx.org archive](http://mailman.nginx.org/mailman/listinfo/nginx)  
> –  
> Sent from my Android device with K-9 Mail. Please excuse my brevity.
